简体   繁体   English

使用JavaCV在图像中检测图案

[英]Pattern detection in an image with JavaCV

I'm trying to create a code which should at the end recognize some pattern/shape in a picture. 我正在尝试创建一个代码,最终应该识别图片中的某些图案/形状。

I've had some trouble when I tried to draw the shape on the pic ("Output3" in this case). 当我试图在图片上绘制形状时我遇到了一些麻烦(在这种情况下为“Output3”)。 The program seems not to end. 该计划似乎没有结束。 I think there is a infinite loop with the while function. 我认为while函数有一个无限循环。 The program doesn't display the output3. 程序不显示output3。 What's the issue? 有什么问题?

INPUT image: INPUT图片:

在此输入图像描述

OUTPUT2 image: OUTPUT2图片:

在此输入图像描述


public class Hello {

/**
 * @param args
 * @throws IOException 
 */

public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub

    // Memory storage
    CvMemStorage memory = CvMemStorage.create();
    CvSeq contours = new CvSeq(null);

    // Display original contour image .png, then GRAYSCALE and display in CanvasFrame
    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);

    // thresholding
    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);

    // finding contours
    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 photo, send it to the program which should return: 我的目标是拍照,将其发送到应返回的程序:

  • This is a square 这是一个正方形
  • This is a rectangle 这是一个矩形
  • This is a circle 这是一个圆圈
  • This is an hexagon 这是一个六边形

I don't use JavaCV, but here is what I would do: 我不使用JavaCV,但这是我要做的:

  1. Conversion to gray level, or even better binary image (because the background is white) 转换为灰度级,甚至更好的二进制图像(因为背景为白色)
  2. Connected component labeling in order to separate each shape 连接组件标签以分离每个形状
  3. Use shape index to classify the shapes. 使用形状索引对形状进行分类。 During my PhD I used shape indexes to distinguish rectangles, disks, triangles and parallelograms. 在我的博士学位期间,我使用形状索引来区分矩形,圆盘,三角形和平行四边形。 You just have to had some measures to differentiate squares from rectangles, or disks from hexagons. 您只需要采取一些措施来区分正方形与矩形,或磁盘与六边形。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM