繁体   English   中英

如何用java在openCv中找到矩形的角

[英]how to find the corner of rectangle in openCv with java

我写了这段检测矩形的代码,但我无法编写检测角的代码。

public class RectDetection {
  public static void main(String[] args) {


 System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
 Mat rectengle=Imgcodecs.imread("D:\\sepano\\rect.png");
       Mat img =new Mat();
      img=rectengle.clone();
        Imgproc.cvtColor(rectengle, img, Imgproc.COLOR_BGR2GRAY);
        Imgproc.GaussianBlur(img, img, new  org.opencv.core.Size(1, 1), 2, 2);
    Imgproc.Canny(img,img,3, 3,5,false);

    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
    Imgproc.findContours(img, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

    MatOfPoint temp_contour = contours.get(0); //the largest is at the index 0 for starting point

    for (int idx = 0; idx < contours.size(); idx++) {
        temp_contour = contours.get(idx);
            MatOfPoint2f new_mat = new MatOfPoint2f( temp_contour.toArray() );
            int contourSize = (int)temp_contour.total();
            MatOfPoint2f approxCurve_temp = new MatOfPoint2f();
            Imgproc.approxPolyDP(new_mat, approxCurve_temp, contourSize*0.05, true);
     if (approxCurve_temp.total()==8) {
                MatOfPoint points = new MatOfPoint( approxCurve_temp.toArray() );
                Rect rect = Imgproc.boundingRect(points);
               Imgproc.rectangle(img, new Point(rect.x,rect.y), new Point(rect.x+rect.width,rect.y+rect.height), new Scalar(170,0,150,0), 5);}}

这是用于角点检测的python代码,但我无法将其转换为java:

import numpy as np
import cv2
from matplotlib import pyplot as plt

img = cv2.imread('simple.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

corners = cv2.goodFeaturesToTrack(gray,25,0.01,10)
corners = np.int0(corners)

for i in corners:
    x,y = i.ravel()
    cv2.circle(img,(x,y),3,255,-1)

plt.imshow(img),plt.show()

有什么可以帮我的吗???

仔细看看你的java代码......

在这一行:

Imgproc.rectangle(img, new Point(rect.x,rect.y), new Point(rect.x+rect.width,rect.y+rect.height), new Scalar(170,0,150,0), 5);

Point(rect.x,rect.y)对应于矩形的左上角, Point(rect.x+rect.width,rect.y+rect.height)对应于矩形的右下角。

矩形检测代码应该就可以了,4个角如下:

Point(rect.x,rect.y) //左上角

Point(rect.x+rect.width,rect.y) //右上角

Point(rect.x,rect.y+rect.height) // Point(rect.x,rect.y+rect.height)

Point(rect.x+rect.width,rect.y+rect.height) // Point(rect.x+rect.width,rect.y+rect.height)

暂无
暂无

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

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