Ask Your Question

Chamfer Matching

asked 2015-01-06 14:56:19 -0600

fortes_23 gravatar image

updated 2018-10-04 23:42:27 -0600

I'm new using OpenCV. I have started a project for recognitizing animals and for starting with the library I'm trying to recognize shapes. For this, I found this method of Chamfer Matching:

" 1. You do an edge detection (for example, cvCanny in opencv) 2. You create a distance image, where the value of each pixel means the distance frome the nearest edge. 3. You take the shapes you would like to detect, define sample points along the edges of the shape, and try to match these points on the distance image. Basically you just add the values on the distance image which are "under" the coordinates of your objects. 4. Find a good minimization algorithm, the effectiveness of this depends on your application."

Right now, I have this program:

int main() {

//Open the image
Mat image = imread("root");

//Open the shape I want to detect
Mat templ = imread("root");

//There could be a test to check that the images have loaded

//Inicialize the contours image and the gray one
Mat contours;
Mat gray_image;

//Obtain gray image
cvtColor(image, gray_image, CV_RGB2GRAY);

// Reduce the noise with kernel a 3x3 before the canny
blur(image, image, cv::Size(3, 3));
blur(templ, templ, cv::Size(3, 3));

//Canny edge
Canny(image, contours, 10, 350, 3);
Canny(templ, templ, 5, 350, 3);

// Create a new image, for the distance to the nearest edge 
Mat img(image.rows, image.cols, CV_8UC3);

//Change the colour Black->White and White->Black of the canny edge
for (int i = 0; i < contours.rows; i++) {
    for (int j = 0; j < contours.cols; j++) {<Vec3b>(i, j)[0] = 255 -<Vec3b>(i, j)[0];<Vec3b>(i, j)[1] = 255 -<Vec3b>(i, j)[1];<Vec3b>(i, j)[2] = 255 -<Vec3b>(i, j)[2];
        //cout <<<cv::Vec3b>(i, j) << endl;

//Calculates the distance to the closest zero pixel for each pixel of the source image
distanceTransform(contours, img, CV_DIST_L2, 3);

//Normalizes the norm or value range of an array
normalize(img, img, 0.0, 1.0, CV_MINMAX);
 * If we want to see the pixels' values
    for (int i = 0; i < contours.rows; i++) {
        for (int j = 0; j < contours.cols; j++) {
            cout <<<cv::Vec3b>(i, j) << endl;

// Show the images
imshow("Image", image);
imshow("Gray", gray_image);
imshow("canny2", contours);
imshow("pixel", img);

while (waitKey(33) != 27) {


I have done the first two steps, but I don't understand the third one well. What should I do now? I have tried to use the void chamerMatching(), but I always obtain the error "double free or corruption (!prev)". Could anyone help me?


edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2015-04-21 01:30:32 -0600

sam123 gravatar image

to solve the problem with the chamerMatching() function, you can remove the following line in chamfermatching.h

delete templates[i];

however, I'm not sure if you get memory leaks, but it works.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2015-01-06 14:56:19 -0600

Seen: 6,750 times

Last updated: Jan 07 '15