访问负像素值OpenCV

时间:2011-06-09 11:29:50

标签: c++ opencv

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] << " "; 
}

这段代码给出了不同的像素值,我在matlab中得到的是积极的,开放的cv给出了负值。

2 个答案:

答案 0 :(得分:1)

您可能正在将带符号的字节值转换为int,这可以给出负值。试试这段代码:

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

答案 1 :(得分:0)

使用 uchar 为我工作

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