[英]Accessing negative pixel values 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] << " ";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.