-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cu
105 lines (91 loc) · 3.5 KB
/
main.cu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* MIT License
*
* Copyright (c) 2019 - Folke Vesterlund
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include "medianFilter.cuh"
#include "utils.hpp"
#include "timer.h"
int main(int argc,char **argv){
std::string fileName;
size_t numPixels, numRows, numCols;
if (argc < 2){
std::cout << "Usage: "<< argv[0] << " <image file>" << std::endl;
return(-1);
}
fileName = argv[1];
// Read image
cv::Mat image;
image = cv::imread(fileName, CV_LOAD_IMAGE_GRAYSCALE);
if(!image.data){
std::cerr << "Couldn't open file" << std::endl;
return(-1);
}
if(!image.isContinuous()){
std::cerr << "Image is not allocated with continuous data. Exiting..." << std::endl;
return(-1);
}
numCols = image.cols;
numRows = image.rows;
numPixels = numRows*numCols;
// Allocate GPU data
// Image needs to be padded to remove illegal memory accesses.
size_t nRows, nCols;
// Calculate padding size
util::getPadding(numCols, numRows, &nCols, &nRows);
size_t charPitch;
size_t intPitch;
unsigned char* d_binaryImg;
unsigned char* d_filteredImg;
unsigned int* d_temp;
unsigned int* d_temp2;
cudaMallocPitch(&d_binaryImg , &charPitch, nCols * sizeof(char), nRows);
cudaMallocPitch(&d_temp , &intPitch , nCols * sizeof(int) , nRows);
cudaMallocPitch(&d_temp2 , &intPitch , nCols * sizeof(int) , nRows);
// Final image can be stored in managed memory for easier post processing
cudaMallocManaged(&d_filteredImg, numPixels * sizeof(char));
// Pre process image
unsigned int imgMean = util::mean(image.data, numPixels);
util::threshold(image.data, image.data, imgMean, numPixels);
// Copy image to GPU
cudaMemcpy2D(d_binaryImg, charPitch, image.data , numCols*sizeof(char), numCols * sizeof(char), numRows, cudaMemcpyHostToDevice);
// Run and time kernel
GpuTimer timer;
timer.Start();
medianFilter(d_filteredImg, d_binaryImg, d_temp, d_temp2, numCols, numRows,charPitch, intPitch);
timer.Stop();
std::cout << "GPU code ran in: " << timer.Elapsed() << "ms" << std::endl;
// cudaDeviceSynchronize(); // Timer has syncronization built in
// No need to copy image back from GPU as it is handled by managed memory
// Plot result
cv::Mat finalImage(numRows, numCols, CV_8UC1, (void*)d_filteredImg);
cv::imshow("Labelled image", finalImage);
cv::waitKey();
// Free memory
cudaFree(d_binaryImg);
cudaFree(d_filteredImg);
cudaFree(d_temp);
cudaFree(d_temp2);
}