简体   繁体   中英

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.

I have an RGB image, I read the grayscale version, equalize the histogram, then use canny to detect edges.

The question is: does the Canny function return a grayscale image or a binary one? 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. In a binary image must be only two values 0 or 1 (0 or 255)... How can I do that?

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 . How can I make this binary?

For the Canny function I use a threshold value obtaining the "mean" value of the grayscale image; I saw it here: 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:

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.

Documentation ( http://opencv.willowgarage.com/documentation/cpp/imgproc_feature_detection.html?highlight=canny#Canny ) says that:

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

Second parameter - edges – The output edge map. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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