【发布时间】:2021-03-10 19:23:35
【问题描述】:
最后我有一个工作代码,可以检测图像中矩形的角。但问题是代码在同一个角检测多个点。现在我试图在我的代码中引入非最大抑制,但它不起作用。我以前尝试过一个建议,但它也不起作用。如何正确进行这种非极大值抑制。
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as im
from scipy import ndimage
# 1. Before doing any operations convert the image into gray scale image
img = im.imread('OD6.jpg')
plt.imshow(img)
plt.show()
# split
R=img[:,:,0]
G=img[:,:,1]
B=img[:,:,2]
M,N=R.shape
gray_img=np.zeros((M,N), dtype=int);
for i in range(M):
for j in range(N):
gray_img[i, j]=(R[i, j]*0.2989)+(G[i, j]*0.5870)+(B[i, j]*0.114);
plt.imshow(gray_img, cmap='gray')
plt.show()
# 2. Applying sobel filter to find the gradients in x and y direction respectively and remove noise
# using gaussian filter with sigma=1
imarr = np.asarray(gray_img, dtype=np.float64)
ix = ndimage.sobel(imarr, 0)
iy = ndimage.sobel(imarr, 1)
ix2 = ix * ix
iy2 = iy * iy
ixy = ix * iy
ix2 = ndimage.gaussian_filter(ix2, sigma=1)
iy2 = ndimage.gaussian_filter(iy2, sigma=1)
ixy = ndimage.gaussian_filter(ixy, sigma=1)
c, l = imarr.shape
result = np.zeros((c, l))
r = np.zeros((c, l))
rmax = 0 # initialize the maximum value of harris response
for i in range(c):
for j in range(l):
m = np.array([[ix2[i, j], ixy[i, j]], [ixy[i, j], iy2[i, j]]], dtype=np.float64)
r[i, j] = np.linalg.det(m) - 0.04 * (np.power(np.trace(m), 2))
if r[i, j] > rmax:
rmax = r[i, j]
# 3. Applying non maximum supression
for i in range(c - 1):
for j in range(l - 1):
if r[i, j] > 0.01 * rmax and r[i, j] > r[i-1, j-1] and r[i, j] > r[i-1, j+1]\
and r[i, j] > r[i+1, j-1] and r[i, j] > r[i+1, j+1]:
result[i, j] = 1
xy_coords = np.flip(np.column_stack(np.where(result==1)), axis=1)
print (xy_coords)
pc, pr = np.where(result == 1)
plt.plot(pr, pc, "b.")
plt.imshow(img, 'gray')
plt.show()
【问题讨论】:
标签: image image-processing computer-vision filtering