3. OpenCV-Python——圖像梯度算法、邊緣檢測、圖像金字塔與輪廓檢測與傅里恭弘=叶 恭弘變換_如何寫文案
※別再煩惱如何寫文案,掌握八大原則!
什麼是銷售文案服務?A就是幫你撰寫適合的廣告文案。當您需要販售商品、宣傳活動、建立個人品牌,撰寫廣告文案都是必須的工作。
一、圖像梯度算法
1、圖像梯度-Sobel算子
dst = cv2.Sobel(src, ddepth, dx, dy, ksize)
- ddepth:圖像的深度
- dx和dy分別表示水平和豎直方向
- ksize是Sobel算子的大小
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圖像來實際操作一下:
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 # *******************圖像梯度算法-實際操作**********************結束
2、圖像梯度-Scharr和Laplacian算子
(1)Scharr算子
(2)Laplacian算子
(3)不同算子之間的差距
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**********************結束
二、邊緣檢測
Canny邊緣檢測
-
1) 使用高斯濾波器,以平滑圖像,濾除噪聲。
-
2) 計算圖像中每個像素點的梯度強度和方向。
-
3) 應用非極大值(Non-Maximum Suppression)抑制,以消除邊緣檢測帶來的雜散響應。
-
4) 應用雙閾值(Double-Threshold)檢測來確定真實的和潛在的邊緣。
-
5) 通過抑制孤立的弱邊緣最終完成邊緣檢測。
1、高斯濾波器
2、梯度和方向
3、非極大值抑制
4、雙閾值檢測
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、高斯金字塔
(1)高斯金字塔:向下採樣方法(縮小)
(2)高斯金字塔:向上採樣方法(放大)
※教你寫出一流的銷售文案?
銷售文案是什麼?A文案是廣告用的文字。舉凡任何宣傳、行銷、販賣商品時所用到的文字都是文案。在網路時代,文案成為行銷中最重要的宣傳方式,好的文案可節省大量宣傳資源,達成行銷目的。
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 # *******************圖像金字塔--高斯金字塔**********************結束
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 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:輪廓檢索模式
- RETR_EXTERNAL :只檢索最外面的輪廓;
- RETR_LIST:檢索所有的輪廓,並將其保存到一條鏈表當中;
- RETR_CCOMP:檢索所有的輪廓,並將他們組織為兩層:頂層是各部分的外部邊界,第二層是空洞的邊界;
- RETR_TREE:檢索所有的輪廓,並重構嵌套輪廓的整個層次;
method:輪廓逼近方法
- CHAIN_APPROX_NONE:以Freeman鏈碼的方式輸出輪廓,所有其他方法輸出多邊形(頂點的序列)。
- CHAIN_APPROX_SIMPLE:壓縮水平的、垂直的和斜的部分,也就是,函數只保留他們的終點部分。
為提高準確性,使用二值圖像。
1、輪廓檢測及繪製
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 # *******************圖像輪廓**********************結束
2、輪廓特徵
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表示閉合的
3、輪廓近似
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')
五、傅里恭弘=叶 恭弘變換
https://zhuanlan.zhihu.com/p/19763358
1、傅里恭弘=叶 恭弘的作用
-
高頻:變化劇烈的灰度分量,例如邊界
-
低頻:變化緩慢的灰度分量,例如一片大海
2、濾波
-
低通濾波器:只保留低頻,會使得圖像模糊
-
高通濾波器:只保留高頻,會使得圖像細節增強
opencv中主要就是cv2.dft()和cv2.idft(),輸入圖像需要先轉換成np.float32 格式,得到的結果中頻率為0的部分會在左上角,通常要轉換到中心位置,通過shift變換。
3、傅里恭弘=叶 恭弘變換
1 # *******************傅里恭弘=叶 恭弘變換**********************開始 2 import numpy as np 3 import cv2 4 from matplotlib import pyplot as plt 5 6 img = cv2.imread('lena.jpg',0) 7 8 img_float32 = np.float32(img) 9 10 # 傅里恭弘=叶 恭弘變換 11 dft = cv2.dft(img_float32, flags = cv2.DFT_COMPLEX_OUTPUT) 12 dft_shift = np.fft.fftshift(dft) # 低頻值移動到中間 13 14 # 對兩通道進行轉換——映射公式 15 magnitude_spectrum = 20*np.log(cv2.magnitude(dft_shift[:,:,0],dft_shift[:,:,1])) 16 17 plt.subplot(121),plt.imshow(img, cmap = 'gray') 18 plt.title('Input Image'), plt.xticks([]), plt.yticks([]) 19 plt.subplot(122),plt.imshow(magnitude_spectrum, cmap = 'gray') 20 plt.title('Magnitude Spectrum'), plt.xticks([]), plt.yticks([]) 21 plt.show() 22 # *******************傅里恭弘=叶 恭弘變換**********************結束
4、高通、低通濾波器
低通濾波器:
1 # *******************低通濾波器**********************開始 2 import numpy as np 3 import cv2 4 from matplotlib import pyplot as plt 5 6 img = cv2.imread('lena.jpg',0) 7 8 img_float32 = np.float32(img) 9 10 # 傅里恭弘=叶 恭弘變換 11 dft = cv2.dft(img_float32, flags = cv2.DFT_COMPLEX_OUTPUT) 12 dft_shift = np.fft.fftshift(dft) 13 14 rows, cols = img.shape 15 crow, ccol = int(rows/2) , int(cols/2) # 中心位置 16 17 # 低通濾波 18 mask = np.zeros((rows, cols, 2), np.uint8) # 掩碼 19 mask[crow-30:crow+30, ccol-30:ccol+30] = 1 # 區域 20 21 # IDFT 22 fshift = dft_shift*mask # 掩碼結合 23 f_ishift = np.fft.ifftshift(fshift) # 位置還原 24 img_back = cv2.idft(f_ishift) # 傅里恭弘=叶 恭弘逆變換 25 img_back = cv2.magnitude(img_back[:,:,0],img_back[:,:,1]) # 圖像轉換 26 27 plt.subplot(121),plt.imshow(img, cmap = 'gray') 28 plt.title('Input Image'), plt.xticks([]), plt.yticks([]) 29 plt.subplot(122),plt.imshow(img_back, cmap = 'gray') 30 plt.title('Result'), plt.xticks([]), plt.yticks([]) 31 32 plt.show() 33 # *******************低通濾波器**********************結束
高通濾波器:
1 # *******************高通濾波器**********************開始 2 import numpy as np 3 import cv2 4 from matplotlib import pyplot as plt 5 6 img = cv2.imread('lena.jpg',0) 7 8 img_float32 = np.float32(img) 9 10 dft = cv2.dft(img_float32, flags = cv2.DFT_COMPLEX_OUTPUT) 11 dft_shift = np.fft.fftshift(dft) 12 13 rows, cols = img.shape 14 crow, ccol = int(rows/2) , int(cols/2) # 中心位置 15 16 # 高通濾波 17 mask = np.ones((rows, cols, 2), np.uint8) # 掩碼 18 mask[crow-30:crow+30, ccol-30:ccol+30] = 0 19 20 # IDFT 21 fshift = dft_shift*mask 22 f_ishift = np.fft.ifftshift(fshift) 23 img_back = cv2.idft(f_ishift) 24 img_back = cv2.magnitude(img_back[:,:,0],img_back[:,:,1]) 25 26 plt.subplot(121),plt.imshow(img, cmap = 'gray') 27 plt.title('Input Image'), plt.xticks([]), plt.yticks([]) 28 plt.subplot(122),plt.imshow(img_back, cmap = 'gray') 29 plt.title('Result'), plt.xticks([]), plt.yticks([]) 30 31 plt.show() 32 # *******************高通濾波器**********************結束
本站聲明:網站內容來源於博客園,如有侵權,請聯繫我們,我們將及時處理
※廣告預算用在刀口上,台北網頁設計公司幫您達到更多曝光效益
擁有後台管理系統的網站,將擁有強大的資料管理與更新功能,幫助您隨時新增網站的內容並節省網站開發的成本。