简体   繁体   English

OpenCV二进制图像仅获得白色像素不起作用

[英]OpenCV binary image get only white pixels don't work

I'm doing some image processing to learn, I'm using openCV with the new C++ iterface. 我正在做一些图像处理学习,我正在将openCV与新的C ++ iterface一起使用。

I have an RGB image, I read the grayscale version, equalize the histogram, then use canny to detect edges. 我有一个RGB图像,我读取了灰度版本,均衡了直方图,然后使用canny来检测边缘。

The question is: does the Canny function return a grayscale image or a binary one? 问题是:Canny函数返回的是灰度图像还是二进制图像? I use a double for loop to check the values of pixels in the image resulting of applying canny edge detector, and I have a lot of values totally different. 我使用double for循环来检查由于应用Canny边缘检测器而导致的图像像素值,并且我有很多完全不同的值。 In a binary image must be only two values 0 or 1 (0 or 255)... How can I do that? 在二进制图像中,必须只有两个值0或1(0或255)...我该怎么做?

My code: 我的代码:

imagen = imread(nombre_imagen,0); //lee la imagen en escala de grises
equalizeHist(imagen,imagen);
Canny(binary,binary,0.66*threshold,1.33*threshold,3,true);
for(int i=0;i<binary.rows;i++){
    for(int j=0;j<binary.cols;j++){
       std::cout << binary.at<float>(i,j)<< ",";
    }
    std::cout << "\n";
}

This is a 183x183 matrix and I can see a lot of values even some nan . 这是一个183x183矩阵,我什至可以看到很多数值,甚至是nan How can I make this binary? 如何制作此二进制文件?

For the Canny function I use a threshold value obtaining the "mean" value of the grayscale image; 对于Canny函数,我使用一个阈值来获取灰度图像的“平均值”值; I saw it here: http://www.kerrywong.com/2009/05/07/canny-edge-detection-auto-thresholding/ . 我在这里看到了它: http : //www.kerrywong.com/2009/05/07/canny-edge-detection-auto-thresholding/

Binary images have type uchar which you need to cast to int (or unsigned int ) if you want the 0-255 range: 二进制图像的类型为uchar ,如果需要0-255的范围,则需要将其uchar转换为int (或unsigned int ):

for(int i=0;i<edges.rows;i++) {
    for(int j=0;j<edges.cols;j++) {
       std::cout << (int)edges.at<uchar>(i,j)<< ",";
    }
    std::cout << "\n";
}

You should avoid the C-style cast, but I'll leave that as an exercise to the reader. 您应该避免使用C样式的强制转换,但我将其留给读者练习。

Documentation ( http://opencv.willowgarage.com/documentation/cpp/imgproc_feature_detection.html?highlight=canny#Canny ) says that: 说明文件( http://opencv.willowgarage.com/documentation/cpp/imgproc_feature_detection.html?highlight=canny#Canny )表示:

void Canny(const Mat& image, Mat& edges, double threshold1, double threshold2, int apertureSize=3, bool L2gradient=false) 无效Canny(const Mat&image,Mat&edge,double threshold1,double threshold2,int apertureSize = 3,bool L2gradient = false)

Second parameter - edges – The output edge map. 第二个参数-edge –输出边缘图。 It will have the same size and the same type as image 它将具有与图像相同的尺寸和类型

In your call you pass the same source image as the second parameter, which causes, I think, strange results. 在您的通话中,您传递了与第二个参数相同的源图像,这会导致奇怪的结果。

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

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