绘制面积大于阈值的连通域轮廓边界
import cv2
import numpy as np
img=cv2.imread('21biyunsuan14.bmp', 0)
print(np.shape(img))
Laplacian = cv2.Laplacian(img,cv2.CV_64F)
Laplacian = cv2.convertScaleAbs(Laplacian)
_, labels, stats, centroids = cv2.connectedComponentsWithStats(Laplacian)
print(centroids)
print("123")
print("stats = ", stats)
i = 0
for istat in stats:
if istat[4] < 10500:
print(istat[0:2])
if istat[3] > istat[4]:
r = istat[3]
else:
r = istat[4]
cv2.rectangle(Laplacian, tuple(istat[0:2]), tuple(istat[0:2] + istat[2:4]), (0, 0, 255), thickness=-1)
i = i + 1
cv2.imwrite('105.jpg',Laplacian)
补充:绘制输出面积大于阈值的连通域实心轮廓
import cv2
import numpy as np
img=cv2.imread('biyunsuan8.bmp', 0)
print(np.shape(img))
thres_label = 100
new_label = np.zeros(img.shape,np.uint8)
_,contours,_ = cv2.findContours(img,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
n=len(contours)
for i in range(n):
temp = np.zeros(img.shape,np.uint8)
temp[img == i] = i
for num,values in enumerate(contours):
if cv2.contourArea(values) < thres_label:
cv2.drawContours(temp,contours,num,0,thickness=-1)
new_label += temp
img1 = new_label
cv2.imwrite('mianjijiance06.jpg',img1)