Sunday, July 27, 2014

example using matchShapes in OpenCV

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <cmath>
#include <iostream>
#include <cstdio>
using namespace std;
using namespace cv;

int main(int argc, char*argv[])
{
     if(argc!=3)
    {
        cout<<"usage: ./ms main.png match.png"<<endl;
        return 1;
     }
    Mat src = imread(argv[1]);
    if (src.empty())
   {
      cout<<"figuire "<<argv[1]<< "is not located!"<<endl;
      return 1;
    }

    Mat match = imread(argv[2]);
     if (match.empty())
     {
          cout<<"figuire "<<argv[2]<< "is not located!"<<endl;
          return 1;
     }

      Mat srcGray;
      Mat matchGray;
      cvtColor(src, srcGray, CV_BGR2GRAY);
      cvtColor(match, matchGray, CV_BGR2GRAY);

      Mat src_th, match_th;
      threshold(srcGray, src_th,125, 255,THRESH_BINARY_INV);
      threshold(matchGray, match_th,125, 255,THRESH_BINARY_INV);

      vector<vector<Point> > src_contours;
      vector<Vec4i> src_hierarchy;
      findContours(src_th, src_contours, src_hierarchy,CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);


      vector<vector<Point> > match_contours;
      vector<Vec4i> match_hierarchy;
      findContours(match_th, match_contours, match_hierarchy,CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);

      double matchR;  

     for(int i=0;i<src_contours.size();++i)
    {
        matchR = matchShapes(approx, match_contours[1],CV_CONTOURS_MATCH_I1,0);
       cout<<"match resul of contour "<<i<<" is: "<<matchR<<endl;
     }
}

However, matchShapes function does not work very well.

We are using the 2nd figure to match any one in the 1st figure. Here is the result:

match resul of contour 0 is: 0.11592
match resul of contour 1 is: 0.151644
match resul of contour 2 is: 0.282304
match resul of contour 3 is: 0.390383
match resul of contour 4 is: 0.540419
match resul of contour 5 is: 0.757443

Saturday, July 26, 2014

different stages of using OpenCV

1: read in images.
Mat src = imread("and.png");

2: convert to a gray image.

 Mat gray;
cvtColor(src, gray, CV_BGR2GRAY);
3: find edges. There are two ways: threshold and canny. This step is optional. If the picture is only black and white, then this step is not necessary. We can just use findContours.
Mat th; Mat bw;
//method 1:  threshold(gray, th,125, 255,THRESH_BINARY);

//method 2:  Canny(gray, bw, 100, 200); 


       Note the differences between the above two images.
4: find contours and draw contours.
4.1:  findContours(th, contours, hierarchy,CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);
 Note the red rectangle. If we change :
threshold(gray, th,125, 255,THRESH_BINARY);
to
threshold(gray, th,125, 255,THRESH_BINARY_INV);
You will not see that red  rectangle.

   4.2: findContours(bw, contours2, hierarchy2,CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);
Why do we get 4 contours instead of 2 contours since we only two shapes in the canny output?
Because Canny creates two contours for each edge, one outside and one inside of that edge.


4: write out the image.

imwrite("output.png", color);