找到最大的轮廓 - OpenCV,Python - 出现错误
Find the biggest Contour- OpenCV, Python - Getting Errors
我试图在图像中找到汽车周围的最大轮廓。
为了找到轮廓,我从 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 的新手。
我不太清楚是什么导致了你的问题,但我敢打赌这是由这个引起的
threshold = cv2.threshold(image_gray,127, 255,0)
这个函数
cv2.threshold()
returns 元组,所以你需要解压更多的值。像这样
_,threshold = cv2.threshold(image_gray,127, 255,0)
其中 _ 忽略元组的第一个返回值
其中 阈值 是矩阵。
基本上我们所做的是这样的:
_, matrix = (127,'Matrix')
>>> print(matrix)
'Matrix'
你做的是这样的:
matrix = (127,'Matrix')
>>> print(matrix)
(127,'Matrix')
完整代码:适合我
import cv2
import numpy as np
hsv_image = cv2.imread('someimage.jpg',1) # pretend its HSV
rgbimg = cv2.cvtColor(hsv_image, cv2.COLOR_HSV2RGB)
image_gray = cv2.cvtColor(rgbimg, cv2.COLOR_BGR2GRAY)
_,threshold = cv2.threshold(image_gray,127, 255,0)
im2,contours, hierarchy = cv2.findContours(threshold, 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]
注意事项:
理解这一点很重要,OpenCV 出于某种原因(我从历史上听说过)没有使用 RGB,而是 BGR 颜色 space。只要您使用灰度或仅在 opencv 中工作(不使用 RGB),您甚至都不会知道。但是一旦你将你的数组转换成 PIL,你就会知道......再多说一句。在您的情况下没问题,因为 BGR 到 GRAY 或 RGB 到 GRAY 会产生相同的图像...
从 OpenCV 4.0 开始,findContours()
return 只有 2 个值,所以应该是:
contours, hierarchy = cv2.findContours(threshold, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
我试图在图像中找到汽车周围的最大轮廓。
为了找到轮廓,我从 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 的新手。
我不太清楚是什么导致了你的问题,但我敢打赌这是由这个引起的
threshold = cv2.threshold(image_gray,127, 255,0)
这个函数
cv2.threshold()
returns 元组,所以你需要解压更多的值。像这样
_,threshold = cv2.threshold(image_gray,127, 255,0)
其中 _ 忽略元组的第一个返回值
其中 阈值 是矩阵。
基本上我们所做的是这样的:
_, matrix = (127,'Matrix')
>>> print(matrix)
'Matrix'
你做的是这样的:
matrix = (127,'Matrix')
>>> print(matrix)
(127,'Matrix')
完整代码:适合我
import cv2
import numpy as np
hsv_image = cv2.imread('someimage.jpg',1) # pretend its HSV
rgbimg = cv2.cvtColor(hsv_image, cv2.COLOR_HSV2RGB)
image_gray = cv2.cvtColor(rgbimg, cv2.COLOR_BGR2GRAY)
_,threshold = cv2.threshold(image_gray,127, 255,0)
im2,contours, hierarchy = cv2.findContours(threshold, 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]
注意事项:
理解这一点很重要,OpenCV 出于某种原因(我从历史上听说过)没有使用 RGB,而是 BGR 颜色 space。只要您使用灰度或仅在 opencv 中工作(不使用 RGB),您甚至都不会知道。但是一旦你将你的数组转换成 PIL,你就会知道......再多说一句。在您的情况下没问题,因为 BGR 到 GRAY 或 RGB 到 GRAY 会产生相同的图像...
从 OpenCV 4.0 开始,findContours()
return 只有 2 个值,所以应该是:
contours, hierarchy = cv2.findContours(threshold, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)