您好,我面临着一个问题,在原始图像上绘制轮廓使用面具下面的代码,我正在使用它请您知道这是什么问题吗?
import cv2
import numpy as np
def contour_image_mask(image_path, mask_path):
# Read the original image
image = cv2.imread(image_path)
#print(image.shape) # (512, 512, 3)
# Read the segmentation mask, here is a white mask with a black background in this data set
mask_2d = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE)
# Cropped to the same size as the original image
mask_2d = mask_2d[0:399, 0:428]
h, w = mask_2d.shape
# Applying threshold on binary mask. and then find the counter with the binary mask.
ret, thresh = cv2.threshold(mask_2d, 127, 255, 0)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnt = contours[0]
cv2.drawContours(image, [cnt], 0, (0, 255, 0), 1)
cv2.imwrite(os.path.join(outdir,os.path.basename(imgfile)),image)
outdir="/data/Hammad/Hammad/countor/"
for imgfile in glob.glob(r'/data/Hammad/Hammad/*.jpg'):
for maskfile in glob.glob(r'/data/Hammad/Hammad/resize/*.jpg'):
contour_image_mask(imgfile, maskfile)
IndexError Traceback (most recent call last)
<ipython-input-37-2b2baa0d3065> in <module>
2 for imgfile in glob.glob(r'/data/Hammad/Hammad/*.jpg'):
3 for maskfile in glob.glob(r'/data/Hammad/Hammad/resize/*.jpg'):
----> 4 contour_image_mask(imgfile, maskfile)
<ipython-input-36-ab9e8a92d580> in contour_image_mask(image_path, mask_path)
14 ret, thresh = cv2.threshold(mask_2d, 127, 255, 0)
15 contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
---> 16 cnt = contours[0]
17 cv2.drawContours(image, [cnt], 0, (0, 255, 0), 1)
18 cv2.imwrite(os.path.join(outdir,os.path.basename(imgfile)),image)
IndexError: list index out of range
暂无答案!
目前还没有任何答案,快来回答吧!