接上次笔记,继续学习~~~
1)使用高斯滤波,以平滑图像,滤除噪声。
2)计算图像中每个像素点的梯度强度和方向。
3)应用非极大值(Non-Maximum Suppression)抑制,以消除边缘检测带来的杂度响应。
4)应用双阈值(Double-Threshold)检测来确定真实和潜在的边缘。
5)通过抑制孤立的弱边缘最终完成边缘检测。
#Canny边缘检测
img = cv2.imread('lena.jpg',cv2.IMREAD_GRAYSCALE)
v1 = cv2.Canny(img,80,150) #80,150表示自定义的minval和maxval
v2 = cv2.Canny(img,50,100)
res = np.hstack((v1,v2))
cv_show(res,'res')
img = cv2.imread('car.png',cv2.IMREAD_GRAYSCALE)
v1 = cv2.Canny(img,120,250) #80,150表示自定义的minval和maxval
v2 = cv2.Canny(img,50,100)
res = np.hstack((v1,v2))
cv_show(res,'res')
#图像金字塔-高斯金字塔
img = cv2.imread('AM.png')
cv_show(img,'img')
print(img.shape)
up = cv2.pyrUp(img)
cv_show(up,'up')
print(up.shape)
down = cv2.pyrDown(img)
cv_show(down,'down')
print(down.shape)
up2 = cv2.pyrUp(up)
cv_show(up2,'up2')
print(up2.shape)
up = cv2.pyrUp(img)
up_down = cv2.pyrDown(up)
cv_show(up_down,'up-down')
res = np.hstack((img,up_down))
cv_show(res,'res')
#图像金字塔-拉普拉斯金字塔
down = cv2.pyrDown(img)
down_up = cv2.pyrUp(down)
l_l = img-down_up
cv_show(l_l,'l_l')
cv2.findContours(img,mode,method)
mode:轮廓检索模式
cv2.findContours(img,cv2.RETR_EXTERNAL,method)
cv2.RETR_EXTERNAL:只检索最外面的轮廓;
cv2.RETR_LIST:检索所有的轮廓,并将其保存到一条链表中;
cv2.RETR_CCOMP:检索所有的轮廓,并将他们组织为两层:顶层是各部分的外部边界,第二层是空洞的边界;
cv2.RETR_TREE:检索所有的轮廓,并重构嵌套轮廓的整个层次;
methond:轮廓逼近方法
cv2.CHAIN_APPROX_NONE:以Freeman链码的方式输出轮廓,所有其他地方输出多边形(顶点的序列)。
cv2.CHAIN_APPROX_SIMPLE:压缩水平的、垂直的和斜的部分,也就是,函数只保留他们的终点部分。
为了更高的准确率,使用二值图像。
img = cv2.imread('contours.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
cv_show(thresh,'thresh')
binary,contours,hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE) #binary为二值结果,contours为一些轮廓点,即轮廓信息
#绘制轮廓
#注意需要copy,不然原图会变
draw = img.copy()
#传入绘制图像,轮廓,轮廓索引,颜色模式,线条厚度
res = cv2.drawContours(draw,contours,-1,(0,0,253),2)
cv_show(res,'res')
#轮廓特征
cnt = contours[0]
print(cv2.contourArea(cnt)) #面积
print(cv2.arcLength(cnt,True)) #周长,TRUE表示闭合
轮廓近似
#轮廓近似
img = cv2.imread('contours2.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
binary,contours,hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
cnt = contours[0]
draw_img = img.copy()
res = cv2.drawContours(draw_img,contours,-1,(0,0,255),2)
cv_show(res,'res')
epsilon = 0.1*cv2.arcLength(cnt,True)
approx = cv2.approxPolyDP(cnt,epsilon,True)
draw_img = img.copy()
res = cv2.drawContours(draw_img,[approx],-1,(0,0,255),2)
cv_show(res,'res')
#边界矩形
img = cv2.imread('contours.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
binary,contours,hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
cnt = contours[0]
x,y,w,h = cv2.boundingRect(cnt)
img = cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
cv_show(img,'img')
area = cv2.contourArea(cnt)
x,y,w,h = cv2.boundingRect(cnt)
rect_area = w * h
extent = float(area) / rect_area
print("轮廓面积与边界矩形比:",extent)
#外接圆
(x,y),radius = cv2.minEnclosingCircle(cnt)
center = (int(x),int(y))
radius = int (radius)
img = cv2.circle(img,center,radius,(0,255,0),2)
cv_show(img,'img')
模板匹配和卷积原理很像,模板在原图像上从原点开始滑动,计算机模板与(图像被模板覆盖的地方)的差别程度,这个差别程度的计算方法在opencv里有6种,然后将每次计算的结果放入一个矩阵里,作为结果输出,例如原图像是AxB的大小,而模板是axb大小,则输出结果的矩阵是(A-a+1)x(B-b+1)
cv2.TM_SQDIFF:计算平方不同,计算出来的值越小,越相关
cv2.TM_CCORR:计算相关性,计算出来的值越大,越相关
cv2.TM_CCOEFF:计算相关系数,计算出来的值越大,越相关
cv2.TM_SQDIFF_NORMED:计算归一化平方不同,计算出来的值越接近0,越相关
cv2.TM_CCORR_NORMED:计算归一化相关性,计算出来的值越接近1,越相关
cv2.TM_CCOEFF_NORMED:计算归一化相关系数,计算出来的值越接近1,越相关
#模板匹配
img = cv2.imread('lena.jpg',0)
template = cv2.imread('face.jpg',0)
h,w =template.shape[:2]
print(img.shape)
print(template.shape)
methods = {'cv2.TM_CCOEFF','cv2.TM_CCOEFF_NORMED','cv2.TM_CCORR',
'cv2.TM_CCORR_NORMED','cv2.TM_SQDIFF','cv2.TM_SQDIFF_NORMED'}
res = cv2.matchTemplate(img,template,cv2.TM_SQDIFF)
print(res.shape)
min_val,max_val,min_loc,max_loc = cv2.minMaxLoc(res)
print(min_val)
print(max_val)
print(min_loc)
print(max_loc)
#几种方法的比较
for math in methods:
img2 = img.copy()
#匹配方法的真值
method = eval(math)
print(method)
res = cv2.matchTemplate(img,template,method)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
#如果是平方匹配TM_SQDIFF或归一化平方匹配TM_SQDIFF_NORMED,取最小值
if method in [cv2.TM_SQDIFF,cv2.TM_SQDIFF_NORMED]:
top_left = min_loc
else:
top_left = max_loc
bottom_right = (top_left[0]+w,top_left[1]+h)
#画矩形
cv2.rectangle(img2,top_left,bottom_right,255,2)
plt.subplot(121),plt.imshow(res,cmap='gray')
plt.xticks([]),plt.yticks([]) #隐藏坐标轴
plt.subplot(122),plt.imshow(img2,cmap='gray')
plt.xticks([]),plt.yticks([])
plt.suptitle(math)
plt.show()
#匹配多个对象
img_rgb = cv2.imread('mario.jpg')
img_gray = cv2.cvtColor(img_rgb,cv2.COLOR_BGR2GRAY)
template = cv2.imread('mario_coin.jpg',0)
h,w = template.shape[:2]
res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
threshold = 0.8
#取匹配程度大于%80的坐标
loc = np.where(res >= threshold)
for pt in zip(*loc[::-1]): #*号表示可选参数
bottom_right = (pt[0]+w,pt[1]+h)
cv2.rectangle(img_rgb,pt,bottom_right,(0,0,255),2)
cv2.imshow('img_rgb',img_rgb)
cv2.waitKey(0)