访问负像素值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给出的是负的。

您可能正在将带符号的字节值转换为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] << " "; 

使用uchar对我的有效

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

相关内容

  • 没有找到相关文章

最新更新