import numpy as np
import cv2
def harris_corner(image, window_size, alpha, thresh):
"""
:param image: 输入图像
:param window_size: 窗口尺寸
:param alpha: Harris角点常数,通常取值0.04~0.06
:param thresh: 阈值
:return:
"""
dy, dx = np.gradient(image)
Ixx = dx ** 2
Ixy = dy * dx
Iyy = dy ** 2
height = image.shape[0]
width = image.shape[1]
corner_list = []
new_image = image.copy()
color_img = cv2.cvtColor(new_image, cv2.COLOR_GRAY2RGB)
offset = int(window_size / 2)
for y in range(offset, height - offset):
for x in range(offset, width - offset):
windowIxx = Ixx[y - offset:y + offset + 1, x - offset:x + offset + 1]
windowIxy = Ixy[y - offset:y + offset + 1, x - offset:x + offset + 1]
windowIyy = Iyy[y - offset:y + offset + 1, x - offset:x + offset + 1]
A = windowIxx.sum()
B = windowIxy.sum()
C = windowIyy.sum()
det = (A * C) - (B ** 2)
trace = A + C
r = det - alpha * (trace ** 2)
if r > thresh:
corner_list.append([x, y, r])
color_img.itemset((y, x, 0), 0)
color_img.itemset((y, x, 1), 0)
color_img.itemset((y, x, 2), 255)
return color_img, corner_list
image_path = './images/xs.jpg'
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
image = cv2.resize(image,(480,480),cv2.INTER_LINEAR)
result_image, corner_list = harris_corner(image, window_size=3, alpha=0.04, thresh=100000)
cv2.imshow('origin_Image', image)
cv2.imshow('result_Image', result_image)
cv2.waitKey(0)
import cv2
image_path = './images/xs.jpg'
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
image = cv2.resize(image,(480,480),cv2.INTER_LINEAR)
detector = cv2.SIFT_create()
keypoints1, descriptors1 = detector.detectAndCompute(image, None)
result_image1 = cv2.drawKeypoints(image, keypoints1, image, color=(0, 0, 255))
image_path = './images/xs2.jpg'
rotated_image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
rotated_image = cv2.resize(rotated_image,(480,480),cv2.INTER_LINEAR)
keypoints2, descriptors2 = detector.detectAndCompute(rotated_image, None)
result_image2 = cv2.drawKeypoints(rotated_image, keypoints2, rotated_image, color=(0, 0, 255))
bf = cv2.BFMatcher()
matches = bf.knnMatch(descriptors1, descriptors2, k=2)
match_thresh = 0.8
keep = []
for m, n in matches:
if m.distance <= (match_thresh * n.distance):
keep.append([m])
result = cv2.drawMatchesKnn(image, keypoints1, rotated_image, keypoints2, keep, None, flags=2)
cv2.imshow('Keypoints1', result_image1)
cv2.imshow('Keypoints2', result_image2)
cv2.imshow('Result', result)
cv2.waitKey(0)
import numpy as np
import cv2
from skimage.feature import greycomatrix, greycoprops
image_path = './images/xs.jpg'
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
image = cv2.resize(image,(480,480),cv2.INTER_LINEAR)
angle = - np.pi / 4
matrix_coocurrence = greycomatrix(image, [1], [angle], levels=256, normed=False, symmetric=False)
contrast = greycoprops(matrix_coocurrence, 'contrast')
dissimilarity = greycoprops(matrix_coocurrence, 'dissimilarity')
homogeneity = greycoprops(matrix_coocurrence, 'homogeneity')
energy = greycoprops(matrix_coocurrence, 'energy')
correlation = greycoprops(matrix_coocurrence, 'correlation')
asm = greycoprops(matrix_coocurrence, 'ASM')
print('Contrast = {}'.format(contrast))
print('Dissimilarity = {}'.format(dissimilarity))
print('Homogeneity = {}'.format(homogeneity))
print('Energy = {}'.format(energy))
print('Correlation = {}'.format(correlation))
print('ASM = {}'.format(asm))
cv2.imshow('Image', image)
cv2.waitKey(0)