Corner Detection in Rotated Squares Using 3×3 Convolution Filters

1 day ago 4
ARTICLE AD BOX

I am working on a task where I need to detect the four corners of each square in an image and highlight each corner as a single pixel.

Each filter is intended to respond to a specific corner orientation. Applying the filters produces four response maps, which have to be combined into a single 4-channel output array of shape (H, W, 4), ordered as Z, D, L, S (top, right, left, bottom).

Current implementation successfully generates response maps, but I am unsure how to convert these responses into exactly one pixel per corner.

This is the image I am using for testing:

Rotated squares

And this is my output. Rotated squares output image

def corners_of_square(img: np.ndarray) -> np.ndarray: # doesn't work img_cp = img.copy() if img_cp.ndim == 3: img_cp = cv.cvtColor(img_cp, cv.COLOR_BGR2GRAY).astype(np.float32) img_cp = img_cp.astype(np.float32) Z = np.array([ # top [ 1, 1, 1], [ 1, -1, -1], [ 1, -1, -1] ], dtype=np.float32) D = np.array([ # right [ 1, 1, 1], [-1, -1, 1], [-1, -1, 1] ], dtype=np.float32) L = np.array([ # left [ 1, -1, -1], [ 1, -1, -1], [ 1, 1, 1] ], dtype=np.float32) S = np.array([ # bottom [-1, -1, 1], [-1, -1, 1], [ 1, 1, 1] ], dtype=np.float32) L = np.array([[1,-1,-1],[1,-1,-1],[1,1,1]], dtype=np.float32) # L = left S = np.array([[-1,-1,1],[-1,-1,1],[1,1,1]], dtype=np.float32) # S = bottom rZ = cv.filter2D(img_cp , cv.CV_32F, Z) rD = cv.filter2D(img_cp , cv.CV_32F, D) rL = cv.filter2D(img_cp , cv.CV_32F, L) rS = cv.filter2D(img_cp , cv.CV_32F, S) out = np.stack([rZ, rD, rL, rS], axis=2) #out[i, j] = [Z_response, D_response, L_response, S_response] return out if __name__ == '__main__': rotated_squares_img = cv.imread(str(UTILS/"rotirani_kvadrati.png")).astype(np.float32) / 255 corners_img = corners_of_square(rotated_squares_img) cv.imshow("Rotated squares 1", rotated_squares_img) cv.imshow("Rotated squares 2", corners_img) cv.waitKey(0) cv.destroyAllWindows()
Read Entire Article