HOW TO USE CANNY DETECTION METHOD IN OPENCV USING WEBCAM.

 The modern world needs edge detection over the image which is an important image processing technique. 

One such method introduced was the canny detection method, which is available in the OpenCV library, which is one of the most popular libraries for computer visioning and can be coded around languages java,c++, python.

The images are converted into binary images, and that's what thresholding does over a Grayscale image( which you can convert using the Cv2.cvtColor() method), the method works over the binary image so, you have to mandatorily code in this way. 

The purpose of this blog is to teach you to apply canny's edge detection directly using a webcam, rather than using cv2.imread() which, first save the image and then to apply which is lengthy and sometimes you may need to apply it live ( and not on saved image's).

So, now I am going to write the code and explain to you how it works :


import cv2
import NumPy as np 
                                                                                                                                                                                                                                                    
cam=cv2.VideoCapture(0#write  the videocapture(0)

while cam.isOpened :    
    _,frame = cam.read()
    gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) #bgr to gray
   
    blur=cv2.GaussianBlur(gray,(5,5),0)#blurs image, optional
   
    edges=cv2.Canny(blur,100,200#using the cannydetection   
    cv2.imshow("img",edges)   # displaying out the image
    
    if cv2.waitKey(1)==ord("q") : #dont keep "0"inside waitkey
        break
cv2.destroyAllWindows()

so, follow the code and you will get the output, to understand the  canny edge detection theory refer

https://docs.opencv.org/master/da/d22/tutorial_py_canny.html

and if you get any doubts still, you can mail me at kaman2528@gmail.com anytime.

my GitHub:https://github.com/amanem1 to refer to this 

and result output for me was an image looking like this.




See, it's easy and very interesting to have hands-on experience.

Comments

Subscribe for our Newsletter

RE-IMAGINING THE WAY
Back to top