裂缝二值图面积计算与个数检测

裂缝面积

总面积计算

这一部分主要面对网状裂缝,上一节判断裂缝走势的时候,我们就讲到了可以使用裂缝面积去衡量受损程度,而想网状裂缝这种明显需要修补的,其可以求解的也只有面积了。

请需要明白的是一点是这里统计计算的是图像像素个数,实际还要涉及到相机参数,本文这里暂不考虑。

import cv2
import numpy as np
import pyzjr as pz

path = r"D:\PythonProject\RoadCrack\dimension2_data\num\001.png"
img  = cv2.imread(path)
thresh = pz.BinaryImg(img)

image_array = np.array(thresh)
white_pixels = np.count_nonzero(image_array)
print(f"图像中裂缝(白色)的个数:{white_pixels}")

e0014c3c50784e8da65cedce47fbdf97.png

图像中裂缝(白色)的个数:1831

后面我用ps统计了一次,与计算所得符合。np.count_nonzero这个函数是用于统计数组中非零元素的数量,在opencv中你可以使用cv2.countNonZero()进行替代,效果都是相同的。

每条裂缝面积

在这里我想采用大写字母和每条裂缝面积构成字典结构,在我看来这已经足够用了,毕竟如果裂缝多了那就怎么也得判断为网状裂缝。

import cv2
import numpy as np
import pyzjr as pz
from skimage import morphology
from skimage import measure

Bbox = pz.Boundingbox()

path = r"D:\PythonProject\RoadCrack\dimension2_data\num\046.png"
img  = cv2.imread(path)
thresh = pz.BinaryImg(img)

merge_threshold=3
area_threshold=50
connected_image = morphology.closing(thresh, morphology.disk(merge_threshold))
labeled_image = measure.label(connected_image, connectivity=2)
region_props = measure.regionprops(labeled_image)
area = {}
crack_label = ord('A')
for region in region_props:
    area_value = region.area
    if area_value >= area_threshold:
        if crack_label <= ord('Z'):
            area[chr(crack_label)] = area_value
            crack_label += 1

print(area)

{'A': 623, 'B': 781, 'C': 226}

在这里很轻松的就能判定每条裂缝的对应的面积。原图如下所示:

05da5c209eb045e1a5ae5a28de87b4c2.png

这里原本是有四条的,但经过闭运算后,就将中间的两条合并在了一起。

label这个函数可以用于标记整数数组的连接区域,所以我们需要对图片进行二值化,我并不希望有其他的颜色混入,connectivity指考虑像素之间的连接性,这个可以看API里面是怎么解释的:

78b03952c3a444e58d61e5b852e14721.png

很显然,我希望的是八连通的区域。

regionprops返回的是一个列表,其中的每个元素表示一个连通区域的属性。每个元素是一个对象,包含了与该连通区域相关的各种属性信息,例如面积、质心坐标、外接矩形框、周长等。

个数检测

个数检测就想对比较的简单了,因为我们已经求出了每条对应的裂缝面积,并用标签进行了标记。

len(area)

可视化

怎么在原图上显示呢?怎么检测到我所获得的是正确的呢?你可以想下面这样:

0d73c9cdc95e430a929076973025ef4e.png

A中间有一点小间隔,但实际经过闭运算后,这条裂缝是连接在一起的,而C裂缝虽然是分开的,但分开部分的面积小于了设定的50,所以也没有算进去。 

import cv2
import numpy as np
import pyzjr as pz
from skimage import morphology
from skimage import measure

Bbox = pz.Boundingbox()

path = r"D:\PythonProject\RoadCrack\dimension2_data\num\046.png"
img  = cv2.imread(path)
draw_img = img.copy()
thresh = pz.BinaryImg(img)

merge_threshold=3
area_threshold=50
connected_image = morphology.closing(thresh, morphology.disk(merge_threshold))
labeled_image = measure.label(connected_image, connectivity=2)
region_props = measure.regionprops(labeled_image)
area = {}
crack_label = ord('A')
for region in region_props:
    area_value = region.area
    if area_value >= area_threshold:
        minr, minc, maxr, maxc = region.bbox
        Bbox.putBoxText(draw_img, [minc, minr, maxc, maxr], chr(crack_label), mode=0)
        if crack_label <= ord('Z'):
            area[chr(crack_label)] = area_value
            crack_label += 1

print(area)
cv2.imshow("detect crack nums",draw_img)
cv2.waitKey(0)

 

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夏天是冰红茶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值