使用OpenCV识别轮廓

Ray*_* Ck 3 python opencv computer-vision opencv-contour

我在图像中有对象集合。在此处检查样本输入图像。

我想找到每个对象的轮廓。我正在按照以下方法使用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)
Run Code Online (Sandbox Code Playgroud)

这是上面代码的轮廓输出:请参见输出图像

有没有更好的方法来识别图像中的对象?

Zda*_*daR 7

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

步骤1:分割出主要包含灰度像素的背景。

您可以在此处使用HSV色域,其中较低的饱和度值会将背景分割为:

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]))
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

第2步:对于黑色像素,饱和度值突然变化,因此我们将极端的黑色和白色像素细分:

# 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]))
Run Code Online (Sandbox Code Playgroud)

黑色像素遮罩 白色像素遮罩

第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
Run Code Online (Sandbox Code Playgroud)

合并面膜

步骤4:现在填充孔,我们侵蚀并扩大图像:

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))
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

第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)
Run Code Online (Sandbox Code Playgroud)

步骤6:显示最终轮廓

最终输出

这是完整的代码段:

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)
Run Code Online (Sandbox Code Playgroud)