画像から一定の大きさ以下のオブジェクトを消したいと思っているのですが、エラーが出ています。
エラーの意味を教えてください。
Python3.7.3
1#-*- coding:utf-8 -*- 2import cv2 3import numpy as np 4 5def remove_objects(img, lower_size=None, upper_size=None): 6 # find all objects 7 nlabels, labels, stats, centroids = cv2.connectedComponentsWithStats(img) 8 9 sizes = stats[1:, -1] 10 _img = np.zeros((labels.shape)) 11 12 # process all objects, label=0 is background, objects are started from 1 13 for i in range(1, nlabels): 14 15 # remove small objects 16 if (lower_size is not None) and (upper_size is not None): 17 if lower_size < sizes[i - 1] and sizes[i - 1] < upper_size: 18 _img[labels == i] = 255 19 20 elif (lower_size is not None) and (upper_size is None): 21 if lower_size < sizes[i - 1]: 22 _img[labels == i] = 255 23 24 elif (lower_size is None) and (upper_size is not None): 25 if sizes[i - 1] < upper_size: 26 _img[labels == i] = 255 27 28 return _img 29 30 31 32#if __name__ == '__main__': 33img_a = cv2.imread('21/image21_4.jpg') 34''' 35 # change color space 36 img_Lab = cv2.cvtColor(img, cv2.COLOR_BGR2Lab) 37 38 # smoothing 39 img_Lab = cv2.GaussianBlur(img_Lab, (5, 5), 3) 40 img_L, img_a, img_b = cv2.split(img_Lab) 41''' 42 # binary transformation 43 #thre, img_greenarea = cv2.threshold(img_a, 120, 255, cv2.THRESH_BINARY_INV) 44 45 # remove very-large and very-small objects 46img_greenarea_clean = remove_objects(img_a, lower_size=300, upper_size=None) 47 48 # save 49 # cv2.imwrite('output.beans_field.cleanobj.before.jpg', img_greenarea) 50cv2.imwrite('21/clean.jpg', img_greenarea_clean)
エラー
File "yobi.py", line 46, in <module> img_greenarea_clean = remove_objects(img_a, lower_size=300, upper_size=None) File "yobi.py", line 7, in remove_objects nlabels, labels, stats, centroids = cv2.connectedComponentsWithStats(img) cv2.error: OpenCV(4.1.0) C:\projects\opencv-python\opencv\modules\imgproc\src\connectedcomponents.cpp:3927: error: (-215:Assertion failed) L.channels() == 1 && I.channels() == 1 in function 'cv::connectedComponents_sub1'

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/01/24 08:19
2020/01/24 08:27