Commit 5aeb8c49 authored by Gaurav Kukreja's avatar Gaurav Kukreja

Convolution: fixed error when using camera

Signed-off-by: Gaurav Kukreja's avatarGaurav Kukreja <gaurav@gauravk.in>
parent bf02d566
......@@ -29,9 +29,9 @@
using namespace std;
// uncomment to use the camera
//#define CAMERA
#define CAMERA
//#define USING_GPU
#define USING_GPU
template<typename T>
__device__ T gpu_min(T a, T b)
......@@ -190,30 +190,6 @@ int main(int argc, char **argv)
// allocate raw output array (the computation result will be stored in this array, then later converted to mOut for displaying)
float *imgOut = new float[(size_t)w*h*mOut.channels()];
// For camera mode: Make a loop to read in camera frames
#ifdef CAMERA
// Read a camera image frame every 30 milliseconds:
// cv::waitKey(30) waits 30 milliseconds for a keyboard input,
// returns a value <0 if no key is pressed during this time, returns immediately with a value >=0 if a key is pressed
while (cv::waitKey(30) < 0)
{
// Get camera image
camera >> mIn;
// convert to float representation (opencv loads image values as single bytes by default)
mIn.convertTo(mIn,CV_32F);
// convert range of each channel to [0,1] (opencv default is [0,255])
mIn /= 255.f;
#endif
// Init raw input image array
// opencv images are interleaved: rgb rgb rgb... (actually bgr bgr bgr...)
// But for CUDA it's better to work with layered images: rrr... ggg... bbb...
// So we will convert as necessary, using interleaved "cv::Mat" for loading/saving/displaying, and layered "float*" for CUDA computations
convert_mat_to_layered (imgIn, mIn);
int rad = ceil(3 * sigma); // kernel radius
int kw = 2 * rad; // kernel width
float c = 1. / (2. * 3.142857 * sigma * sigma); // constant
......@@ -226,13 +202,13 @@ int main(int argc, char **argv)
float b;
for (int iy = 0; iy < kw; iy++)
{
a = iy - rad;
for (int ix = 0; ix < kw; ix++)
{
b = ix - rad;
kernel[ix + (iy * kw)] = c * exp(-(a*a + b*b) / (2 * sigma*sigma));
}
}
a = iy - rad;
for (int ix = 0; ix < kw; ix++)
{
b = ix - rad;
kernel[ix + (iy * kw)] = c * exp(-(a*a + b*b) / (2 * sigma*sigma));
}
}
// Normalization of Kernel
float sum = 0.;
......@@ -240,19 +216,19 @@ int main(int argc, char **argv)
for (int iy = 0; iy < kw; iy++)
{
for (int ix = 0; ix < kw; ix++)
{
kmax = max(kmax, kernel[ix + (iy * kw)]);
sum += kernel[ix + (iy * kw)];
}
{
kmax = max(kmax, kernel[ix + (iy * kw)]);
sum += kernel[ix + (iy * kw)];
}
}
for (int iy = 0; iy < kw; iy++)
{
for (int ix = 0; ix < kw; ix++)
{
kernelOut[ix + (iy * kw)] = kernel[ix + (iy * kw)] / kmax;
kernel[ix + (iy * kw)] = kernel[ix + (iy * kw)] / sum;
}
{
for (int ix = 0; ix < kw; ix++)
{
kernelOut[ix + (iy * kw)] = kernel[ix + (iy * kw)] / kmax;
kernel[ix + (iy * kw)] = kernel[ix + (iy * kw)] / sum;
}
}
// Display Kernel
......@@ -260,6 +236,28 @@ int main(int argc, char **argv)
convert_layered_to_mat(cvKernelOut, kernelOut);
showImage("Kernel", cvKernelOut, 100, 10);
// For camera mode: Make a loop to read in camera frames
#ifdef CAMERA
// Read a camera image frame every 30 milliseconds:
// cv::waitKey(30) waits 30 milliseconds for a keyboard input,
// returns a value <0 if no key is pressed during this time, returns immediately with a value >=0 if a key is pressed
while (cv::waitKey(30) < 0)
{
// Get camera image
camera >> mIn;
// convert to float representation (opencv loads image values as single bytes by default)
mIn.convertTo(mIn,CV_32F);
// convert range of each channel to [0,1] (opencv default is [0,255])
mIn /= 255.f;
#endif
// Init raw input image array
// opencv images are interleaved: rgb rgb rgb... (actually bgr bgr bgr...)
// But for CUDA it's better to work with layered images: rrr... ggg... bbb...
// So we will convert as necessary, using interleaved "cv::Mat" for loading/saving/displaying, and layered "float*" for CUDA computations
convert_mat_to_layered (imgIn, mIn);
Timer timer;
float t;
// ###
......@@ -320,7 +318,7 @@ int main(int argc, char **argv)
{
for(int iy = 0; iy < h; iy++)
{
for(int iz = 0; iz < h; iz++)
for(int iz = 0; iz < nc; iz++)
{
int idx = ix + (iy * w) + (iz * w * h);
imgOut[idx] = 0; // initialize
......@@ -377,6 +375,7 @@ int main(int argc, char **argv)
delete[] imgIn;
delete[] imgOut;
delete[] kernel;
delete[] kernelOut;
// close all opencv windows
cvDestroyAllWindows();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment