赤峰网站建设培训,建筑资格证书查询官网,怎样做网站性能优化,企业网站优化方案案例python-opencv划痕检测
这次实验#xff0c;我们将对如下图片进行划痕检测#xff0c;其实这个比较有难度#xff0c;因为清晰度太差了。
我们做法如下#xff1a; #xff08;1#xff09;读取图像为灰度图像#xff0c;进行自适应直方图均衡化处理#xff0c;增强图…python-opencv划痕检测
这次实验我们将对如下图片进行划痕检测其实这个比较有难度因为清晰度太差了。
我们做法如下 1读取图像为灰度图像进行自适应直方图均衡化处理增强图片对比度 2然后进行三次图像去噪 - 高斯滤波 3然后我们再进行一次直方图均衡操作增强图片 4然后使用canny进行边缘检测 5最后使用霍夫曼直线检测检测划痕线段并绘制 代码实现如下
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 cv_show(name,img):cv2.imshow(name,img)#cv2.waitKey(0),接收0表示窗口暂停cv2.waitKey(0)#销毁所有窗口cv2.destroyAllWindows()def histogram_equalization(image):gray imageequalized cv2.equalizeHist(gray)return equalizedcv_show(img,img)
# 图像去噪 - 高斯滤波
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)kernelnp.ones((3,3),np.uint8)rows img_gray.shape[0] # rows、cols 行列数rows 也就是高度
cols img_gray.shape[1]#for i in range(rows):
# for j in range(cols):
# print(img_gray[i][j])
#dilate_imgcv2.dilate(img_gray,kernelkernel,iterations1)
#ret,dst1cv2.threshold(img_gray,135,255,cv2.THRESH_BINARY)clahe cv2.createCLAHE(5,(3,3))
dst clahe.apply(img_gray)
for i in range(3):dstgaussian_filtering(dst)
dsthistogram_equalization(dst)
cv_show(dst,dst)kernelnp.ones((3,3),np.uint8)kernelnp.ones((3,3),np.uint8)
kernelkernel5img_graydst# 转换成灰度图
gray img_gray
# 边缘检测, Sobel算子大小为3
edges cv2.Canny(gray, 150, 185, apertureSize3)
orgb cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
oShow orgb.copy()
# 霍夫曼直线检测
lines cv2.HoughLinesP(edges,1,np.pi/180,10,minLineLength60,maxLineGap6)
#遍历
l lines[:,0,:]
for x1,y1,x2,y2 in l [:]: cv2.line(orgb,(x1,y1),(x2,y2),(0,0,255),1)
#展示
plt.subplot(121)
plt.imshow(gray,gray)
plt.axis(off)
plt.subplot(122)
plt.imshow(orgb,gray)
plt.axis(off)plt.show()os.system(pause)检测结果如下 有一点效果但不多。