Question

    cvCvtColor(img,dst,CV_RGB2YCrCb);
    for  (int col=0;col<dst->width;col++) 
    { 
        for (int row=0;row<dst->height;row++) 
        { 
            int idxF = row*dst->widthStep + dst->nChannels*col; // Read the image data 
            CvPoint pt = {row,col};
            temp_ptr2[0] += temp_ptr1[0]* 0.0722 + temp_ptr1[1] * 0.7152 +temp_ptr1[2] *0.2126  ;   // channel Y 
        }
    }

But the result is this:

enter image description here

Please assist where am i going wrong?

Was it helpful?

Solution

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 and cv::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 and temp_ptr2, you swapped the positions of the x and y 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. Call cvReleaseImage(&dst); at the end of your function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top