python 實(shí)現(xiàn)Harris角點(diǎn)檢測(cè)算法
import cv2 as cv import numpy as npimport matplotlib.pyplot as plt# Harris corner detectiondef Harris_corner(img):## Grayscaledef BGR2GRAY(img):gray = 0.2126 * img[..., 2] + 0.7152 * img[..., 1] + 0.0722 * img[..., 0]gray = gray.astype(np.uint8)return gray## Sobeldef Sobel_filtering(gray):# get shapeH, W = gray.shape# sobel kernelsobely = np.array(((1, 2, 1),(0, 0, 0),(-1, -2, -1)), dtype=np.float32)sobelx = np.array(((1, 0, -1),(2, 0, -2),(1, 0, -1)), dtype=np.float32)# paddingtmp = np.pad(gray, (1, 1), ’edge’)# prepareIx = np.zeros_like(gray, dtype=np.float32)Iy = np.zeros_like(gray, dtype=np.float32)# get differentialfor y in range(H):for x in range(W):Ix[y, x] = np.mean(tmp[y : y + 3, x : x + 3] * sobelx)Iy[y, x] = np.mean(tmp[y : y + 3, x : x + 3] * sobely)Ix2 = Ix ** 2Iy2 = Iy ** 2Ixy = Ix * Iyreturn Ix2, Iy2, Ixy# gaussian filteringdef gaussian_filtering(I, K_size=3, sigma=3):# get shapeH, W = I.shape## gaussianI_t = np.pad(I, (K_size // 2, K_size // 2), ’edge’)# gaussian kernelK = np.zeros((K_size, K_size), dtype=np.float)for x in range(K_size):for y in range(K_size):_x = x - K_size // 2_y = y - K_size // 2K[y, x] = np.exp( -(_x ** 2 + _y ** 2) / (2 * (sigma ** 2)))K /= (sigma * np.sqrt(2 * np.pi))K /= K.sum()# filteringfor y in range(H):for x in range(W):I[y,x] = np.sum(I_t[y : y + K_size, x : x + K_size] * K)return I# corner detectdef corner_detect(gray, Ix2, Iy2, Ixy, k=0.04, th=0.1):# prepare output imageout = np.array((gray, gray, gray))out = np.transpose(out, (1,2,0))# get RR = (Ix2 * Iy2 - Ixy ** 2) - k * ((Ix2 + Iy2) ** 2)# detect cornerout[R >= np.max(R) * th] = [255, 0, 0]out = out.astype(np.uint8)return out# 1. grayscalegray = BGR2GRAY(img)# 2. get difference imageIx2, Iy2, Ixy = Sobel_filtering(gray)# 3. gaussian filteringIx2 = gaussian_filtering(Ix2, K_size=3, sigma=3)Iy2 = gaussian_filtering(Iy2, K_size=3, sigma=3)Ixy = gaussian_filtering(Ixy, K_size=3, sigma=3)# 4. corner detectout = corner_detect(gray, Ix2, Iy2, Ixy)return out# Read imageimg = cv.imread('../qiqiao.jpg').astype(np.float32)# Harris corner detectionout = Harris_corner(img)cv.imwrite('out.jpg', out)cv.imshow('result', out)cv.waitKey(0)cv.destroyAllWindows()實(shí)驗(yàn)結(jié)果:
原圖:
Harris角點(diǎn)檢測(cè)算法檢測(cè)結(jié)果:
以上就是python 實(shí)現(xiàn)Harris角點(diǎn)檢測(cè)算法的詳細(xì)內(nèi)容,更多關(guān)于python Harris角點(diǎn)檢測(cè)算法的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 用xslt+css讓RSS顯示的跟網(wǎng)頁(yè)一樣漂亮2. 利用CSS制作3D動(dòng)畫(huà)3. CSS3實(shí)現(xiàn)動(dòng)態(tài)翻牌效果 仿百度貼吧3D翻牌一次動(dòng)畫(huà)特效4. 使用Spry輕松將XML數(shù)據(jù)顯示到HTML頁(yè)的方法5. 存儲(chǔ)于xml中需要的HTML轉(zhuǎn)義代碼6. HTML5 Canvas繪制圖形從入門(mén)到精通7. 讀大數(shù)據(jù)量的XML文件的讀取問(wèn)題8. html5手機(jī)觸屏touch事件介紹9. 讓chatgpt將html中的圖片轉(zhuǎn)為base64方法示例10. 《CSS3實(shí)戰(zhàn)》筆記--漸變?cè)O(shè)計(jì)(一)
