Ask Your Question
0

Image difference

asked 2013-12-16 01:38:34 -0600

Nihad gravatar image

I tried to calculate difference between two images (Canny image and outer contour), but it gave black image. How can I show original difference between two images? Thanks in advance. Here is my code:

int main (...)
{
Mat image = imread("test0.png",CV_LOAD_IMAGE_GRAYSCALE);
 Mat canny_output;  
 vector<vector<cv::Point> > contours;
 vector<Vec4i> hierarchy;

 Canny(image, canny_output, 50, 200);

 findContours (image,  contours ,  hierarchy ,  cv :: RETR_EXTERNAL ,  cv :: CHAIN_APPROX_SIMPLE );

 Mat contour_image = Mat::zeros( image.size(), CV_8UC1);
for(int k= 0; k < contours.size(); k++)
{
    for(int l= 0; l < contours[k].size();l++)
    {
      contour_image.at<uchar>(contours[k][l])=255; 
    }
}
  Mat difference_image= Mat::zeros( image.size(), CV_8UC1);;
  difference_image=canny_output-contour_image;

  imshow("Difference_image", difference_image); //Produce black image
  cv::waitKey(0);
}
edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
1

answered 2013-12-17 11:01:07 -0600

You don't need to create zero-filled Mats to store stuff, this is not MatLab, you should make use of openCV's advantages! First, to assign values to a matrix you should use the setTo method.

Mat A, B;
A = stuff;
B = other stuff;

A.setTo(255, B);

This code sets in A all non-zero elements in B to 255.

To get difference between Mat use the absdiff function.

Mat A, B, diff;

A = stuff;
B = other stuff;
absdiff(A, B, diff);

Just make sure A and B are of the same type.

edit flag offensive delete link more
0

answered 2013-12-17 23:04:06 -0600

Nihad gravatar image

I change the code following way, but it gave only outer contour output. I think output should be inner contour because I subtract contour_image from canny_output.

int main (...)
{
Mat image = imread("test0.png",CV_LOAD_IMAGE_GRAYSCALE);
 Mat canny_output= Mat::zeros( image.size(), CV_8UC1);  
 vector<vector<cv::Point> > contours;
 vector<Vec4i> hierarchy;

 Canny(image, canny_output, 50, 200);

 findContours (image,  contours ,  hierarchy ,  cv :: RETR_EXTERNAL ,  cv :: CHAIN_APPROX_SIMPLE );

 Mat contour_image = Mat::zeros( image.size(), CV_8UC1);
for(int k= 0; k < contours.size(); k++)
{
    for(int l= 0; l < contours[k].size();l++)
    {
      contour_image.at<uchar>(contours[k][l])=255; 
    }
}
  Mat difference_image;
 absdiff( canny_output,contour_image, difference_image);

  imshow("Difference_image", difference_image); //Produce output same as contour_image
  cv::waitKey(0);
}
edit flag offensive delete link more

Comments

I have no idea of what result you are looking for, absdiff function computes the absolute difference between 2 Mats. http://docs.opencv.org/modules/core/doc/operations_on_arrays.html#absdiff

Pedro Batista gravatar imagePedro Batista ( 2013-12-18 04:27:21 -0600 )edit

Question Tools

Stats

Asked: 2013-12-16 01:38:34 -0600

Seen: 5,949 times

Last updated: Dec 17 '13