查找使用openCv在Java中检查的轮廓 [英] Find a contour checked in java with openCv

查看:148
本文介绍了查找使用openCv在Java中检查的轮廓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望程序输入图像(附件) 输入 并输出像这样的字符串:

I want my program to input an image (attachment) input and output a string like :

  • 问题1:选中第2个框
  • 问题2:选中第1框
  • 问题3:选中第3框
  • 问题4:选中第2个框

等等...(每张图片我所问的问题太多)

Etc ... (as many question as I have per image)

我可以使用此程序简单地找到空白框:

I can simply locate empty boxes with this program:

import java.util.ArrayList;
import java.util.List;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
public class test {
    public static void main(String[] args) {
        // Load the library of openCv
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        // Consider the image for processing
        Mat image = Imgcodecs.imread("C:/attachement.jpg", Imgproc.COLOR_BGR2GRAY);
        Mat imageHSV = new Mat(image.size(), CvType.CV_8UC4);
        Mat imageBlurr = new Mat(image.size(),CvType.CV_8UC4);
        Mat imageA = new Mat(image.size(), CvType.CV_32F);
        Imgproc.cvtColor(image, imageHSV, Imgproc.COLOR_BGR2GRAY);
        Imgproc.GaussianBlur(imageHSV, imageBlurr, new Size(5,5), 0);
        Imgproc.adaptiveThreshold(imageBlurr, imageA, 255,Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY,7, 5);
        List<MatOfPoint> contours = new ArrayList<MatOfPoint>();    
        Imgproc.findContours(imageA, contours, new Mat(), Imgproc.RETR_LIST,Imgproc.CHAIN_APPROX_SIMPLE);
        for(int i=0; i< contours.size();i++)
        {
            if (Imgproc.contourArea(contours.get(i)) > 50 )
            {
                Rect rect = Imgproc.boundingRect(contours.get(i));
                if ((rect.height > 35 && rect.height < 60) && (rect.width > 35 && rect.width < 60))
                {
                    Imgproc.rectangle(image, new Point(rect.x,rect.y), new Point(rect.x+rect.width,rect.y+rect.height),new Scalar(0,0,255));
                }
            }
        }
        Imgcodecs.imwrite("C:/Users/Benreghai/Downloads/tof/output.png",image);
    }
}

这是我的程序的输出:(附有图片) 输出

Here is the output of my program: (image attached) output

谢谢您的帮助!

推荐答案

在注释之后,我添加了所需的代码.

Following the comments, I'm adding the piece of code required.

Vector<Mat> rectangles = new Vector<Mat>();
for(int i=0; i< contours.size();i++)
    {
        if (Imgproc.contourArea(contours.get(i)) > 50 )
        {
            Rect rect = Imgproc.boundingRect(contours.get(i));
            if ((rect.height > 35 && rect.height < 60) && (rect.width > 35 && rect.width < 60))
            {
                 //Instead of drawing the rectangle, extract it using the Rect data type
                Rect rec = new Rect(rect.x, rect.y, rect.width, rect.height);
                rectangles.add(new Mat(image, rec));  
                // Imgproc.rectangle(image, new Point(rect.x,rect.y), new Point(rect.x+rect.width,rect.y+rect.height),new Scalar(0,0,255));
            }
        }
    }
     // Once you have all the rectangles, loop over them to find intersecting lines
     // Initialize a vector of points to save the start and end of each line segment found
     Vector<Point> start = new Vector<Point>();
     Vector<Point> end = new Vector<Point>();
     for (Mat rectangle : rectangles){
     // Perform edge detection for using Canny, apply threshold suitably
     int canny_lower = 100, canny_upper = 200;
     Mat canny_img = new Mat(rectangle.size, CvType.8UC1);
     Imgproc.Canny(rectangle, canny_img, canny_lower, canny_upper);
     // Probably a Morphological Operation here, if required
     Mat hough_lines = new Mat();
     // Adjust the parameters from (here)[http://docs.opencv.org/2.4/doc/tutorials/imgproc/imgtrans/hough_lines/hough_lines.html]
     Imgproc.HoughLinesP(canny_img, hough_lines, 1, Math.PI / 720, 80, img.width() * 0.5, 50);
     // hough_lines contains all the line segments detected by HoughLinesP
     // Find coordinates of the line segment found using the previous step
     for(int j = 0; j < hough_lines.cols(); j++){
     double[] vec = hough_lines.get(0, j);
     double x1 = vec[0], y1 = vec[1], x2 = vec[2], y2 = vec[3];
     start.add(new Point(x1, y1));
     end.add(new Point(x2, y2));
} 
 // Now that you have all lines the rectangle, find angle between them to
 check if they are intersecting
 for (int i = 0; i < start.size(); i++){
  // Find the slope between segment i and i+1
  // Multiple approaches here
  char intersect_flag = get_line_intersection();
  if (intersect_flag == 1)
      System.out.print("\n Intersecting");
  else 
      System.out.print("\n Not intersecting");

}

}
// Returns 1 if the lines intersect, otherwise 0. In addition, if the lines intersect the intersection point may be stored in the floats i_x and i_y.
// Pass x and y coordinates of each line segment for p0 to p4.i_x and i_y will have the coordinate of intersection, not required here. 
char get_line_intersection(float p0_x, float p0_y, float p1_x, float p1_y, 
float p2_x, float p2_y, float p3_x, float p3_y, float i_x, float i_y)
{
float s1_x, s1_y, s2_x, s2_y;
s1_x = p1_x - p0_x;     s1_y = p1_y - p0_y;
s2_x = p3_x - p2_x;     s2_y = p3_y - p2_y;

float s, t;
s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / (-s2_x * s1_y + s1_x * s2_y);
t = ( s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / (-s2_x * s1_y + s1_x * s2_y);

if (s >= 0 && s <= 1 && t >= 0 && t <= 1)
{
    // Collision detected
    if (i_x != NULL)
        *i_x = p0_x + (t * s1_x);
    if (i_y != NULL)
        *i_y = p0_y + (t * s1_y);
    return 1;
}

return 0; // No collision
}

希望有帮助!遵循如何检测两个线段相交的地方?表示两条线的交点.

Hope it helps! Follow How do you detect where two line segments intersect? for the intersection of two lines.

这篇关于查找使用openCv在Java中检查的轮廓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆