【发布时间】:2019-01-08 22:58:43
【问题描述】:
所以,我想转换一张图片,但真的找不到使用 OpenCV 的合适方法。
我有图像的第一件事可以说是 500x600px,其中有一个扭曲的东西我想“拉直”看图像:
我正在像这样获得数独的轮廓:
cropped_image, contours, _ =
cv2.findContours(cropped_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
max_contour = max(contours, key=cv2.contourArea)
然后我得到max_contour 和图像极端像素(左上、右上、右下和左下)并得到变换矩阵并像这样变换图像:
x, y = cropped_image.shape
image_extreme_pixels = np.array([[0, y], [x, y], [x, 0], [0, 0]], dtype=np.float32)
c_x, c_y = [], []
for i in contour:
c_x.append(i[0][0])
c_y.append(i[0][1])
contour_extreme_pixels = np.array([
[min(c_x), max(c_y)],
[max(c_x), max(c_y)],
[max(c_x), min(c_y)],
[min(c_x), min(c_y)]],
dtype=np.float32)
t_matrix = cv2.getPerspectiveTransform(contour_extreme_pixels, image_extreme_pixels)
transformed_image = cv2.warpPerspective(cropped_image, t_matrix, (y, x))
plt.imshow(cropped_image, interpolation='nearest', cmap=plt.cm.gray)
但是当我查看图像时,它以一种奇怪的方式发生了变化。我想拉伸数独的顶部,使其轮廓笔直。
您能指出我的代码有什么问题吗?
我假设这可能是我创建 4 个极端像素然后放入 getPerspectiveTransform 以获取转换矩阵但尚未使其工作的方式。
【问题讨论】: