python-opencv 莫来石(连通域检测,去除小面积区域)

绘制面积大于阈值的连通域轮廓边界

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(i)
        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)

  • 4
    点赞
  • 81
    收藏
    觉得还不错? 一键收藏
  • 23
    评论
评论 23
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值