-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.cpp
81 lines (58 loc) · 2.35 KB
/
filter.cpp
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
//to compile: g++ -std=c++11 cpu-process.cpp -o test `pkg-config --cflags --libs opencv`
#include "process.hpp"
#include <ctime>
int main(int argc, char** argv )
{
if ( argc < 3 )
{
printf("usage: DisplayImage.out <Image_Path>\n");
return -1;
}
Mat image;
image = imread( argv[1], 1 );
if ( !image.data )
{
printf("No image data \n");
return -1;
}
Mat processedImage;
struct timespec start, finish;
double elapsed;
printf("-Starting Image Processor-\n");
// Start Timer
clock_gettime(CLOCK_MONOTONIC, &start);
if (strcmp(argv[2], "-l") == 0) {
// unsigned char* lineImageData = detectLine(image.data, image.rows, image.cols, image.channels(), image.step);
// processedImage = Mat(image.rows, image.cols, CV_8UC1, lineImageData);
} else if (strcmp(argv[2], "-g") == 0) {
unsigned char* grayImageData = grayscale(image.data, image.rows, image.cols, image.channels(), image.step);
processedImage = Mat(image.rows, image.cols, CV_8UC1, grayImageData);
} else if (strcmp(argv[2], "-b") == 0 && argc == 4 && atoi(argv[3]) >= 0) {
// unsigned char* blurImageData = blur(image.data, image.rows, image.cols, image.channels(), image.step, atoi(argv[3]));
// processedImage = Mat(image.rows, image.cols, CV_8UC3, blurImageData);
} else {
printf("Missing or wrong tag\n");
return 0;
}
clock_gettime(CLOCK_MONOTONIC, &finish);
elapsed = (finish.tv_sec - start.tv_sec);
elapsed += (finish.tv_nsec - start.tv_nsec) / 1000000000.0;
printf("Time: %f\n", elapsed);
namedWindow("Display Image", WINDOW_AUTOSIZE );
imshow("Display Image", processedImage);
waitKey(0);
return 0;
}
uchar* grayscale(uchar* image, int rows, int cols, int channels, int step) {
uchar* grayImage = (uchar*)malloc(sizeof(uchar)*rows*cols);
memset(grayImage, 0, sizeof(uchar)*rows*cols);
for(int y = 0; y < rows; y++) {
for (int x = 0; x < cols; x++) {
int blue = (int)image[channels*x + step*y];
int green = (int)image[channels*x + step*y + 1];
int red = (int)image[channels*x + step*y + 2];
grayImage[x + cols*y] = (uchar)(.3*red) + (.59 * green) + (.11 * blue);
}
}
return grayImage;
}