因此,我想轉(zhuǎn)換圖像,但無法真正找到使用 OpenCV 進(jìn)行轉(zhuǎn)換的正確方法。我有圖像的第一件事就是說 500x600px 里面有一個扭曲的東西,我想“拉直”看圖像:我正在像這樣獲得數(shù)獨的輪廓: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.shapeimage_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)但是當(dāng)我查看圖像時,它以一種奇怪的方式發(fā)生了變化。我想拉伸數(shù)獨的頂部,使其輪廓筆直。你能指出我的代碼有什么問題嗎?我假設(shè)這可能是我創(chuàng)建 4 個極端像素的方式,然后將這些像素放入getPerspectiveTransform以獲得轉(zhuǎn)換矩陣,但尚未設(shè)法使其工作。
2 回答

守著一只汪
TA貢獻(xiàn)1872條經(jīng)驗 獲得超4個贊
假設(shè)您已經(jīng)準(zhǔn)確地找到了數(shù)獨的角點,您可以將輸入圖像仿射變換為:
# Hard coded the points here assuming that you already have 4 corners of sudoku image
sudoku_corner_points = np.float32([[235, 40], [1022, 55], [190, 875], [1090, 880]])
canvas = np.ones((500, 500), dtype=np.uint8)
dst_points = np.float32([[0, 0], [500, 0], [0, 500], [500, 500]])
t_matrix = cv2.getPerspectiveTransform(sudoku_corner_points, dst_points)
transformed_image = cv2.warpPerspective(img, t_matrix, (500, 500))
添加回答
舉報
0/150
提交
取消