-
Notifications
You must be signed in to change notification settings - Fork 9
/
helpers.cpp
executable file
·108 lines (95 loc) · 3.05 KB
/
helpers.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
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
#include "helpers.h"
double read_timer()
{
struct timeval start;
gettimeofday( &start, NULL );
return (double)((start.tv_sec) + 1.0e-6 * (start.tv_usec)); //in seconds
}
// designed for use with UW Madison raw image data
// http://pages.cs.wisc.edu/~dyer/cs534-fall11/hw-toc.html
cv::Mat getRawImage(string in_filename)
{
cv::Mat img = cv::imread(in_filename);
cv::cvtColor(img, img, CV_RGB2GRAY);
int dim = min(img.rows, img.cols);
img = img.colRange(0, dim).rowRange(0, dim); //make the image square (cut off bottom right if necessary)
//printf("in getRawImage(). rows = %d, cols = %d \n", img.rows, img.cols);
return img;
}
void outputProcessedImage(unsigned int* processedImg, int width, int height, string out_filename)
{
cv::Mat img = cv::Mat::zeros(height, width, CV_8UC1);
for(int i=0; i<height; i++)
{
for(int j=0; j<width; j++)
{
uchar* px = (uchar*)&processedImg[i*width + j];
img.at<uchar>(i,j) = px[0]; //just grab the 1st of the 4 pixel spaces in a uchar4
}
}
vector<int> compression_params;
compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
compression_params.push_back(9);
cv::imwrite(out_filename, img, compression_params);
}
void outputProcessedImageFloat(float* processedImg, int width, int height, string out_filename)
{
cv::Mat img = cv::Mat::zeros(height, width, CV_8UC1);
//cv::Mat img = cv::Mat::zeros(height, width, CV_32FC1);
for(int i=0; i<height; i++)
{
for(int j=0; j<width; j++)
{
img.at<uchar>(i,j) = (uchar)processedImg[i*width + j];
}
}
vector<int> compression_params;
compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
compression_params.push_back(9);
cv::imwrite(out_filename, img, compression_params);
}
void outputProcessedImageUchar(uchar* processedImg, int width, int height, string out_filename)
{
cv::Mat img = cv::Mat::zeros(height, width, CV_8UC1);
for(int i=0; i<height; i++)
{
for(int j=0; j<width; j++)
{
img.at<uchar>(i,j) = processedImg[i*width + j];
}
}
vector<int> compression_params;
compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
compression_params.push_back(9);
cv::imwrite(out_filename, img, compression_params);
}
float* getDummyImg(int height, int width)
{
float* img = (float*)malloc(sizeof(float)*width*height);
for(int i=0; i<height; i++)
{
for(int j=0; j<width; j++)
{
img[i*width + j] = (i+j)%256; //arbitrary
}
}
return img;
}
uchar* getDummyImgUchar(int height, int width)
{
uchar* img = (uchar*)malloc(sizeof(uchar)*width*height);
for(int i=0; i<height; i++)
{
for(int j=0; j<width; j++)
{
img[i*width + j] = (i+j)%256; //arbitrary
}
}
return img;
}
float getResponseTime(cudaEvent_t start, cudaEvent_t stop)
{
float responseTime;
cudaEventElapsedTime(&responseTime, start, stop);
return responseTime/1000; //convert ms to seconds
}