Accessing negative pixel values OpenCV

后端 未结 1 645
忘了有多久
忘了有多久 2021-01-15 17:15

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

相关标签:
1条回答
  • 2021-01-15 17:48

    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: 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.

    0 讨论(0)
提交回复
热议问题