python-opencv划痕检测-续

python-opencv划痕检测-续

这次划痕检测,是上一次划痕检测的续集。

处理的图像如下:
在这里插入图片描述

这次划痕检测,我们经过如下几步:
第一步:读取灰度图像
第二步:进行均值滤波
第三步:进行图像差分
第四步:阈值分割
第五步:轮廓检测
第六步:绘制轮廓,并将过滤面积较小的轮廓,且进行轮廓填充

代码如下:

import cv2
import copy
import math
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
import os


path=r'sta.bmp'

img=cv2.imread(path)

def histogram_equalization(image):
    gray = image
    equalized = cv2.equalizeHist(gray)
    return equalized

# 图像去噪 - 高斯滤波
def gaussian_filtering(image):
    blurred = cv2.GaussianBlur(image, (3, 3), 0)
    return blurred


#img=gaussian_filtering(img)

#img = histogram_equalization(img)
img_gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)




def cv_show(name,img):
    cv2.imshow(name,img)
    #cv2.waitKey(0),接收0,表示窗口暂停
    cv2.waitKey(0)
    #销毁所有窗口
    cv2.destroyAllWindows()



img_mean_3 = cv2.blur(img_gray, (10, 10))

#图像差分
img_diffence=cv2.subtract(img_mean_3,img_gray)

img_diffence1=img_mean_3-img_gray

plt.subplot(131)
plt.imshow(img_diffence,'gray')
plt.title('img_diffence')


#阈值分割

_,img_binary=cv2.threshold(img_diffence,5,255,cv2.THRESH_BINARY_INV)
plt.subplot(132)
plt.imshow(img_binary,'gray')
plt.title('img_binary')

plt.show()
#cv_show('img

grayimg=img_binary



cout,hi=cv2.findContours(grayimg,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

#hierarchy 轮廓层级关系
result=np.zeros(img.shape,np.uint8)

#绘制轮廓边框
for  i in range(len(cout)):
    moms=cv2.moments(cout[i])#计算轮廓的矩
    area=moms['m00']#面积
    if area>50 and area<1000:
            cv2.drawContours(result,cout,i,(0,0,255),thickness=cv2.FILLED,hierarchy=hi,maxLevel=0)


cv_show('result',result)

os.system("pause")

结果如下:
在这里插入图片描述
在这里插入图片描述

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用OpenCV进行划痕检测Python代码中,可以使用以下代码来找到图像中的轮廓和层次结构:\[1\] ```python im2, contours, hierarchy = cv2.findContours(result.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) ``` 其中,`result`是经过边缘检测后的图像。通过`cv2.findContours()`函数,可以得到图像中所有轮廓的坐标和层次结构。 对于划痕检测和分类,可以采用以下简单思路:\[2\] 1. 通过边缘检测找到每个工件的轮廓,并计算出工件的中心来标记工件的ID。 2. 根据工件的每一帧位移量来确定是否为同一个工件。 3. 将每一个工件截取出来,进行缺陷的提取。 4. 对提取的缺陷进行直方图计算,通过直方图来进行值归一化。 5. 根据缺陷的颜色值分布来进行分类。 在实际应用中,如果得到的图像阈值不太理想,可以使用动态阈值分割法来提取特征。该方法首先对图像进行均值滤波,然后与现有图像最差后进行阈值分割。这种方法适用于较小的特征提取,例如金属表面的划痕、丝网的漏洞等。\[3\] #### 引用[.reference_title] - *1* [pythonOpenCV检测水果上的划痕](https://blog.csdn.net/weixin_39916549/article/details/111013424)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [基于python-opencv实现木质工件的污渍和划痕识别和分类](https://blog.csdn.net/weixin_44736584/article/details/105737739)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [opencv检测划痕](https://blog.csdn.net/yuelcay/article/details/77879484)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值