import numpy as np
import cv2 as cv
filename = r'UcbZn.jpg'
im = cv.imread(filename)
imgray = cv.cvtColor(im, cv.COLOR_BGR2GRAY)
ret, thresh = cv.threshold(imgray, 240, 255, cv.THRESH_BINARY)
contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
# Filter -1 Filtering countours with first level with no parent
a = [[cv.contourArea(c), h, c] for c, h in zip(contours, hierarchy[0]) if h[3]==0]
a.sort(key=lambda x: -x[0])
base_contour = a[0]
# Filter-2 contours of rectangle size
res = [c for c in a if c[0] >= base_contour[0]*0.99]
count = 0
for c in res:
x,y,w,h = cv.boundingRect(c[2])
cv.imwrite('objects_{}.jpg'.format(str(count)), im[y:y+h,x:x+w, :])
count+=1
您可以尝试在图像上应用轮廓,使用二值图像cv2.THRESH_BINARY比灰度图像更有效的轮廓 -&燃气轮机;应用等高线
过滤轮廓
第一个过滤器-是第一级with no parent Contours Hierarchy
第二个过滤器-具有相同范围的矩形,偏差为1%
然后是轮廓边界框和切片图像
相关问题 更多 >
编程相关推荐