Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 基于OpenCV的轮廓识别_Python_Opencv_Computer Vision_Opencv Contour - Fatal编程技术网

Python 基于OpenCV的轮廓识别

Python 基于OpenCV的轮廓识别,python,opencv,computer-vision,opencv-contour,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(dil

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

我想找出每个物体的轮廓。 我遵循以下方法使用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)
这是我得到的上述代码的轮廓输出:


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

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

第一步:分割出主要包含灰度像素的背景。 您可以在此处使用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]))

步骤2:对于沥青黑色像素,饱和度值是突然的,因此我们分割了极端的黑色和白色像素:

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

第四步:现在,为了填补空洞,我们腐蚀并放大图像:

第5步:使用
cv2.findContours()
获取轮廓,并在区域上对其进行过滤,以删除较小的轮廓: 步骤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)

您可以尝试选择与apple“sure interior”相似的超级像素,并拒绝与apple“sure exterior”相似的超级像素,您可以通过一些预处理和阈值粗略检测这些像素。自动化的方式是。为了获得更好的质量,搜索用于对象语义分割的神经网络。例如,在我早期的一个项目中,我编写了一个应用程序,在该应用程序中,我可以使用鼠标手动选择/取消选择超级像素来修复错误的超级像素分割,并保存生成的二进制掩码。在我有了大约一百个基本事实示例(图像和二进制掩码)之后,我用图像作为输入,掩码作为输出,训练了pip2pix神经网络。这是一个很棒的方法+谢谢你的回答。我已经检查了另一张图像,但是轮廓检测不好。你能帮忙吗?对于参考,请检查输入和输出图像:我刚刚为您提供了一个起点,显然,您需要对许多不同的图像进行实验,以获得此脚本的一些通用值,我只调整了给定图像的值。您可以从这里获取概念,并根据需要调整值。主要的收获是:因为你的背景是黑色的,所以试着先把它分割出来,而不是把苹果分割出来。您也可以尝试其他颜色域,如YCrCb、LAB等。谢谢您的建议。我会尝试其他方法。
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)
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)