这是一张卡车集装箱的图像,但是从俯视角度来看。首先,我需要找到矩形并知道每个角落的位置。目标是了解集装箱的尺寸。
这是一张卡车集装箱的图像,但是从俯视角度来看。首先,我需要找到矩形并知道每个角落的位置。目标是了解集装箱的尺寸。
检测到的边界矩形 ->
蒙版 ->
检测到的角落
角点
(188, 351)
(47, 348)
(194, 32)
(53, 29)
代码
import cv2
import numpy as np
# Load image, grayscale, blur, Otsu's threshold
image = cv2.imread('1.png')
mask = np.zeros(image.shape[:2], dtype=np.uint8)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5,5), 0)
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
# Find distorted bounding rect
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
area = cv2.contourArea(c)
if area > 5000:
# Find distorted bounding rect
rect = cv2.minAreaRect(c)
corners = cv2.boxPoints(rect)
corners = np.int0(corners)
cv2.fillPoly(mask, [corners], (255,255,255))
# Draw corner points
corners = corners.tolist()
print(corners)
for corner in corners:
x, y = corner
cv2.circle(image, (x, y), 5, (36,255,12), -1)
cv2.imshow('thresh', thresh)
cv2.imshow('image', image)
cv2.imshow('mask', mask)
cv2.waitKey()
cv2.goodFeaturesToTrack
来获取角落坐标。 - nathancyminAreaRect
! - user1196549minAreaRect
应用于轮廓是您最好的朋友。不需要角点检测器。
https://docs.opencv.org/3.4/d3/dc0/group__imgproc__shape.html#ga3d476a3417130ae5154aea421ca7ead9