简体   繁体   English

访问负像素值OpenCV

[英]Accessing negative pixel values OpenCV

I am attempting to perform a zero-crossing edge detection on an image in OpenCV. 我正在尝试在OpenCV中对图像执行零交叉边缘检测。 I blur and use the cvLaplace() then scale it from (0, max). 我模糊并使用cvLaplace()然后从(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. 使用OpenCV(cvPtr2D)提供的功能将返回未签名的字符。 Any ideas or comments? 有什么想法或意见吗?

Thank you 谢谢

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). 像素在内部存储为IPL_DEPTH_8U,表示8位无符号字符,范围从0到255。但是您也可以将它们打包为IPL_DEPTH_16S(有符号整数),甚至打包为IPL_DEPTH_32F(单精度浮点数)。

cvConvertScale() probably will do the job! cvConvertScale()可能会完成这项工作! But if you want to convert it manually: OpenCV need to convert IPL_DEPTH_32S to IPL_DEPTH_32F 但是,如果要手动转换: OpenCV需要将IPL_DEPTH_32S转换为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. 基本思想是使用cvCreateImage()及其所需的格式创建新图像,然后使用cvConvertScale()将原始数据复制为新格式。 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. 不要忘记投票并将此答案标记为接受,以防万一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM