Skip to content

Commit

Permalink
image viewer added
Browse files Browse the repository at this point in the history
I think the project is finally done!
  • Loading branch information
alihatamitajik committed Jul 25, 2022
1 parent 8b077ea commit 58c32da
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 18 deletions.
3 changes: 3 additions & 0 deletions Edge Detector/Edge Detector.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,22 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="EdgeDetector.cpp" />
<ClCompile Include="imageviewer.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="panel.cpp" />
<QtRcc Include="Resource.qrc" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="edge.cuh" />
<ClInclude Include="EdgeDetector.h" />
<QtMoc Include="imageviewer.h" />
<QtMoc Include="panel.h" />
</ItemGroup>
<ItemGroup>
<CudaCompile Include="kernel.cu" />
</ItemGroup>
<ItemGroup>
<QtUic Include="imageviewer.ui" />
<QtUic Include="panel.ui" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
Expand Down
3 changes: 3 additions & 0 deletions Edge Detector/Edge Detector.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<ClCompile Include="main.cpp" />
<ClCompile Include="panel.cpp" />
<ClCompile Include="EdgeDetector.cpp" />
<ClCompile Include="imageviewer.cpp" />
</ItemGroup>
<ItemGroup>
<QtRcc Include="Resource.qrc" />
Expand All @@ -17,8 +18,10 @@
</ItemGroup>
<ItemGroup>
<QtMoc Include="panel.h" />
<QtMoc Include="imageviewer.h" />
</ItemGroup>
<ItemGroup>
<QtUic Include="panel.ui" />
<QtUic Include="imageviewer.ui" />
</ItemGroup>
</Project>
14 changes: 14 additions & 0 deletions Edge Detector/imageviewer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "imageviewer.h"

ImageViewer::ImageViewer(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
}

ImageViewer::~ImageViewer()
{}

void ImageViewer::setImage(QPixmap p) {
ui.image->setPixmap(p);
}
17 changes: 17 additions & 0 deletions Edge Detector/imageviewer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <QMainWindow>
#include "ui_imageviewer.h"

class ImageViewer : public QMainWindow
{
Q_OBJECT

public:
ImageViewer(QWidget *parent = nullptr);
~ImageViewer();
void setImage(QPixmap p);

private:
Ui::ImageViewerClass ui;
};
53 changes: 53 additions & 0 deletions Edge Detector/imageviewer.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ImageViewerClass</class>
<widget class="QMainWindow" name="ImageViewerClass">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1005</width>
<height>496</height>
</rect>
</property>
<property name="windowTitle">
<string>ImageViewer</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QScrollArea" name="scrollArea">
<property name="widgetResizable">
<bool>true</bool>
</property>
<widget class="QWidget" name="scrollAreaWidgetContents">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>985</width>
<height>476</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="image">
<property name="text">
<string/>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
30 changes: 15 additions & 15 deletions Edge Detector/kernel.cu
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "edge.cuh"
#include <chrono>

constexpr auto BLOCK_SIZE = 1024;
constexpr auto BLOCK_DIM = 32;
Expand All @@ -22,14 +23,12 @@ __global__ void changeBrightnessCUDA(uint8_t* input, const int width,
val = input[idx] + brightness;
// Truncate the result (0..255)
if (val > 255) {
input[idx] = 255;
val = 255;
}
else if (val < 0) {
input[idx] = 0;
}
else {
input[idx] = val;
val = 0;
}
input[idx] = val;
}
}

Expand Down Expand Up @@ -256,18 +255,15 @@ __host__ cudaError_t launchDetectEdge(uint8_t * input, uint8_t * bright, uint8_t
cudaStreamCreate(&mem);

// Choose which GPU to run on, change this on a multi-GPU system.
cudaEventRecord(start);
checkGpuError(cudaSetDevice(0));

checkGpuError(cudaMalloc((void**)&dev_input, imageSize));

checkGpuError(cudaMalloc((void**)&dev_edge, imageSize));

checkGpuError(cudaMemcpy(dev_input, input, imageSize, cudaMemcpyHostToDevice));
cudaEventRecord(stop);
auto startT = std::chrono::high_resolution_clock::now();
cudaMalloc((void**)&dev_input, imageSize);
cudaMalloc((void**)&dev_edge, imageSize);
cudaMemcpy(dev_input, input, imageSize, cudaMemcpyHostToDevice);
auto endT = std::chrono::high_resolution_clock::now();

cudaEventElapsedTime(mem_ms, start, stop);
printf("Memory Launch = %f\n", *mem_ms);
std::chrono::duration<double, std::milli> float_ms = endT - startT;
*mem_ms = float_ms.count();

cudaEventRecord(start);
changeBrightnessCUDA <<<grid, block, 0, main>>> (dev_input, width, height, brightness);
Expand All @@ -293,5 +289,9 @@ __host__ cudaError_t launchDetectEdge(uint8_t * input, uint8_t * bright, uint8_t
Error:
cudaFree(dev_input);
cudaFree(dev_edge);
cudaEventDestroy(start);
cudaEventDestroy(stop);
cudaStreamDestroy(main);
cudaStreamDestroy(mem);
return cudaStatus;
}
18 changes: 17 additions & 1 deletion Edge Detector/panel.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#include "panel.h"
#include <QFileDialog>
#include <QMessageBox>
#include "imageviewer.h"
#include <QCloseEvent>

#define FMT_HEADER_ONLY
#include <fmt/core.h>
#include <time.h>

void setLabelPictureScaled(QLabel*, QPixmap);

Expand Down Expand Up @@ -41,7 +44,9 @@ void Panel::handleOpen() {

void Panel::showEdgesMaximized() {
if (isProcessed) {
detector.showMaximized();
ImageViewer* iv = new ImageViewer(this);
iv->setImage(detector.getEdgePix());
iv->show();
}
}

Expand Down Expand Up @@ -71,6 +76,12 @@ void Panel::handleClose() {
ui.settingBox->setEnabled(false);
}

void Panel::closeEvent(QCloseEvent* event)
{
handleClose();
QWidget::closeEvent(event);
}

void Panel::handleSave() {
if (!isProcessed) {
QMessageBox msgBox;
Expand Down Expand Up @@ -181,6 +192,11 @@ void Panel::compute() {
// set brightness image
setLabelPictureScaled(ui.modified, detector.getBrightnessPix());
setLabelPictureScaled(ui.edges, detector.getEdgePix());

// Performance Results
ui.brightness->setText(QString::fromStdString(fmt::format("Brightness: {:.2f}ms", stat.brightnessTime)));
ui.edgeDetection->setText(QString::fromStdString(fmt::format("Edge Detection: {:.2f}ms", stat.edgeTime)));
ui.memAlloc->setText(QString::fromStdString(fmt::format("Memory Time: {:.2f}ms", stat.mallocTime)));
}
ui.statusBar->clearMessage();
}
Expand Down
3 changes: 2 additions & 1 deletion Edge Detector/panel.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <QMainWindow>
#include "ui_panel.h"
#include "EdgeDetector.h"

#include <QCloseEvent>

class Panel : public QMainWindow
{
Expand All @@ -12,6 +12,7 @@ class Panel : public QMainWindow
public:
Panel(QWidget *parent = nullptr);
~Panel();
void closeEvent(QCloseEvent* event);

public slots:
void handleOpen();
Expand Down
2 changes: 1 addition & 1 deletion Edge Detector/panel.ui
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
<item>
<widget class="QLabel" name="modified">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
Expand Down

0 comments on commit 58c32da

Please sign in to comment.