我正在尝试让OpenCV在图像中检测床。我运行了常规的灰度、模糊、Canny操作,并尝试了凸包算法。然而,由于存在大量“噪音”,导致额外的轮廓和干扰物体检测。因此,我无法正确地检测床。
以下是输入图像以及Canny边缘检测结果: 如您所见,已经接近完成。我已经有了床的轮廓,尽管右上角有一个间隙 - 这妨碍了我检测到一个封闭的矩形。
这是我正在运行的代码:
以下是输入图像以及Canny边缘检测结果: 如您所见,已经接近完成。我已经有了床的轮廓,尽管右上角有一个间隙 - 这妨碍了我检测到一个封闭的矩形。
这是我正在运行的代码:
import cv2
import numpy as np
def contoursConvexHull(contours):
print("contours length = ", len(contours))
print("contours length of first item = ", len(contours[1]))
pts = []
for i in range(0, len(contours)):
for j in range(0, len(contours[i])):
pts.append(contours[i][j])
pts = np.array(pts)
result = cv2.convexHull(pts)
print(len(result))
return result
def auto_canny(image, sigma = 0.35):
# compute the mediam of the single channel pixel intensities
v = np.median(image)
# apply automatic Canny edge detection using the computed median
lower = int(max(0, (1.0 - sigma) * v))
upper = int(min(255, (1.0 + sigma) *v))
edged = cv2.Canny(image, lower, upper)
# return edged image
return edged
# Get our image in color mode (1)
src = cv2.imread("bed_cv.jpg", 1)
# Convert the color from BGR to Gray
srcGray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
# Use Gaussian Blur
srcBlur = cv2.GaussianBlur(srcGray, (3, 3), 0)
# ret is the returned value, otsu is an image
##ret, otsu = cv2.threshold(srcBlur, 0, 255,
## cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# Use canny
##srcCanny = cv2.Canny(srcBlur, ret, ret*2, 3)
srcCanny1 = auto_canny(srcBlur, 0.70)
# im is the output image
# contours is the contour list
# I forgot what hierarchy was
im, contours, hierarchy = cv2.findContours(srcCanny1,
cv2.RETR_TREE,
cv2.CHAIN_APPROX_SIMPLE)
##cv2.drawContours(src, contours, -1, (0, 255, 0), 3)
ConvexHullPoints = contoursConvexHull(contours)
##cv2.polylines(src, [ConvexHullPoints], True, (0, 0, 255), 3)
cv2.imshow("Source", src)
cv2.imshow("Canny1", srcCanny1)
cv2.waitKey(0)
由于床的轮廓不是封闭的,我无法适应矩形或检测具有最大面积的轮廓。
我能想到的解决方案是使用轮廓点推断出可能的最大矩形,希望填补这个小间隙,但由于矩形不完整,我不太确定如何继续。