cv.cornerHarris(img, blockSize, ksize, k)
Its arguments are:
img - Input image. It should be grayscale and float32 type.
blockSize - It is the size of neighbourhood considered for corner detection
ksize - Aperture parameter of the Sobel derivative used.
k - Harris detector free parameter in the equation.
实验对象:t.jpg
尺寸:(1920 x 1920)
import numpy as np
import cv2 as cv
filename = 't.jpg'
img = cv.imread(filename)
img2 = img.copy()
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
gray = np.float32(gray)
# block_param test
img1 = img.copy()
dst1 = cv.cornerHarris(gray, 2, 3, 0.04)
dst1 = cv.dilate(dst1, None)
img1[dst1 > 0.001*dst1.max()] = [0, 0, 255]
img2 = img.copy()
dst2 = cv.cornerHarris(gray, 5, 3, 0.04)
dst2 = cv.dilate(dst2, None)
img2[dst2 > 0.001*dst2.max()] = [0, 0, 255]
img3 = img.copy()
dst3 = cv.cornerHarris(gray, 8, 3, 0.04)
dst3 = cv.dilate(dst3, None)
img2[dst3 > 0.001*dst3.max()] = [0, 0, 255]
plt.subplot(131)
plt.title('block_param:2')
plt.imshow(img1[:, :, ::-1])
plt.subplot(132)
plt.title('block_param:5')
plt.imshow(img2[:, :, ::-1])
plt.subplot(133)
plt.title('block_param:8')
plt.imshow(img3[:, :, ::-1])
plt.savefig('test_block_param.png')
# plt.show()
plt.close()
# ksobel_param test
img1 = img.copy()
dst1 = cv.cornerHarris(gray, 2, 3, 0.04)
dst1 = cv.dilate(dst1, None)
img1[dst1 > 0.001*dst1.max()] = [0, 0, 255]
img2 = img.copy()
dst2 = cv.cornerHarris(gray, 2, 5, 0.04)
dst2 = cv.dilate(dst2, None)
img2[dst2 > 0.001*dst2.max()] = [0, 0, 255]
img3 = img.copy()
dst3 = cv.cornerHarris(gray, 2, 7, 0.04)
dst3 = cv.dilate(dst3, None)
img2[dst3 > 0.001*dst3.max()] = [0, 0, 255]
plt.subplot(131)
plt.title('ksobel_param:3')
plt.imshow(img1[:, :, ::-1])
plt.subplot(132)
plt.title('ksobel_param:5')
plt.imshow(img2[:, :, ::-1])
plt.subplot(133)
plt.title('ksobel_param:7')
plt.imshow(img3[:, :, ::-1])
plt.savefig('test_ksobel_param.png')
# plt.show()
plt.close()
# k_param test
img1 = img.copy()
dst1 = cv.cornerHarris(gray, 2, 3, 0.02)
dst1 = cv.dilate(dst1, None)
img1[dst1 > 0.001*dst1.max()] = [0, 0, 255]
img2 = img.copy()
dst2 = cv.cornerHarris(gray, 2, 3, 0.04)
dst2 = cv.dilate(dst2, None)
img2[dst2 > 0.001*dst2.max()] = [0, 0, 255]
img3 = img.copy()
dst3 = cv.cornerHarris(gray, 2, 3, 0.06)
dst3 = cv.dilate(dst3, None)
img2[dst3 > 0.001*dst3.max()] = [0, 0, 255]
plt.subplot(131)
plt.title('k_param:0.02')
plt.imshow(img1[:, :, ::-1])
plt.subplot(132)
plt.title('k_param:0.04')
plt.imshow(img2[:, :, ::-1])
plt.subplot(133)
plt.title('k_param:0.06')
plt.imshow(img3[:, :, ::-1])
plt.savefig('test_k_param.png')
# plt.show()
plt.close()
# 目前针对此图的最佳参数:
dst3 = cv.cornerHarris(gray, 5, 3, 0.02)
dst3 = cv.dilate(dst3, None)
img[dst3 > 0.001*dst3.max()] = [0, 0, 255]
cv.imwrite('test.png', img)