I have a camera pointing at a Zen Garden from above. However, the camera is fixed on the side rather than directly above the plate. As a result, the image looks like this (note the skewed shape of the rectangle):

KQ9qk.jpg

Is there a way to process the image so that the sand area can look more or less like a perfect square?

cap = cv2.VideoCapture(0)

while True:

ret, img = cap.read()

img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

img = cv2.flip(img,0)

cv2.imshow('Cropping', img)

if cv2.waitKey(500) & 0xff == 27:

cv2.destroyAllWindows()

break

Many thanks.

解决方案

You can do perspective transformation on your image. Here's a first shot that gets you in the ballpark:

import cv2

import numpy as np

img = cv2.imread('zen.jpg')

rows, cols, ch = img.shape

pts1 = np.float32(

[[cols*.25, rows*.95],

[cols*.90, rows*.95],

[cols*.10, 0],

[cols, 0]]

)

pts2 = np.float32(

[[cols*0.1, rows],

[cols, rows],

[0, 0],

[cols, 0]]

)

M = cv2.getPerspectiveTransform(pts1,pts2)

dst = cv2.warpPerspective(img, M, (cols, rows))

cv2.imshow('My Zen Garden', dst)

cv2.imwrite('zen.jpg', dst)

cv2.waitKey()

eSg5z.jpg

You can fiddle more with the numbers, but you get the idea.

Here's some online examples. The first link has a useful plot of peg points that correlate to the transformation matrix:

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐