I have this code for contour detection and I also need to display the Freeman chain code, but for some reason it's not working and I don't really know how to fix it.
#include<opencv2/opencv.hpp>
#include <iostream>
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#define CV_READ_SEQ_ELEM(elem, reader)
using namespace std;
using namespace cv;
int main() {
// read the image
Mat image = imread("C:/Users/Admin/Downloads/img.jpg");
// convert the image to grayscale format
Mat img_gray;
cvtColor(image, img_gray, COLOR_BGR2GRAY);
// apply binary thresholding
Mat thresh;
threshold(img_gray, thresh, 150, 255, THRESH_BINARY);
imshow("Binary mage", thresh);
waitKey(0);
imwrite("image_thres1.jpg", thresh);
destroyAllWindows();
// detect the contours on the binary image using cv2.CHAIN_APPROX_NONE
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(thresh, contours, hierarchy, RETR_TREE, CHAIN_APPROX_NONE);
// draw contours on the original image
Mat image_copy = image.clone();
drawContours(image_copy, contours, -1, Scalar(0, 255, 0), 2);
imshow("None approximation", image_copy);
waitKey(0);
imwrite("contours_none_image1.jpg", image_copy);
//find chain code
vector<vector<Point> > contours;
findContours(image_copy, contours, RETR_EXTERNAL, CV_CHAIN_CODE);
cout << Mat(contours[0]) << endl;
findContours(image_copy, 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(&Mat(image_copy), 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);
}
}
destroyAllWindows();
}
These are the errors that I get when I try to run the code in Visual Studio:
Build started...
1>------ Build started: Project: Lab1_PI, Configuration: Debug x64 ------
1>chain_code.cpp
1>C:\Users\Admin\source\repos\Lab1_PI\Lab1_PI\chain_code.cpp(7,1): warning C4005: 'CV_READ_SEQ_ELEM': macro redefinition
1>C:\Users\Admin\Downloads\opencv\build\include\opencv2\core\types_c.h(1916): message : see previous definition of 'CV_READ_SEQ_ELEM'
1>C:\Users\Admin\source\repos\Lab1_PI\Lab1_PI\chain_code.cpp(39,25): error C2086: 'std::vector<std::vector<cv::Point,std::allocator<cv::Point>>,std::allocator<std::vector<cv::Point,std::allocator<cv::Point>>>> contours': redefinition
1>C:\Users\Admin\source\repos\Lab1_PI\Lab1_PI\chain_code.cpp(29): message : see declaration of 'contours'
1>C:\Users\Admin\source\repos\Lab1_PI\Lab1_PI\chain_code.cpp(51): error C2102: '&' requires l-value
1>Done building project "Lab1_PI.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
If anyone knows how to fix it, I would be very grateful.