梅林网站建设,自己做的网站怎么接入网页游戏,产品设计ppt案例,福田附近做网站公司python-opencv划痕检测-续
这次划痕检测#xff0c;是上一次划痕检测的续集。
处理的图像如下#xff1a;
这次划痕检测#xff0c;我们经过如下几步: 第一步#xff1a;读取灰度图像 第二步#xff1a;进行均值滤波 第三步#xff1a;进行图像差分 第四步#xff1…python-opencv划痕检测-续
这次划痕检测是上一次划痕检测的续集。
处理的图像如下
这次划痕检测我们经过如下几步: 第一步读取灰度图像 第二步进行均值滤波 第三步进行图像差分 第四步阈值分割 第五步轮廓检测 第六步绘制轮廓并将过滤面积较小的轮廓且进行轮廓填充
代码如下
import cv2
import copy
import math
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
import ospathrsta.bmpimgcv2.imread(path)def histogram_equalization(image):gray imageequalized cv2.equalizeHist(gray)return equalized# 图像去噪 - 高斯滤波
def gaussian_filtering(image):blurred cv2.GaussianBlur(image, (3, 3), 0)return blurred#imggaussian_filtering(img)#img histogram_equalization(img)
img_graycv2.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_diffencecv2.subtract(img_mean_3,img_gray)img_diffence1img_mean_3-img_grayplt.subplot(131)
plt.imshow(img_diffence,gray)
plt.title(img_diffence)#阈值分割_,img_binarycv2.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(imggrayimgimg_binarycout,hicv2.findContours(grayimg,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)#hierarchy 轮廓层级关系
resultnp.zeros(img.shape,np.uint8)#绘制轮廓边框
for i in range(len(cout)):momscv2.moments(cout[i])#计算轮廓的矩areamoms[m00]#面积if area50 and area1000:cv2.drawContours(result,cout,i,(0,0,255),thicknesscv2.FILLED,hierarchyhi,maxLevel0)cv_show(result,result)os.system(pause)结果如下