Ask Your Question
0

Freeman chain code in OpenCV 3

asked 2017-03-29 11:05:42 -0600

szecsit gravatar image

updated 2017-03-31 09:55:38 -0600

pklab gravatar image

In OpenCV 1 there used to be an option of cvFindContours (using CV_CHAIN_CODE) to obtain a contour from an image in Freeman chain code. However, I cannot find it in OpenCV 3. Has it been removed as option? I know I could develop my own function to generate Freeman chain code from a contour given as a sequence of points, but I thought there was a ready-made function already available.

edit retag flag offensive close merge delete

Comments

In opencv 3.2 doc CHAIN_APPROX_SIMPLE is very similar to freeman code (direction is equal to end-begin and number of code is equal to difference?).

LBerger gravatar imageLBerger ( 2017-03-29 11:11:08 -0600 )edit

CHAIN_APPROX_SIMPLE does not give directional codes; it gives you characteristic points sufficient to describe elements in the contour. The Freeman code gives directional codes from one point to the next, including all points in the contour. I still use the Freeman code; based on it I have developed a differential chain code and the two together are a good description of the general shape of the contour, that is why I am still using it.

szecsit gravatar imageszecsit ( 2017-03-30 03:23:19 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
1

answered 2017-03-31 09:51:32 -0600

pklab gravatar image

Since OpenCV 3.2.0 source image is not modified by findContour therefore the chain code is no more returned. Unfortunately this information is in the master doc but not in 3.2.0 doc.

See news: findcontours doesn't alter the input image and and this comment on issue #8225

edit flag offensive delete link more

Comments

It is true that since 3.2.0 findContours() does not modify the source image, but it could still return Freeman chain code as opposed to point lists. Not sure why they have removed the Freeman chain code, perhaps its usage is now limited.

szecsit gravatar imageszecsit ( 2017-04-04 09:47:26 -0600 )edit
0

answered 2017-03-29 13:12:44 -0600

just tested an answer on SO

it seems calling findContours with CV_CHAIN_CODE returns useless data and old C API still useful.

#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include <iostream>

using namespace std;
using namespace cv;

int main() {

    cv::Mat img = cv::Mat::zeros(100, 100, CV_8UC1);
    cv::line(img, Point(50,50), Point(60,60), cv::Scalar(255), 1);
    cv::line(img, Point(40, 60), Point(60, 60), cv::Scalar(255), 1);
    cv::line(img, Point(40, 60), Point(50, 50), cv::Scalar(255), 1);

    imshow("Test", img);

    vector<vector<Point> > contours;

    findContours(img, contours, RETR_EXTERNAL, CV_CHAIN_CODE);
    cout << Mat(contours[0]) << endl;

    findContours(img, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
    cout << "CHAIN_APPROX_SIMPLE" << endl;
    cout << Mat(contours[0]) << endl;

    CvChain* chain = 0;
    CvMemStorage* storage = 0;
    storage = cvCreateMemStorage(0);
    cvFindContours(&IplImage(img), storage, (CvSeq**)(&chain), sizeof(*chain), CV_RETR_EXTERNAL, CV_CHAIN_CODE);

    for (; chain != NULL; chain = (CvChain*)chain->h_next)
    {
        //chain=(CvChain*)chain ->h_next; 
        //if(chain==NULL){break;}
        CvSeqReader reader;
        int i, total = chain->total;
        cvStartReadSeq((CvSeq*)chain, &reader, 0);
        printf("--------------------chain\n");

        for (i = 0; i<total; i++)
        {
            char code;
            CV_READ_SEQ_ELEM(code, reader);
            printf("%d", code);
        }
    }

    waitKey();

    return 0;
}
edit flag offensive delete link more

Comments

Yes, the old C API is still working, I was just wondering why it disappeared from version 3. Thanks anyway.

szecsit gravatar imageszecsit ( 2017-03-30 03:19:16 -0600 )edit

Hi, I used the same code as of above for my vertices data, but failed to get the freeman chain code.Please help me.

My dataset is as follows:

0 0
0 0.00144928
0.00144928 0.00144928
0.00289855 0.00144928
0.00724638 0.00434783
0.015942 0.00434783
0.0275362 0.00434783
0.0434783 0.00434783
0.0637681 0.00434783
0.0782609 0.00434783
0.0869565 0.00434783
0.102899 0.0057971
0.111594 0.0057971
0.126087 0.0057971
0.136232 0.0057971
0.15942 0.0057971
0.172464 0.0057971
0.182609 0.00434783
0.198551 0.00434783
0.214493 0.00434783
0.23913 0.00434783
0.250725 0.00434783
0.269565 0.00434783
0.284058 0.00434783
0.305797 0.00434783
0.331884 0.00434783
0.344928 0.00434783
0.371014 0.0057971
0
Programming_Enthusiast gravatar imageProgramming_Enthusiast ( 2018-10-29 01:16:17 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2017-03-29 11:05:42 -0600

Seen: 3,779 times

Last updated: Mar 31 '17