【发布时间】:2021-04-04 00:55:09
【问题描述】:
我试图通过使用连接的 cv2.connectedComponentsWithStats 仅获取此图像中的中心 retangle。但是我不知道如何只获取中心图像。
我的尝试是这样的:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, bw = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
img = cv2.threshold(bw, 127, 255, cv2.THRESH_BINARY)[1] # ensure binary
def undesired_objects(image):
image = image.astype('uint8')
nb_components, output, stats, centroids = cv2.connectedComponentsWithStats(image, connectivity=4)
sizes = stats[:, -1]
max_label = 1
max_size = sizes[1]
for i in range(2, nb_components):
if sizes[i] > max_size:
max_label = i
max_size = sizes[i]
img2 = np.zeros(output.shape)
img2[output == max_label] = 255
cv2.imshow("Biggest component", img2)
cv2.waitKey()
undesired_objects(img)
【问题讨论】:
标签: python image opencv object-detection