【发布时间】:2020-02-13 16:31:28
【问题描述】:
我正在尝试检测这样的照片上的黑色形状。
到目前为止,我有形状的图片,但仍然有很多线条和噪音,因此我无法使用 findContours(),因为它也标记了线条。你能给我一些建议或帮助完成这项任务吗?我将非常感谢您的帮助!
原图
二值图像
import cv2
import numpy as np
import imutils
def color_seg(choice):
if choice == 'blue':
lower_hue = np.array([100,30,30])
upper_hue = np.array([150,148,255])
elif choice == 'white':
lower_hue = np.array([0,0,0])
upper_hue = np.array([0,0,255])
elif choice == 'black':
lower_hue = np.array([0,0,0])
upper_hue = np.array([50,50,100])
return lower_hue, upper_hue
# Take each frame
frame = cv2.imread('11.jpg')
#frame = cv2.imread('images/road_1.jpg')
frame = imutils.resize(frame, height = 500)
chosen_color = 'black'
# Convert BGR to HSV
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# define range of a color in HSV
lower_hue, upper_hue = color_seg(chosen_color)
# Threshold the HSV image to get only blue colors
mask = cv2.inRange(hsv, lower_hue, upper_hue)
kernel = np.ones((5,5),np.uint8)
erosion = cv2.erode(mask,kernel,iterations = 10)
erosion = cv2.filter2D(mask,-1,kernel)
erosion = cv2.GaussianBlur(mask,(5,5),cv2.BORDER_DEFAULT)
cv2.imshow('frame',frame)
cv2.imshow('mask',mask)
cv2.waitKey(0)
【问题讨论】:
-
建议:找到轮廓。然后测量圆度 (programcreek.com/python/example/89409/cv2.fitEllipse) 或偏心率 (python.hotexamples.com/examples/cv2/-/fitEllipse/…) 并过滤更圆(圆度或偏心率接近 1)且面积在合理范围内的圆度
标签: python numpy opencv image-processing computer-vision