【发布时间】:2021-07-03 06:14:15
【问题描述】:
我有以下图片。我想对矩形白板进行检测和透视变换。
我想检测这 4 个边界/角并对其应用透视变换。请看下图:
我无法检测到矩形的边界。这是我尝试过的:
import cv2, os
import numpy as np
from google.colab.patches import cv2_imshow
image = cv2.imread("img.jpg")
orig1 = image.copy()
# 1) Grayscale image
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# cv2_imshow(gray)
# 2) Erosion
kernel = np.ones((5, 5), np.uint8)
erosion = cv2.erode(gray, kernel, iterations = 1)
# cv2_imshow(erosion)
# 3) Thresholding (OTSU)
blur = cv2.GaussianBlur(erosion, (5,5),0)
ret3, thresh = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# cv2_imshow(thresh)
# 4) Contours
copy = thresh; orig = image;
cnts = cv2.findContours(copy, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
area = -1; c1 = 0
for c in cnts:
if area < cv2.contourArea(c):
area = cv2.contourArea(c)
c1 = c
cv2.drawContours(orig,[c1], 0, (0,255,0), 3)
epsilon = 0.09 * cv2.arcLength(c1,True)
approx = cv2.approxPolyDP(c1,epsilon,True)
if len(approx) != 4:
# Then it will fail here.
pass
cood = []
for i in range(0, len(approx)):
cood.append([approx[i][0][0], approx[i][0][1]])
# 5) Perspective Transformation
def reorder(myPoints):
myPoints = np.array(myPoints).reshape((4, 2))
myPointsNew = np.zeros((4, 1, 2), dtype=np.int32)
add = myPoints.sum(1)
myPointsNew[0] = myPoints[np.argmin(add)]
myPointsNew[3] =myPoints[np.argmax(add)]
diff = np.diff(myPoints, axis=1)
myPointsNew[1] =myPoints[np.argmin(diff)]
myPointsNew[2] = myPoints[np.argmax(diff)]
return myPointsNew
pts1 = np.float32(reorder(cood))
w = 1000; h = 1000; m1 = 1000; m2 = 1000
pts2 = np.float32([[0, 0], [w, 0], [0, h], [w, h]])
matrix = cv2.getPerspectiveTransform(pts1, pts2)
result = cv2.warpPerspective(orig1, matrix, (m1, m2))
cv2_imshow(result)
我也经历过Microsoft's research,但不知道如何实现。 我无法检测和透视变换板。如果你们中的任何人都可以帮助我,那就太好了。另外,如果我的问题需要更多详细信息,请告诉我。
【问题讨论】:
-
您尝试过基于颜色的方法吗?它总是同一个板子吗?比你知道的宽高比。然后通过一些边缘检测/轮廓检测它应该可以工作
-
我还没有尝试过基于颜色的方法。但是,董事会可能不一样。我们无法确定坐标。有没有办法概括它?
-
问题是曝光过度的墙壁,所以墙壁很白,而不是他的自然色灰色。所以你必须先解决这个问题。我还看到您的图像质量很差。基于母猪纹理的方法将是困难的。也许开始寻找一些模型来检测图像中的白板?
-
@Bamwani 我试过了。但是,它不起作用。
标签: python image opencv machine-learning computer-vision