There is a lot to say about this code sample:
First, you are using the old C-style API (
IplImage
pointers,cvBlah
functions, etc), which is obsolete and more difficult to maintain (in particular, memory leaks are introduced easily), so you should consider using the C++-style structures and functions (cv::Mat
structure andcv::blah
functions).Your error is probably coming from the instruction
cvCopy(dst,img);
at the very beginning. This fills your input image with nothing just before you start your processing, so you should remove this line.For maximum speed, you should invert the two loops, so that you first iterate over rows then over columns. This is because images in OpenCV are stored row-by-row in memory, hence accessing the images by increasing column is more efficient with respect to the cache usage.
The temporary variable
idxF
is never used, so you should probably remove the following line too:int idxF = row*dst->widthStep + dst->nChannels*col;
When you access image data to store the pixels in
temp_ptr1
andtemp_ptr2
, you swapped the positions of thex
andy
coordinates. You should access the image in the following way:temp_ptr1 = &((uchar*)(img->imageData + (img->widthStep*pt.y)))[pt.x*3];
You never release the memory allocated for
dst
, hence introducing a memory leak in your application. CallcvReleaseImage(&dst);
at the end of your function.