Extract detected edges from image into separate image

362 Views Asked by At

I would like to crop a certain region around a detected corner (e.g. detected with Harris Detector) and save this region as a separate image. Does anyone have an idea how to work this out?

I had the idea to start from this basic lines:

import cv2
import numpy as np

filename = 'pic1.jpg'
img = cv2.imread(filename)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

gray = np.float32(gray)
dst = cv2.cornerHarris(gray,2,3,0.04)

#result is dilated for marking the corners, not important
dst = cv2.dilate(dst,None)

# Threshold for an optimal value, it may vary depending on the image.
img[dst>0.01*dst.max()]=[0,0,255]

cv2.imwrite('pic2.jpg',img)
if cv2.waitKey(0) & 0xff == 27: cv2.destroyAllWindows()
1

There are 1 best solutions below

0
Rahul Kedia On

Find contours in the edge image and get the bounding rectangle of that contour. Using this bounding rectangle details, crop the image. You will have the desired output.