python opencv轮廓检测_图像python中的圆形轮廓检测opencv

我想有一个与色彩分割阈值问题,所以这里的想法是生成二进制掩码。通过检查您感兴趣的区域似乎比输入图像的其他区域更亮,因此可以在grayScale图像上简单地完成阈值处理以简化上下文。 注意:您可以按照您的要求更改此步骤。在满足阈值输出后,您可以使用cv2.convexHull()来获得轮廓的凸面形状。

还要记住选择最大的轮廓并忽略小轮廓。下面的代码可以用来生成所需的输出:只有红色的平面上进行,当

import cv2

import numpy as np

# Loading the input_image

img = cv2.imread("/Users/anmoluppal/Downloads/3xGG4.jpg")

# Converting the input image to grayScale

img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Thresholding the image to get binary mask.

ret, img_thresh = cv2.threshold(img_gray, 145, 255, cv2.THRESH_BINARY)

# Dilating the mask image

kernel = np.ones((3,3),np.uint8)

dilation = cv2.dilate(img_thresh,kernel,iterations = 3)

# Getting all the contours

_, contours, __ = cv2.findContours(dilation, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

# Finding the largest contour Id

largest_contour_area = 0

largest_contour_area_idx = 0

for i in xrange(len(contours)):

if (cv2.contourArea(contours[i]) > largest_contour_area):

largest_contour_area = cv2.contourArea(contours[i])

largest_contour_area_idx = i

# Get the convex Hull for the largest contour

hull = cv2.convexHull(contours[largest_contour_area_idx])

# Drawing the contours for debugging purposes.

img = cv2.drawContours(img, [hull], 0, [0, 255, 0])

cv2.imwrite("./garbage.png", img)

HMfQU.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值