OpenCV的抗扭斜的轮廓
inputImage的OpenCV的抗扭斜的轮廓
ResultImage
我已经能够过滤最大的轮廓图像中检测令牌。
我已经应用了经纱知觉,但它只是在轮廓的边缘裁剪图像,没有别的。
我想要将检测到的令牌从图像的其余部分中裁剪出来,在保持比例的情况下对其进行去偏斜,以便结果图像应该直立,笔直。然后,我将继续寻找令牌中的斑点来检测其中标记的日期。
private Mat processMat(Mat srcMat) { Mat processedMat = new Mat();
Imgproc.cvtColor(srcMat, processedMat, Imgproc.COLOR_BGR2GRAY);
Imgproc.GaussianBlur(processedMat, processedMat, new Size(5, 5), 5);
Imgproc.threshold(processedMat, processedMat, 127, 255, Imgproc.THRESH_BINARY);
List<MatOfPoint> contours = new ArrayList<>();
Mat hierarchy = new Mat();
Imgproc.findContours(processedMat, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
double maxVal = 0;
int maxValIdx = 0;
for (int contourIdx = 0; contourIdx < contours.size(); contourIdx++) {
double contourArea = Imgproc.contourArea(contours.get(contourIdx));
if (maxVal < contourArea) {
maxVal = contourArea;
maxValIdx = contourIdx;
}
}
if (!contours.isEmpty()) {
Imgproc.drawContours(srcMat, contours, maxValIdx, new Scalar(0,255,0), 3);
Rect rect = Imgproc.boundingRect(contours.get(maxValIdx));
Log.e("rect", "" + rect);
int top = srcMat.height();
int left = srcMat.width();
int right = 0;
int bottom = 0;
if(rect.x < left) {
left = rect.x;
}
if(rect.x+rect.width > right){
right = rect.x+rect.width;
}
if(rect.y < top){
top = rect.y;
}
if(rect.y+rect.height > bottom){
bottom = rect.y+rect.height;
}
Point topLeft = new Point(left, top);
Point topRight = new Point(right, top);
Point bottomRight = new Point(right, bottom);
Point bottomLeft = new Point(left, bottom);
return warp(srcMat, topLeft, topRight, bottomLeft, bottomRight);
}
return null;
}
Mat warp(Mat inputMat, Point topLeft, Point topRight, Point bottomLeft, Point bottomRight) {
int resultWidth = (int)(topRight.x - topLeft.x);
int bottomWidth = (int)(bottomRight.x - bottomLeft.x);
if(bottomWidth > resultWidth)
resultWidth = bottomWidth;
int resultHeight = (int)(bottomLeft.y - topLeft.y);
int bottomHeight = (int)(bottomRight.y - topRight.y);
if (bottomHeight > resultHeight) {
resultHeight = bottomHeight;
}
Mat outputMat = new Mat(resultWidth, resultHeight, CvType.CV_8UC1);
List<Point> source = new ArrayList<>();
source.add(topLeft);
source.add(topRight);
source.add(bottomLeft);
source.add(bottomRight);
Mat startM = Converters.vector_Point2f_to_Mat(source);
Point ocvPOut1 = new Point(0, 0);
Point ocvPOut2 = new Point(resultWidth, 0);
Point ocvPOut3 = new Point(0, resultHeight);
Point ocvPOut4 = new Point(resultWidth, resultHeight);
List<Point> dest = new ArrayList<>();
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));
return outputMat;
}
更新1
替换此:
return warp(srcMat, topLeft, topRight, bottomLeft, bottomRight);
这一点:
return warp(srcMat, topLeft, topRight, bottomRight, bottomLeft);
结果更新1:
更新2
public Mat warp(Mat inputMat, MatOfPoint selectedContour) { MatOfPoint2f new_mat = new MatOfPoint2f(selectedContour.toArray());
MatOfPoint2f approxCurve_temp = new MatOfPoint2f();
int contourSize = (int) selectedContour.total();
Imgproc.approxPolyDP(new_mat, approxCurve_temp, contourSize * 0.05, true);
double[] temp_double;
temp_double = approxCurve_temp.get(0,0);
Point p1 = new Point(temp_double[0], temp_double[1]);
temp_double = approxCurve_temp.get(1,0);
Point p2 = new Point(temp_double[0], temp_double[1]);
temp_double = approxCurve_temp.get(2,0);
Point p3 = new Point(temp_double[0], temp_double[1]);
temp_double = approxCurve_temp.get(3,0);
Point p4 = new Point(temp_double[0], temp_double[1]);
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);
int resultWidth = 846;
int resultHeight = 2048;
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;
}
结果更新2:
我已经改变了我的经功能的位和代码附加。 然而,合成图像以某种方式在错误的方向上旋转。你能指导我这是做这件事的正确方法吗?
Android设备方向设置为:纵向,输入图像也是纵向。
更新3
我设法通过排序的角落,像这样伸直令牌:
List<Point> source = new ArrayList<Point>(); source.add(p2);
source.add(p3);
source.add(p4);
source.add(p1);
Mat startM = Converters.vector_Point2f_to_Mat(source);
结果更新3:
然而,由此产生的图像从左侧裁剪,我不知道如何解决这个问题。 如果令牌向右或向左倾斜并且输出图像是笔直的,我已设法拉直输入图像。但是,如果输入图像已经将令牌居中且直线向上。它旋转像这样的令牌,使用相同的代码:
问题更新3:
回答:
到纠偏票转型是接近仿射之一。您可以通过用平行四边形近似轮廓来获得它。您可以将平行四边形的顶点作为最左边,最顶端,最右边和最底部的点。其实,你只需要三个顶点(第四个可以从这些顶点重新计算)。也许平行四边形的最小二乘拟合是可能的,我不知道。
另一种选择是考虑从四个点定义的单应变换(但计算更复杂)。它将考虑到视角。 (您可能会在这里获得一些见解:https://www.codeproject.com/Articles/674433/Perspective-Projection-of-a-Rectangle-Homography。)
要整理图像,只需应用逆变换并检索矩形即可。无论如何,你会注意到这个矩形的大小是未知的,所以你可以任意缩放它。最难的问题是找到合适的宽高比。
以上是 OpenCV的抗扭斜的轮廓 的全部内容, 来源链接: utcz.com/qa/257750.html