I am trying to create code that should recognize some pattern / figure in the picture at the end.
I had problems when I tried to draw a figure in fig. ("Output3" in this case). The program does not seem to end. I think there is an infinite loop with a while function. The program does not display output 3. What is the problem?
Image INPUT:

Image OUTPUT2:

public class Hello {
public static void main(String[] args) throws IOException {
CvMemStorage memory = CvMemStorage.create();
CvSeq contours = new CvSeq(null);
IplImage image = cvLoadImage("contour.jpg", CV_LOAD_IMAGE_GRAYSCALE);
CanvasFrame canvas = new CanvasFrame("Output", 1);
CanvasFrame canvas2 = new CanvasFrame("Output2", 1);
CanvasFrame canvas3 = new CanvasFrame("Output3", 1);
canvas.showImage(image);
cvSmooth(image, image, CV_BLUR, 9 , 9, 2, 2);
cvThreshold(image, image, 155, 255, CV_THRESH_BINARY);
cvCanny(image, image, 20*7*7, 40*7*7, 7);
cvDilate(image, image, null, 1);
canvas2.showImage(image);
cvSaveImage("output2.jpg", image);
cvFindContours(image, memory, contours, Loader.sizeof(CvContour.class), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));
while(contours != null && !contours.isNull()) {
if(contours.elem_size() > 0) {
CvSeq approx = cvApproxPoly(contours, Loader.sizeof(CvContour.class), memory, CV_POLY_APPROX_DP, (int) cvContourPerimeter(contours)*0.02, 0);
cvDrawContours(image, approx, CvScalar.BLUE, CvScalar.BLUE, -1,1, CV_AA);
}
contours.h_next();
}
canvas3.showImage(image);
}
My goal is to take a picture, send it to the program, which should return:
- It's a square
- It's a rectangle
- It's a circle
- It's a hexagon