Ask Your Question

Revision history [back]

you should use cv::threshold instead of cv::Canny

take a look at my update on your code

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace cv;

int main(int argc, char** argv)
{
    int lowThreshold = 40;
    int max_lowThreshold = 255;

    Mat src1 = imread("img1.jpg");
    Mat src2 = imread("img2.jpg");
    Mat src_gray1;
    Mat src_gray2;
    cvtColor(src1, src_gray1, CV_BGR2GRAY);
    cvtColor(src2, src_gray2, CV_BGR2GRAY);
    Mat edge1;
    Mat edge2;
    Mat dst1;
    Mat dst2;
    Mat detected_edges1;
    Mat detected_edges2;
    std::vector<std::vector<Point> > contours1, contours2;
    std::vector<Vec4i> hierarchy1, hierarchy2;
    edge1.create(src1.size(), src1.type());
    edge2.create(src2.size(), src2.type());
    dst1.create(src1.size(), src1.type());
    dst2.create(src2.size(), src2.type());
    blur(src_gray1, detected_edges1, Size(3,3));
    blur(src_gray2, detected_edges2, Size(3,3));

    threshold(detected_edges1, detected_edges1, lowThreshold, lowThreshold, THRESH_BINARY );
    threshold(detected_edges2, detected_edges2, lowThreshold, lowThreshold, THRESH_BINARY );

    findContours(detected_edges1, contours1, hierarchy1, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
    findContours(detected_edges2, contours2, hierarchy2, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
    drawContours(edge1, contours1, 0, Scalar(0, 0, 255), CV_FILLED);
    drawContours(edge2, contours2, 0, Scalar(0, 0, 255), CV_FILLED);
    imwrite("result1.jpg", edge1);
    imwrite("result2.jpg", edge2);
    return 0;
}