-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcuda_img_inv.cu
More file actions
71 lines (49 loc) · 1.65 KB
/
cuda_img_inv.cu
File metadata and controls
71 lines (49 loc) · 1.65 KB
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
#include <cuda_runtime.h>
#include <stdio.h>
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#define T 64
using namespace cv;
using namespace std;
__global__ void Inversion(unsigned char* image, unsigned char* image_inv, int size) {
int pixel = blockIdx.x*blockDim.x+threadIdx.x;
unsigned char mcolor = '255';
if ( pixel < size)
{
image_inv[pixel] = mcolor-image[pixel];
}
}
int main(int argc, char *argv[])
{
//scan the filename of image
string imgfile;
cout << "Input your image file : ";
getline (cin, imgfile);
Mat img = imread(imgfile,IMREAD_COLOR);
Size imgsize = img.size();
int width = imgsize.width;
int height = imgsize.height;
Mat img_invert(height,width,CV_8UC3,Scalar(0,0,0));
unsigned char* charImg = img.data;
unsigned char* newImg = img_invert.data;
int uCharSize = height*width*3*sizeof(unsigned char);
unsigned char *devImg,*devInv;
int vecSize = height*width*3;
int blocks = (vecSize+T-1)/T;
cudaMalloc((void**) &devImg, uCharSize);
cudaMalloc((void**) &devInv, uCharSize);
cudaMemcpy(devImg,charImg,uCharSize,cudaMemcpyHostToDevice);
cudaMemcpy(devInv,newImg,uCharSize,cudaMemcpyHostToDevice);
Inversion<<<blocks,T>>> (devImg,devInv,vecSize);
cudaMemcpy(charImg,devImg,uCharSize,cudaMemcpyDeviceToHost);
cudaMemcpy(newImg,devInv,uCharSize,cudaMemcpyDeviceToHost);
cudaFree(devImg);
cudaFree(devInv);
Mat output = Mat(height,width,CV_8UC3, newImg);
imshow("Your Image",img);
imshow("Inverted Image",output);
imwrite("output.jpg",output);
cvWaitKey(0);
}