views:

170

answers:

1

Hello,

I am attempting to perform a zero-crossing edge detection on an image in OpenCV. I blur and use the cvLaplace() then scale it from (0, max). My question is: How can I access the pixel values in that image in such a way as to correctly identify negative values? Using the function provided by OpenCV (cvPtr2D) returns unsigned chars. Any ideas or comments?

Thank you

+2  A: 

Pixels are stored internally as IPL_DEPTH_8U, which means 8-bit unsigned char, ranging from 0 to 255. But you could also pack them as IPL_DEPTH_16S (signed integer) and even IPL_DEPTH_32F (single precision floating point number).

cvConvertScale() probably will do the job! But if you want to convert it manually: http://stackoverflow.com/questions/882066/opencv-need-to-convert-ipl-depth-32s-to-ipl-depth-32f

The basic idea is to create a new image with cvCreateImage() and the format you need them to be, then use cvConvertScale() to copy the original data to new format. In the end, your code might look something like the following:

IplImage* img = cvLoadImage("file.png", CV_LOAD_IMAGE_ UNCHANGED);
// then retrieve size of loaded image to create the new one

IplImage* new_img = cvCreateImage(img_size, IPL_DEPTH_16S, 1);

cvConvertScale(img, new_img, 1/255.0, -128);

I think this answers the question of the thread.

Answering your comment, you could access the pixel information like this:

IplImage* pRGBImg = cvLoadImage(input_file.c_str(), CV_LOAD_IMAGE_UNCHANGED); 
int width = pRGBImg->width; 
int height = pRGBImg->height;
int bpp = pRGBImg->nChannels; 
for (int i=0; i < width*height*bpp; i+=bpp) 
{
  if (!(i % (width*bpp))) // print empty line for better readability
      std::cout << std::endl;

  std::cout << std::dec << "R:" << (int) pRGBImg->imageData[i] <<  
                          " G:" << (int) pRGBImg->imageData[i+1] <<  
                          " B:" << (int) pRGBImg->imageData[i+2] << " "; 
}

Dont forget to vote up and mark this answer as accepted in case it did.

karlphillip
Thank you for response! Any idea how I would access those pixels without using cvPtr2D()? I am attempting to access them manually using (float*)(new_img->imageData + x*new_img->widthStep + y), where x -> new_img->height and y -> width. But have gotten no successes.
bluth
I edited my answer to be able to add code and answer your comment. Dont forget to vote up if it helped you. =)
karlphillip