OpenCV getPerspectiveTransform和Java透视
我目前有一个ArrayList<MatOfPoint>
存储图像的轮廓,并检索到最大的轮廓,这是我的透视目标
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Imgproc.findContours(matOut, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);
sort(contours);
MatOfPoint2f m2f = MatOfPoint2f(contours.get(0).toArray());
double arc = Imgproc.arcLength(m2f, true);
MatOfPoint2f approx = new MatOfPoint2f();
Imgproc.approxPolyDP(m2f, approx, arc*0.02, true);
我在如何使用getPerspectiveTransform,然后应用WarpPerspectiveTransform来更改图像以使其适合450x450图像方面遇到了问题
我在python中找到了一个很好的例子,但是我对它不是很熟悉。有人能解释一下我将如何在java中进行纠正吗
def rectify(h):
''' this function put vertices of square we got, in clockwise order '''
h = h.reshape((4,2))
hnew = np.zeros((4,2),dtype = np.float32)
add = h.sum(1)
hnew[0] = h[np.argmin(add)]
hnew[2] = h[np.argmax(add)]
diff = np.diff(h,axis = 1)
hnew[1] = h[np.argmin(diff)]
hnew[3] = h[np.argmax(diff)]
return hnew
# 1 楼答案
简介:为了使用wrapPerspective,我们需要使用getPerspectiveTransform takes:mat src和mat dst计算包裹垫。这些是带有轮廓src(左上、右上、左下、右下)和相应的mat dst(0,0;0449;449,0;449449)的MatOfPoints2f(因此图像将为450x450-如果这是错误的,请纠正我)
opencv 3的Java解决方案是:
整理积分 矩形轮廓为MatOfPoints2f约我们需要按照上面所述对点进行排序:
准备材料src和det
使用getPerspectiveTransform和WrapSpective
我刚刚完成了类似的项目,所以我相信这可以做得更好,但如果有人需要的话,这是一个有效的解决方案