利用OpenCV进行轮廓识别

2024-03-29 08:20:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个图像中的对象集合。 检查示例输入图像here。在

我想找出每个物体的轮廓。 我按照下面的方法使用OpenCV2来识别轮廓

gray = cv2.cvtColor(input_image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (7, 7), 0)
edged = cv2.Canny(gray, 50, 100)
dilate= cv2.dilate(edged, None, iterations=1)
erode= cv2.erode(dilate, None, iterations=1)
cnts = cv2.findContours(erode, cv2.RETR_EXTERNAL,
        cv2.CHAIN_APPROX_SIMPLE)

这是上面代码的轮廓输出:see output image

有没有更好的方法来识别图像中的物体?在


Tags: 对象方法图像imagenone示例herecv2
1条回答
网友
1楼 · 发布于 2024-03-29 08:20:55

您错过了代码片段中的一个简单步骤,cv2.findContours()对二进制图像效果最好,但您只是将灰度图像传递给cv2.findContours。我按照以下步骤从背景中分割出苹果:

第一步:分割出主要包含灰度像素的背景

您可以在这里使用HSV color domain,其中饱和度较低的背景将被分割为:

img_hsv = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2HSV_FULL)

# Filter out low saturation values, which means gray-scale pixels(majorly in background)
bgd_mask = cv2.inRange(img_hsv, np.array([0, 0, 0]), np.array([255, 30, 255]))

enter image description here

第2步:对于深黑像素,饱和度值是突然的,所以我们分割了极端的黑白像素:

^{pr2}$

Black pixels maskWhite pixels mask

步骤3:合并这些掩码以获得cv2.findContours的最终掩码:

final_mask = cv2.max(bgd_mask, black_pixels_mask)
final_mask = cv2.min(final_mask, ~white_pixels_mask)
final_mask = ~final_mask

Merged mask

第四步:现在要填补这些漏洞,我们要腐蚀和放大图像:

final_mask = cv2.erode(final_mask, np.ones((3, 3), dtype=np.uint8))
final_mask = cv2.dilate(final_mask, np.ones((5, 5), dtype=np.uint8))

enter image description here

第5步:使用cv2.findContours()获得轮廓,并对区域进行过滤以删除较小的轮廓:

# Now you can finally find contours.
im, contours, hierarchy = cv2.findContours(final_mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

final_contours = []
for contour in contours:
    area = cv2.contourArea(contour)
    if area > 2000:
        final_contours.append(contour)

第6步:显示最终轮廓

Final output

以下是完整的代码片段:

import cv2
import numpy as np

img_bgr = cv2.imread("/home/anmol/Downloads/tWuTW.jpg")
img_hsv = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2HSV_FULL)

# Filter out low saturation values, which means gray-scale pixels(majorly in background)
bgd_mask = cv2.inRange(img_hsv, np.array([0, 0, 0]), np.array([255, 30, 255]))

# Get a mask for pitch black pixel values
black_pixels_mask = cv2.inRange(img_bgr, np.array([0, 0, 0]), np.array([70, 70, 70]))

# Get the mask for extreme white pixels.
white_pixels_mask = cv2.inRange(img_bgr, np.array([230, 230, 230]), np.array([255, 255, 255]))

final_mask = cv2.max(bgd_mask, black_pixels_mask)
final_mask = cv2.min(final_mask, ~white_pixels_mask)
final_mask = ~final_mask

final_mask = cv2.erode(final_mask, np.ones((3, 3), dtype=np.uint8))
final_mask = cv2.dilate(final_mask, np.ones((5, 5), dtype=np.uint8))

# Now you can finally find contours.
im, contours, hierarchy = cv2.findContours(final_mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

final_contours = []
for contour in contours:
    area = cv2.contourArea(contour)
    if area > 2000:
        final_contours.append(contour)


for i in xrange(len(final_contours)):
    img_bgr = cv2.drawContours(img_bgr, final_contours, i, np.array([50, 250, 50]), 4)


debug_img = img_bgr
debug_img = cv2.resize(debug_img, None, fx=0.3, fy=0.3)
cv2.imwrite("./out.png", debug_img)

相关问题 更多 >