CV - Harris特征点检测

本文介绍了如何将RGB图像转换为灰度图,并探讨了Harris角点检测在计算机视觉中的应用。
摘要由CSDN通过智能技术生成
import cv2 as cv
from matplotlib import pyplot as plt
import numpy as np
# detector parameter
block_size = 3
ksize = 3
k = 0.05

# input image
image = cv.imread('./lighttower.jpg')

print(image.shape)
height = image.shape[0]
width = image.shape[1]
channels = image.shape[2]
print('image\'s height:%s, width:%s, number of channel:%s'%(height,width,channels))
(768, 1024, 3)
image's height:768, width:1024, number of channel:3

RGB图像转灰度图

# convert it to gray image
# method 1
gray_img = cv.imread('./lighttower.jpg', cv.IMREAD_GRAYSCALE)
plt.imshow(gray_img)

<matplotlib.image.AxesImage at 0x7fda708fd890>

在这里插入图片描述

# method 2
#gray_img = image.resize((768, 1024, 1))
#plt.imshow(gray_img)

gray_img = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
# modify the image to 32-bit float point
gray_img = np.float32(gray_img)
plt.imshow(gray_img)
<matplotlib.image.AxesImage at 0x7fda7063d990>

在这里插入图片描述

corner_image = cv.cornerHarris(gray_img,block_size,ksize,k)

kernel = cv.getStructuringElement(cv.MORPH_RECT,(2,2)) # 函数会返回制定形状,MORPH_RECT是矩形,后面是锚点位置

dst = cv.dilate(corner_image,kernel) # 图像腐蚀和图像膨胀是图像中两种最基本形态学操作
# Threshold for an optimal value, marking the corners in Green
# image[corners_img>0.01*corners_img.max()] = [0,0,255]

for r in range(height):
    for c in range(width):
        pix = dst[r,c]
        if pix>0.05*dst.max():
            cv.circle(image,(c,r),5,(0,0,255),0)
        

img = cv.cvtColor(image,cv.COLOR_BGR2RGB)
plt.imshow(img)
<matplotlib.image.AxesImage at 0x7fda71c028d0>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值