【发布时间】:2016-12-09 11:09:59
【问题描述】:
我正在开发一个程序来检测矩形形状并将边界框绘制到检测到的区域。
对于边缘检测,我使用了 Canny 边缘检测。 然后,我使用霍夫变换提取线条。
这是原图 enter image description here
这是结果图片 enter image description here
我的问题是我无法在检测到的区域上绘制边界框。 看来我的程序只能检测到一条水平线。 如何检测矩形形状并将矩形线绘制到检测到的形状?
我看过类似的问题,要求找到矩形的4个角点,检查点是否为90度,然后找到交点。我真的很困惑如何在 Java opencv 中对其进行编码。检测形状并将边界框绘制到检测到的其他方法也可以。
这是代码
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.imgcodecs.*;
import org.opencv.imgproc.Imgproc;
public class HoughTransformCV2 {
public static void main(String[] args) {
try {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat source = Imgcodecs.imread("rectangle.jpg", Imgcodecs.CV_LOAD_IMAGE_ANYCOLOR);
Mat destination = new Mat(source.rows(), source.cols(), source.type());
Imgproc.cvtColor(source, destination, Imgproc.COLOR_RGB2GRAY);
Imgproc.equalizeHist(destination, destination);
Imgproc.GaussianBlur(destination, destination, new Size(5, 5), 0, 0, Core.BORDER_DEFAULT);
Imgproc.Canny(destination, destination, 50, 100);
//Imgproc.adaptiveThreshold(destination, destination, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 15, 40);
Imgproc.threshold(destination, destination, 0, 255, Imgproc.THRESH_BINARY);
if (destination != null) {
Mat lines = new Mat();
Imgproc.HoughLinesP(destination, lines, 1, Math.PI / 180, 50, 30, 10);
Mat houghLines = new Mat();
houghLines.create(destination.rows(), destination.cols(), CvType.CV_8UC1);
//Drawing lines on the image
for (int i = 0; i < lines.cols(); i++) {
double[] points = lines.get(0, i);
double x1, y1, x2, y2;
x1 = points[0];
y1 = points[1];
x2 = points[2];
y2 = points[3];
Point pt1 = new Point(x1, y1);
Point pt2 = new Point(x2, y2);
//Drawing lines on an image
Imgproc.line(source, pt1, pt2, new Scalar(0, 0, 255), 4);
}
}
Imgcodecs.imwrite("rectangle_houghtransform.jpg", source);
} catch (Exception e) {
System.out.println("error: " + e.getMessage());
}
}
}
对 Java 的任何帮助将不胜感激 :) 非常感谢!
【问题讨论】:
标签: java algorithm opencv image-processing object-detection