【发布时间】:2019-07-04 00:55:54
【问题描述】:
我正在尝试在图像中找到汽车周围的最大轮廓。
为了找到轮廓,我从 OpenCv 官方文档中了解到以下内容:
#convert the image to grayscale from rgb
1. image_gray = cv2.cvtColor(image,cv2.COLOR_RGB2GRAY)
2. threshold = cv2.threshold(image_gray, 127,(0,255,0),0)
3. image2, contours_list, hierarchy = cv2.findContours(threshold, cv2.RETR_LIST, cv2.CHAIN_APROX_SIMPLE)
问题-1:
我已经应用了 cv2.GaussianBlur() 并将其转换为 HSV 格式以创建掩码,以便稍后使用 MorphologyEx 方法检测某些特定颜色。问题是,上面步骤 2 中的代码需要 RGB 格式的图像将其转换为灰度或灰度格式本身,但我有 HSV 格式,它没有像 cv2.COLOR_HSV2GRAY 这样的标志。
我已经编写了以下 2 个版本的相同方法来查找最大轮廓,它们会引发 2 个不同的错误:
在这个方法中,我首先创建了一个阈值,它需要一个灰度图像,以传递给 cv2.findContour 方法
def find_biggest_contour(image):
image = image.copy()
#1
image_gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
#2
threshold = cv2.threshold(image_gray,127, 255,0)
#3
image2, contours, heirarchy = cv2.findContours(threshold, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) # countours is a python list
contours_sizes= [(cv2.contourArea(cnt), cnt) for cnt in contours]
biggest_contour = max(contours_sizes, key=lambda x: x[0])[1]
#define a mask
mask = np.zeros(image.shape, np.uint8)
cv2.drawContours(mask,[biggest_contour], -1, (0,255,0), 3)# 3=thickness, -1= draw all contours, 2nd arg must be a list
return biggest_contour, mask
此方法向我抛出以下错误:
另一个版本如下(基本上是我从某个地方拿的):
def find_biggest_contour(image):
image = image.copy()
im2,contours, hierarchy = cv2.findContours(image, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
contour_sizes = [(cv2.contourArea(contour), contour) for contour in contours]
biggest_contour = max(contour_sizes, key=lambda x: x[0])[1]
mask = np.zeros(image.shape, np.uint8)
cv2.drawContours(mask, [biggest_contour], -1, 255, -1)
return biggest_contour, mask
请帮助我修复错误。我是opencv的新手。
【问题讨论】:
标签: python opencv image-processing opencv-contour