运用measure.regionprops
import cv2
import numpy as np
from skimage.measure import label,regionprops
import math
def detection(c):
perimeter = cv2.arcLength(c, True)
approximate = cv2.approxPolyDP(c, 0.04*perimeter, True)
return approximate
def hough_detection(img):
img = img.astype(np.uint8)
gray_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
_, thre_img = cv2.threshold(gray_img, 253, 255, cv2.THRESH_BINARY_INV)
circleDetect2 = cv2.HoughCircles(gray_img, cv2.HOUGH_GRADIENT, 1, 100, param1=40, param2=15, minRadius=30,
maxRadius=100)
circles = circleDetect2[0, :, :]
for i in circles[:]:
cv2.circle(img, (i[0], i[1]), i[2], (255, 0, 0), 2)
# cv2.circle(img, (i[0], i[1]), 1, (255, 0, 0), 2)
# cv2.imwrite('cleaning.png', img)
return img
def find_contours(img):
img = img.astype(np.uint8)
gray_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
result_img = gray_img.copy()
crop_img = np.zeros([gray_img.shape[0], gray_img.shape[1]], np.uint8)
# gray_img = cv2.blur(gray_img, (3, 3))
_, thre_img = cv2.threshold(gray_img, 210, 255, cv2.THRESH_BINARY_INV)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
thre_img = cv2.morphologyEx(thre_img, cv2.MORPH_OPEN, kernel)
im_floodfill = thre_img.copy()
# Mask used to flood filling.
# Notice the size needs to be 2 pixels than the image.
h, w = thre_img.shape[:2]
mask = np.zeros((h + 2, w + 2), np.uint8)
# Floodfill from point (0, 0)
cv2.floodFill(im_floodfill, mask, (0, 0), 255);
# Invert floodfilled image
im_floodfill_inv = cv2.bitwise_not(im_floodfill)
im_out = thre_img | im_floodfill_inv
#_, contours, hierachy = cv2.findContours(thre_img(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
'''contours, hierachy = cv2.findContours(thre_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
contours = sorted(contours, key=cv2.contourArea, reverse=True)
# img = hough_detection(img)
for idx, cnt in enumerate(contours):
M = cv2.moments(cnt) #像像矩
cnt_area = cv2.contourArea(cnt) #廓廓积积
(x, y), radius = cv2.minEnclosingCircle(cnt) #最小外接圆
circle_area = np.pi * radius * radius
ratio = cnt_area / circle_area
if ratio >= 0.7:
cnt = cnt.reshape(-1, 2)
min = np.min(cnt[:, 1])
max = np.max(cnt[:, 1])
for y in range(min+1, max):
indices = np.argwhere(cnt[:, 1] == y)
indices = indices.reshape(-1)
contour = cnt[indices]
index = np.argsort(contour[:, 0])
x_min = np.min(contour[:, 0])
x_max = np.max(contour[:, 0])
for x in range(x_min, x_max+1):
result_img[y, x] = result_img[y, x] + (255 - gray_img[y, x])
(x, y, radius) = np.int0((x, y, radius)) # 圆心和半径取整
cv2.circle(img, (x, y), radius, (0, 0, 255), 2)'''
label_img = label(im_out, connectivity=2)
props = regionprops(label_img)
ind=[]
area=[]
for idx, prop in enumerate(props):
# for prop in props:
#ind = []
p = prop.area
per = prop.perimeter
#coo=prop.coords
roundness = 4 * math.pi * p / (per * per)
if roundness >= 0.6 and p>5000:
ind.append(idx)
area.append(p)
print(area)
for i in range(len(ind)):
coo=props[ind[i]].coords
y=coo[:,1]
x=coo[:,0]
for j in range(len(coo)):
im_out[x[j]][y[j]]=0
np.savetxt('new.csv', coo, delimiter=',')
#print(coo)
#thre_img[]
#result_img=np.where(a > 0, a, 0)
#if roundness >= 0.7:
#thre_img[coo]=0
cv2.imwrite('RemoveResult.PNG', im_out)
cv2.imwrite('contours.png', img)
if name == 'main':
img = cv2.imread('./image/13-338~A.004.A.TIF')
find_contours(img)
https://scikit-image.org/docs/dev/api/skimage.measure.html#skimage.measure.regionprops