简体   繁体   English

如何使用javacv识别长度和宽度可变的方形或矩形?

[英]How to identify square or rectangle with variable lengths and width by using javacv?

I'm developing project using java to identify components using opencv package but I'm new to javacv and I just want to know how to identify rectangles in a particular source image please can some experience person give some basic guide line to archive this task. 我正在使用java开发项目来识别使用opencv包的组件,但我是javacv的新手,我只是想知道如何识别特定源图像中的矩形,请一些经验人员给出一些基本的指导来存档这个任务。 I try to use template matching on here but it can identify exact size rectangle only. 我尝试在这里使用模板匹配,但它只能识别确切大小的矩形。 But In my case I need to identify variable length rectangle ? 但在我的情况下,我需要识别变长矩形?

import java.util.Arrays;
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
public class TestingTemplate {
public static void main(String[] args) {
//Original Image
IplImage src = cvLoadImage("src\\lena.jpg",0);
//Template Image
IplImage tmp = cvLoadImage("src\\those_eyes.jpg",0);
//The Correlation Image Result
IplImage result = cvCreateImage(cvSize(src.width()-tmp.width()+1, src.height()-tmp.height()+1), IPL_DEPTH_32F, 1);
//Init our new Image
cvZero(result);
cvMatchTemplate(src, tmp, result, CV_TM_CCORR_NORMED);

double[] min_val = new double[2];
double[] max_val = new double[2];

//Where are located our max and min correlation points
CvPoint minLoc = new CvPoint();
CvPoint maxLoc = new CvPoint();
cvMinMaxLoc(result, min_val, max_val, minLoc, maxLoc, null); //the las null it's for
 optional mask mat()

System.out.println(Arrays.toString(min_val)); //Min Score
System.out.println(Arrays.toString(max_val)); //Max Score

CvPoint point = new CvPoint();
point.x(maxLoc.x()+tmp.width());
point.y(maxLoc.y()+tmp.height());
cvRectangle(src, maxLoc, point, CvScalar.WHITE, 2, 8, 0); //Draw the rectangule result in original img.
cvShowImage("Lena Image", src);
cvWaitKey(0);
//Release
cvReleaseImage(src);
cvReleaseImage(tmp);
cvReleaseImage(result);
}
}

Please can some one help to accomplish this 请有人帮助完成此任务

(So it is fixed as square.) (因此它被固定为方形。)

For square detection, OpenCV comes with some samples for this. 对于方形检测,OpenCV附带了一些样本。 Codes are in C++, C, Python. 代码使用C ++,C,Python。 Hope you can port this to JavaCV. 希望你能把它移植到JavaCV。

C++ code , Python Code . C ++代码Python代码

I will just illustrate how it works: 我将只说明它是如何工作的:

1 - First you split the image to R,G,B planes. 1 - 首先将图像分割为R,G,B平面。

2 - Then for each plane perform edge detection , and in addition to that, threshold for different values like 50, 100, .... etc. 2 - 然后为每个平面执行边缘检测 ,除此之外,还有不同值的值,如50,100,......等。

3 - And in all these binary images, find contours ( remember it is processing a lot of images, so may be a little bit slow, if you don't want, you can remove some threshold values). 3 - 在所有这些二进制图像中, 找到轮廓 (记住它正在处理大量图像,所以可能有点慢,如果你不想要,你可以删除一些阈值)。

4 - After finding contours, remove some small unwanted noises by filtering according to area . 4 - 找到轮廓后, 根据区域过滤去除一些小的不需要的噪音。

5 - Then, approximate the contour . 5 - 然后, 近似轮廓 ( More about contour approximation ). 关于轮廓近似的更多信息 )。

6 - For a rectangle, it will give you the four corners. 6 - 对于一个矩形,它会给你四个角。 For others, corresponding corners will be given. 对于其他人,将给出相应的角落。

So filter these contours with respect to number of elements in approximated contour that should be four, which is same as number of corners. 因此,根据近似轮廓中应为4的元素数量过滤这些轮廓,这与角数相同。 First property of rectangle. 矩形的第一个属性。

7 - Next, there may be some shapes with four corners but not rectangles. 7 - 接下来,可能有一些形状有四个角但不是矩形。 So we take second property of rectangles, ie all inner angles are 90 . 所以我们采用矩形的第二个属性,即所有内角都是90° So we find the angle at all the corners using the relation below : 因此,我们使用以下关系找到所有角落的角度:

在此输入图像描述

And if cos (theta) < 0.1, ie theta > 84 degree, that is a rectangle. 如果cos(theta)<0.1,即theta> 84度,那就是一个矩形。

8 - Then what about the square? 8 - 那广场怎么样? Use its property, that all the sides are equal. 使用它的财产,所有方面都是平等的。

You can find the distance between two points by the relation as shown above. 您可以通过上述关系找到两点之间的距离。 Check if they all are equal, then that rectangle is a square. 检查它们是否相等,然后该矩形是正方形。

This is how the code works. 这就是代码的工作原理。

Below is the output I got applying above mentioned code on an image : 下面是我在图像上应用上述代码的输出:

在此输入图像描述

EDIT : 编辑:

It has been asked how to remove the rectangle detected at the border. 已经询问如何删除边界处检测到的矩形。 It is because, opencv finds white objects in black background, so is border. 这是因为,opencv在黑色背景中找到白色物体,因此是边框。 Just inverting the image using cv2.bitwise_not() function will solve the problem. 只需使用cv2.bitwise_not()函数反转图像就可以解决问题。 we get the result as below: 我们得到如下结果:

在此输入图像描述

You can find more information about contour here : Contours - 1 : Getting Started 您可以在此处找到有关轮廓的更多信息: 轮廓 - 1:入门

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

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