gpt4 book ai didi

android - 如何在 openCV 中找到 Rect 对象的角点?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:59:54 24 4
gpt4 key购买 nike

我在 android 平台上使用 openCV 库。我已成功从图像中检测到最大的矩形,但由于我的应用程序将用于扫描目的,因此我也希望具有透视更改功能。

我知道如何应用 perspectiveTransform 和 warpPerspectiveTransform,但为此我需要源点的矩形角。

鉴于我们有第一个角的坐标(左上角)和与 Rect 对象相关联的宽度/高度,看起来很容易找到角,但问题是,对于旋转的矩形(通常是 boundingRect 但边不平行于轴),这些值非常不同。在这种情况下,它存储对应于另一个矩形的值,该矩形的边平行于轴并覆盖旋转的矩形,因此我无法检测到实际矩形的角。

我还想比较这两种从图像中检测纸张的算法。

  1. Canny edge -> 最大轮廓 -> 最大矩形 -> 寻找角点 -> 透视变化

  2. Canny edge -> Hough lines -> 线的交点 -> 透视变化

我想问的是,如果我们有一个 Rect 对象,如何获取该矩形的所有角?

提前致谢。

最佳答案

很高兴回答我的问题!这很容易,但是当你刚开始做一些相关文档不太合适的事情时,就会发生这种情况。

我一直在努力获取在 openCV 的实现中未定义的一般矩形的角,因此几乎是不可能的。

我按照 stackoverflow 上的标准代码进行了最大的方形检测。使用 approxCurve 本身可以轻松找出角点。

//将图片转为黑白

        Imgproc.cvtColor(imgSource, imgSource, Imgproc.COLOR_BGR2GRAY);

//convert the image to black and white does (8 bit)
Imgproc.Canny(imgSource, imgSource, 50, 50);

//apply gaussian blur to smoothen lines of dots
Imgproc.GaussianBlur(imgSource, imgSource, new org.opencv.core.Size(5, 5), 5);

//find the contours
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Imgproc.findContours(imgSource, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

double maxArea = -1;
int maxAreaIdx = -1;
Log.d("size",Integer.toString(contours.size()));
MatOfPoint temp_contour = contours.get(0); //the largest is at the index 0 for starting point
MatOfPoint2f approxCurve = new MatOfPoint2f();
MatOfPoint largest_contour = contours.get(0);
//largest_contour.ge
List<MatOfPoint> largest_contours = new ArrayList<MatOfPoint>();
//Imgproc.drawContours(imgSource,contours, -1, new Scalar(0, 255, 0), 1);

for (int idx = 0; idx < contours.size(); idx++) {
temp_contour = contours.get(idx);
double contourarea = Imgproc.contourArea(temp_contour);
//compare this contour to the previous largest contour found
if (contourarea > maxArea) {
//check if this contour is a square
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() == 4) {
maxArea = contourarea;
maxAreaIdx = idx;
approxCurve=approxCurve_temp;
largest_contour = temp_contour;
}
}
}

Imgproc.cvtColor(imgSource, imgSource, Imgproc.COLOR_BayerBG2RGB);
sourceImage =Highgui.imread(Environment.getExternalStorageDirectory().
getAbsolutePath() +"/scan/p/1.jpg");
double[] temp_double;
temp_double = approxCurve.get(0,0);
Point p1 = new Point(temp_double[0], temp_double[1]);
//Core.circle(imgSource,p1,55,new Scalar(0,0,255));
//Imgproc.warpAffine(sourceImage, dummy, rotImage,sourceImage.size());
temp_double = approxCurve.get(1,0);
Point p2 = new Point(temp_double[0], temp_double[1]);
// Core.circle(imgSource,p2,150,new Scalar(255,255,255));
temp_double = approxCurve.get(2,0);
Point p3 = new Point(temp_double[0], temp_double[1]);
//Core.circle(imgSource,p3,200,new Scalar(255,0,0));
temp_double = approxCurve.get(3,0);
Point p4 = new Point(temp_double[0], temp_double[1]);
// Core.circle(imgSource,p4,100,new Scalar(0,0,255));
List<Point> source = new ArrayList<Point>();
source.add(p1);
source.add(p2);
source.add(p3);
source.add(p4);
Mat startM = Converters.vector_Point2f_to_Mat(source);
Mat result=warp(sourceImage,startM);
return result;

下面给出了用于透视变换的函数:

 public Mat warp(Mat inputMat,Mat startM) {
int resultWidth = 1000;
int resultHeight = 1000;

Mat outputMat = new Mat(resultWidth, resultHeight, CvType.CV_8UC4);



Point ocvPOut1 = new Point(0, 0);
Point ocvPOut2 = new Point(0, resultHeight);
Point ocvPOut3 = new Point(resultWidth, resultHeight);
Point ocvPOut4 = new Point(resultWidth, 0);
List<Point> dest = new ArrayList<Point>();
dest.add(ocvPOut1);
dest.add(ocvPOut2);
dest.add(ocvPOut3);
dest.add(ocvPOut4);
Mat endM = Converters.vector_Point2f_to_Mat(dest);

Mat perspectiveTransform = Imgproc.getPerspectiveTransform(startM, endM);

Imgproc.warpPerspective(inputMat,
outputMat,
perspectiveTransform,
new Size(resultWidth, resultHeight),
Imgproc.INTER_CUBIC);

return outputMat;
}

关于android - 如何在 openCV 中找到 Rect 对象的角点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21084098/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com