OpenCV - identifying edges and corners

26 Jun 2019

back to meddle with openCV for fun!!


Corner detection


import numpy as np 
import cv2 
from matplotlib import pyplot as plt 

# read the image 
img = cv2.imread('dog.jpeg') 
  
# convert image to gray scale image 
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 

# detect corners with the goodFeaturesToTrack function. 
corners = cv2.goodFeaturesToTrack(gray, 40, 0.01, 10) 
corners = np.int0(corners) 

# we iterate through each corner,  
# making a circle at each point that we think is a corner. 
for i in corners: 
    x, y = i.ravel() 
    cv2.circle(img, (x, y), 2, 255, -1) 
  
plt.imshow(img), plt.savefig('dog_dots.jpeg')

full credit to code at: https://www.geeksforgeeks.org/python-detect-corner-of-an-image-using-opencv/


Edge Detection


import numpy as np 
import cv2 
from matplotlib import pyplot as plt 

img = cv2.imread('dog.jpeg',0)
edges = cv2.Canny(img,150,200)

plt.imshow(img,cmap = 'gray'), plt.savefig('dog_grey.jpeg')
plt.title('Original Image'), plt.xticks([]), plt.yticks([])

plt.imshow(edges,cmap = 'gray'), plt.savefig('dog_edge.jpeg')
plt.title('Edge Image'), plt.xticks([]), plt.yticks([])

plt.show()

full credit of code to: https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_canny/py_canny.html