基于JavaCV的图像java模式检测
我正在尝试创建一个代码,它应该在最后识别图片中的一些图案/形状
当我试图在图片上绘制形状时遇到了一些麻烦(在本例中为“Output3”)。 这个项目似乎没有结束。我认为有一个带while函数的无限循环。 程序不显示输出3。有什么问题吗
输入图像:
输出2图像:
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);
}
我的目标是拍摄一张照片,并将其发送给程序,该程序应返回:
- 这是一个正方形
- 这是一个长方形
- 这是一个圆圈
- 这是一个六边形
# 1 楼答案
我不使用JavaCV,但我会这样做: