dst = cv2.Sobel(src, ddepth, dx, dy, ksize)算法
1 # *******************圖像梯度算法**********************開始 2 import cv2 3 # import numpy as np 4 5 img = cv2.imread('pie.png',cv2.IMREAD_GRAYSCALE) 6 cv2.imshow("img",img) 7 cv2.waitKey() 8 cv2.destroyAllWindows() 9 10 # 顯示圖像函數 11 def cv_show(img,name): 12 cv2.imshow(name,img) 13 cv2.waitKey() 14 cv2.destroyAllWindows() 15 16 # Sobel算子——x軸 17 sobelx = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=3) # 計算水平的 18 cv_show(sobelx,'sobelx') 19 20 # 白到黑是正數,黑到白就是負數了,全部的負數會被截斷成0,因此要取絕對值 21 sobelx = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=3) 22 sobelx = cv2.convertScaleAbs(sobelx) # 取絕對值 23 cv_show(sobelx,'sobelx') 24 25 # Sobel算子——y軸 26 sobely = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=3) 27 sobely = cv2.convertScaleAbs(sobely) # 取絕對值 28 cv_show(sobely,'sobely') 29 30 # 求和 31 sobelxy = cv2.addWeighted(sobelx,0.5,sobely,0.5,0) # 按權重計算 32 cv_show(sobelxy,'sobelxy') 33 34 # 也有直接計算xy軸的————不推薦使用 35 # sobelxy=cv2.Sobel(img,cv2.CV_64F,1,1,ksize=3) 36 # sobelxy = cv2.convertScaleAbs(sobelxy) 37 # cv_show(sobelxy,'sobelxy') 38 # *******************圖像梯度算法**********************結束
用lena圖像來實際操做一下:app
1 # *******************圖像梯度算法-實際操做**********************開始 2 import cv2 3 4 # 顯示圖像函數 5 def cv_show(img,name): 6 cv2.imshow(name,img) 7 cv2.waitKey() 8 cv2.destroyAllWindows() 9 10 img = cv2.imread('lena.jpg',cv2.IMREAD_GRAYSCALE) 11 cv_show(img,'img') 12 13 # 分別計算x和y 14 img = cv2.imread('lena.jpg',cv2.IMREAD_GRAYSCALE) 15 sobelx = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=3) 16 sobelx = cv2.convertScaleAbs(sobelx) 17 sobely = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=3) 18 sobely = cv2.convertScaleAbs(sobely) 19 sobelxy = cv2.addWeighted(sobelx,0.5,sobely,0.5,0) 20 cv_show(sobelxy,'sobelxy') 21 # *******************圖像梯度算法-實際操做**********************結束
(1)Scharr算子函數
(2)Laplacian算子spa
(3)不一樣算子之間的差距3d
1 # *******************圖像梯度算子-Scharr+laplacian**********************開始 2 import cv2 3 import numpy as np 4 5 #不一樣算子的差別 6 img = cv2.imread('lena.jpg',cv2.IMREAD_GRAYSCALE) 7 sobelx = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=3) 8 sobely = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=3) 9 sobelx = cv2.convertScaleAbs(sobelx) 10 sobely = cv2.convertScaleAbs(sobely) 11 sobelxy = cv2.addWeighted(sobelx,0.5,sobely,0.5,0) 12 13 scharrx = cv2.Scharr(img,cv2.CV_64F,1,0) 14 scharry = cv2.Scharr(img,cv2.CV_64F,0,1) 15 scharrx = cv2.convertScaleAbs(scharrx) 16 scharry = cv2.convertScaleAbs(scharry) 17 scharrxy = cv2.addWeighted(scharrx,0.5,scharry,0.5,0) 18 19 laplacian = cv2.Laplacian(img,cv2.CV_64F) 20 laplacian = cv2.convertScaleAbs(laplacian) 21 22 res = np.hstack((sobelxy,scharrxy,laplacian)) 23 24 # 顯示圖像函數 25 def cv_show(img,name): 26 cv2.imshow(name,img) 27 cv2.waitKey() 28 cv2.destroyAllWindows() 29 cv_show(res,'res') 30 # *******************圖像梯度算子-Scharr+laplacian**********************結束
1) 使用高斯濾波器,以平滑圖像,濾除噪聲。code
2) 計算圖像中每一個像素點的梯度強度和方向。blog
3) 應用非極大值(Non-Maximum Suppression)抑制,以消除邊緣檢測帶來的雜散響應。索引
4) 應用雙閾值(Double-Threshold)檢測來肯定真實的和潛在的邊緣。ci
5) 經過抑制孤立的弱邊緣最終完成邊緣檢測。it
1 # *******************邊緣檢測**********************開始 2 import cv2 3 import numpy as np 4 5 img=cv2.imread("lena.jpg",cv2.IMREAD_GRAYSCALE) 6 7 v1=cv2.Canny(img,80,150) # 設置雙閾值 最小和最大 8 v2=cv2.Canny(img,50,100) 9 10 res = np.hstack((v1,v2)) 11 12 # 顯示圖像函數 13 def cv_show(img,name): 14 cv2.imshow(name,img) 15 cv2.waitKey() 16 cv2.destroyAllWindows() 17 cv_show(res,'res') 18 # *******************邊緣檢測**********************結束
(1)高斯金字塔:向下採樣方法(縮小)
(2)高斯金字塔:向上採樣方法(放大)
1 # *******************圖像金字塔--高斯金字塔**********************開始 2 import cv2 3 import numpy as np 4 5 # 顯示圖像函數 6 def cv_show(img,name): 7 cv2.imshow(name,img) 8 cv2.waitKey() 9 cv2.destroyAllWindows() 10 11 img=cv2.imread("AM.png") 12 # cv_show(img,'img') 13 print (img.shape) 14 15 # 高斯金字塔-上採樣 (可執行屢次) 16 up=cv2.pyrUp(img) 17 # cv_show(up,'up') 18 print (up.shape) 19 20 # 高斯金字塔-下采樣 (可執行屢次) 21 down=cv2.pyrDown(img) 22 # cv_show(down,'down') 23 print (down.shape) 24 25 # 高斯金字塔-先上採樣再下采樣 (會損失信息-變模糊) 26 up=cv2.pyrUp(img) 27 up_down=cv2.pyrDown(up) 28 # cv_show(up_down,'up_down') 29 cv_show(np.hstack((img,up_down)),'up_down') 30 # *******************圖像金字塔--高斯金字塔**********************結束
1 # *******************圖像金字塔-拉普拉斯金字塔**********************開始 2 import cv2 3 import numpy as np 4 5 # 顯示圖像函數 6 def cv_show(img,name): 7 cv2.imshow(name,img) 8 cv2.waitKey() 9 cv2.destroyAllWindows() 10 11 img=cv2.imread("AM.png") 12 down=cv2.pyrDown(img) 13 down_up=cv2.pyrUp(down) 14 l_1=img-down_up 15 cv_show(l_1,'l_1') 16 # *******************圖像金字塔-拉普拉斯金字塔**********************結束
cv2.findContours(img,mode,method)
mode:輪廓檢索模式
method:輪廓逼近方法
爲提升準確性,使用二值圖像。
1 # *******************圖像輪廓**********************開始 2 import cv2 3 import numpy as np 4 5 # 讀入圖像轉換爲二值圖像 6 img = cv2.imread('contours.png') 7 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 轉換爲灰度圖 8 ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY) # 轉換成二值圖 9 10 # 顯示圖像函數 11 def cv_show(img,name): 12 cv2.imshow(name,img) 13 cv2.waitKey() 14 cv2.destroyAllWindows() 15 # cv_show(thresh,'thresh') 16 17 # 輪廓檢測 第一個就是二值的結果 第二個是一堆輪廓點 第三個是層級 18 binary, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) 19 20 # 繪製輪廓 21 draw_img = img.copy() # 注意須要copy,要不原圖會變。。。 22 res = cv2.drawContours(draw_img, contours, -1, (0, 0, 255), 2) # 傳入繪製圖像,輪廓,輪廓索引(-1所有),顏色模式,線條厚度 23 # cv_show(res,'res') 24 25 draw_img = img.copy() 26 res = cv2.drawContours(draw_img, contours, 2, (0, 0, 255), 2) 27 cv_show(res,'res') 28 # *******************圖像輪廓**********************結束
1 import cv2 2 3 # 讀入圖像轉換爲二值圖像 4 img = cv2.imread('contours.png') 5 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 轉換爲灰度圖 6 ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY) # 轉換成二值圖 7 8 # 輪廓檢測 第一個就是二值的結果 第二個是一堆輪廓點 第三個是層級 9 binary, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) 10 11 # 繪製輪廓 12 draw_img = img.copy() 13 res = cv2.drawContours(draw_img, contours, 2, (0, 0, 255), 2) 14 15 # 輪廓特徵 16 cnt = contours[0] # 獲取輪廓 17 print(cv2.contourArea(cnt)) # 計算面積 18 print(cv2.arcLength(cnt, True)) # 計算周長,True表示閉合的
1 import cv2 2 3 img = cv2.imread('contours2.png') 4 # 顯示圖像函數 5 def cv_show(img,name): 6 cv2.imshow(name,img) 7 cv2.waitKey() 8 cv2.destroyAllWindows() 9 10 # 二值+輪廓檢測 11 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 12 ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY) 13 binary, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) 14 cnt = contours[0] 15 # 輪廓繪製 16 draw_img = img.copy() 17 res = cv2.drawContours(draw_img, [cnt], -1, (0, 0, 255), 2) 18 # cv_show(res,'res') 19 20 # 輪廓近似 21 epsilon = 0.05*cv2.arcLength(cnt,True) 22 approx = cv2.approxPolyDP(cnt,epsilon,True) 23 24 draw_img = img.copy() 25 res = cv2.drawContours(draw_img, [approx], -1, (0, 0, 255), 2) 26 cv_show(res,'res')
(1)邊界矩形
1 # *******************圖像輪廓-邊界矩形**********************開始 2 import cv2 3 4 # 顯示圖像函數 5 def cv_show(img,name): 6 cv2.imshow(name,img) 7 cv2.waitKey() 8 cv2.destroyAllWindows() 9 10 img = cv2.imread('contours.png') 11 12 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 13 ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY) 14 binary, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) 15 cnt = contours[0] 16 17 # 邊界矩形 18 x,y,w,h = cv2.boundingRect(cnt) 19 img = cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2) 20 cv_show(img,'img') 21 # 輪廓面積與邊界矩形比 22 area = cv2.contourArea(cnt) 23 x, y, w, h = cv2.boundingRect(cnt) 24 rect_area = w * h 25 extent = float(area) / rect_area 26 print ('輪廓面積與邊界矩形比',extent) 27 # *******************圖像輪廓-邊界矩形**********************結束
(2)外接圓
1 # 外接圓 2 (x,y),radius = cv2.minEnclosingCircle(cnt) 3 center = (int(x),int(y)) 4 radius = int(radius) 5 img = cv2.circle(img,center,radius,(0,255,0),2) 6 cv_show(img,'img')