Learn Python

Learn Data Structure & Algorithm

Learn Numpy

Learn Pandas

Learn Matplotlib

Learn Seaborn

Learn Statistics

Learn Math

Learn MATLAB

Learn Machine learning

Learn Github

OpenCV Introduction

OpenCV Image Read & Write

OpenCV Image Channels

OpenCV Color Conversion

OpenCV Rotation & Crop

OpenCV Text & Shape

OpenCV Video

OpenCV Arithmetics Operation

OpenCV Thresholding

OpenCV Blur & Blending

OpenCV Edge Detection

OpenCV Contours

OpenCV Hough Transformation Lines

OpenCV Moments & Convexhull

OpenCV Morphological Transformation

OpenCV Template Matching

Learn Deep Learning

Learn MySQL

Learn MongoDB

Learn Web scraping

Learn Excel

Learn Power BI

Learn Tableau

Learn Docker

Learn Hadoop

Image edge detection in opencv

How to perform edge detection?

Input
import cv2

path=r"E:\picture\prac_deep\dd.jpg"

img=cv2.imread(path,0)
img=cv2.resize(img,(800,700))

canny=cv2.Canny(img,26,170)
'''
Canny function is used for detecting the edge. 26 is lower threshold and 170 is the upper threshold.
'''

cv2.imshow("crop_img",canny)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output

How to detect edge using webcam/video stream?

Input
import cv2

cap=cv2.VideoCapture(0)
'''
Let's access the frames of a video. We know that in the video there are a lot of frames and we display those frames one by one continuously. So to play we have to get those frames.To do that we will use a while loop.
'''

def sketch(image):
   img_gray= cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
   #For edge detection, we will convert the images into grayscale

   img_gray_blur=cv2.GaussianBlur(img_gray,(5,5),0)
   '''
   We have to perform blur to smooth the image. So we use GaussianBlur    function and here we took a 5x5 size kernel.
   '''

   canny_edge_detec=cv2.Canny(img_gray_blur,10,70)
   '''
   Canny function is used to detect the edge. Here 10 is lower    threshold and 70 is the upper threshold.
   '''

   ret,mask= cv2.threshold(canny_edge_detec,70,255,cv2.THRESH_BINARY)
   '''
   We will see the output in black and white. Here 70 is the lower threshold    and 255 is the upper threshold
   '''

   return mask

while cap.isOpened():
   '''
   The loop will be always    true while capturing the image. That's why we used cap.isopened() function
   '''
   ret,frame=cap.read()
   '''
   using the read function we will read the frame. After reading we will get two    things. The first one is True or False. True or false means the cap is open or not    and another one is the frame. We have to store these values in two variables (in the    example ret and frame). Now if the value of ret is false then we have to break    the loop. Because ret value false means cap is not open and if the cap is not open    then no frames will come. That's why no need to run the loop
   '''

   if ret:
      cv2.imshow("live edge detection",sketch(frame))

      if cv2.waitKey(25) & 0xff==ord("q"):
         '''
         waitKey means here we will wait, if the user press the q button then          the video display will be off otherwise the video will be shown
         '''
         break
   else:
      break
cap.release()
cv2.destroyAllWindows()
Output

Corner Detection

Detecting corner using cv2.cronerHarris() function

Input
import cv2

path=r"F:\picture\prac_deep\shape.png"

img=cv2.imread(path)

img=cv2.resize(img,(800,700))
'''
set the image height and width. The displayed image height will be that height which you give in the resize function
'''

#let's convert the image into gray scale
gray_img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

#Let's get the corners
corners=cv2.goodFeaturesToTrack(gray_img,1000005,0.01,40)
'''
This function takes four parameters:
the first parameter is the image,
second parameter is the number of corners. If we pass 5 then the function will show 5 corners and it doesn't matter how many corners are there so try to pass a big number.
Suppose you pass 100000 but there are only 70 corners present in the image but don't worry it will mark 70 corners.
Third parameters quality lever. Here we pass the pixel quality.
The fourth parameter is the minimum distance between corners. It is the distance between two corners. If the distance is more then you will see fewer dots and if less then you will see more dot's
'''

#Let's convert the data type of corners into int64.
corners=np.int64(corners) #we did this because this algorithm required this int64 data type

for i in corners:
   '''
   In the corners variable, we have to coordinate of the corners.    So we are taking corners value one by one in i variable
   '''

   x,y=i.ravel()
   '''
   Here x has x-axis pixel value and y has y-axis pixel value of a particular corner
   '''

   cv2.circle(img,(x,y),3,255,-1)
   '''
   We want to draw a circle where the function detect the corner
   '''

cv2.imshow("my image",img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output

CodersAim is created for learning and training a self learner to become a professional from beginner. While using CodersAim, you agree to have read and accepted our terms of use, privacy policy, Contact Us

© Copyright All rights reserved www.CodersAim.com. Developed by CodersAim.