Open CV/Image processing

 what is OpenCV?

OpenCV (Open source computer Vision) is a library of programming language.  This library plays an important role in real-time operation which is mandatory for the todays system. It is an open source library for the computer vision, machine learning and image processing. OpenCV was built to provide a common infrastructure for computer vision applications and to accelerate the use of machine perception in the commercial products.


IMAGE PROCESSING TUTORIAL IN PYTHON (Using OpenCV)

In this, tutorial , you will learn how you can process image in python using OpenCV library.

It is a free open source library used in real-time image processing. It's used to process images, videos and even live streams, but  in this tutorial we will focus on image processing basic to advance.


Prerequisites

1. Anaconda Navigator  should be installed in your local Machine

2. The IDE I am here using is SPIDER you can use any SPIDER or JUPYTERNOTEBOOK


Lets starts,

what is  Digital image processing?

Digital image processing is the processing of the digital image  by means of digital computer in order to extract the meaningful information from it.

Digital image consist of pixel in it. Simply, digital image is the represent by a pixel value. Pixel is the smallest item of information in an image. 



Lets open an IDE and start coding (here I am using Spyder)

first , import the OpenCV  here I use OpenCV as cv2 in python.


import cv2 #OpenCV use as cv2 in python
img1 = cv2.imread('H:\\Data\\avengers.jpg',1)  
img1 = cv2.resize(img1,(1280,700))#width ,height
cv2.imshow("Colored Image",img1)  #It accept two parameters 1)- Name of screen ,2) -  Image
print("Give image with color==\n",img1)

#cv2.IMREAD_GRAYSCALE : Loads image in grayscale mode
img2 = cv2.imread('H:\\Data\\avengers.jpg',0)
img2 = cv2.resize(img2,(1280,700))#width ,height
cv2.imshow("Gray Scale Image",img2)
print("Image in gray scale==\n",img2)

#cv2.IMREAD_UNCHANGED : Loads image as such including alpha channel
img3 = cv2.imread('H:\\Data\\avengers.jpg',-1)
img3 = cv2.resize(img3,(1280,700))#width ,height
cv2.imshow("Original Image",img3)
print("Image in original value==\n",img3)

cv2.waitKey(0)  #here parameter inside waitkey handle the life duration of an image
cv2.destroyAllWindows()


#Image conversion project colored image into grayscale.

#path = input("Enter the Path and name of an image===")
#print("You Enter this===",path)

#Now read image 
img1 = cv2.imread("H:\\Data\\thor.jpg",0) #convert image into grayscale
img1 = cv2.resize(img1,(560,700))
img1 = cv2.flip(img1,0)#it accept 3 parameters 0,-1,1
cv2.imshow("converted image==",img1)
k = cv2.waitKey(0) & 0xFF
if k == ord("q"):
    cv2.destroyAllWindows()
    
elif k == ord("s"):
    cv2.imwrite("H:\\ouput.png",img1)  #it accept name of image and data
    cv2.destroyAllWindows()



How to read a video from any folder using OpenCV?

Here, with the help of video capture function we easily read any video.


cap = cv2.VideoCapture("C:\Users\Hp\Desktop\video.mp4")   #Here parameter is a path of any video
while True:
    ret, frame = cap.read()   #here read the frame
    #get height and width of frame
    print("Width ==>",cv2.CAP_PROP_FRAME_WIDTH)
    print("Height==>",cv2.CAP_PROP_FRAME_HEIGHT)
    
    frame = cv2.resize(frame,(700,450))
    gray  = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    frame = cv2.flip(frame,-1) 
    cv2.imshow('Colorframe',frame)
    cv2.imshow("Gray Frame",gray)
    if cv2.waitKey(25) & 0xFF == ord('q'):   #press to exit
        break
   
# Release everything if job is finished
cap.release()
cv2.destroyAllWindows()

How to capture a video from webcam and save it into the required location?

CODE:
  


cap = cv2.VideoCapture(0,cv2.CAP_DSHOW)   #Here parameter 0 is a path of any video use for webcam
print("check===",cap.isOpened())

#it is 4 byte code which is use to specify the video codec
#Various codec -- 
#DIVX, XVID, MJPG, X264, WMV1, WMV2
fourcc = cv2.VideoWriter_fourcc(*"XVID")  # *"XVID"
#It contain 4 parameter , name, codec,fps,resolution
output = cv2.VideoWriter("output.avi",fourcc,20.0,(640,480),0)

while(cap.isOpened()):
    ret, frame = cap.read()   #here read the frame
    
    if ret==True:
        
        gray  = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
        #here flip is used to lip the video at recording time
        frame = cv2.flip(frame,0)
        output.write(gray)
        
        cv2.imshow("Gray Frame",gray)
        cv2.imshow('Colorframe',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):   #press to exit
            break
    else:
        break
 
# Release everything if job is finished
cap.release()
output.release()
cv2.destroyAllWindows()

Canny Edge Detection Using OpenCV

It is a popular edge detection approach which uses multi-stage algorithm to detect an edges. It is developed by John F Canny in 1986. 
Canny Edge Detection Approach is combine with 5 approaches and they are mention below.
1. Noise Reduction 
2. Gradient Calculation
3. Non-maximum Suppression
4. Double Threshold
5. Edge Tracking by Hysteresis

After applying these steps, you will be able to get the following results
canny edge detection of building.

 The above results is the output of the below code.

CODE:                               
import cv2
import numpy as np
#load image into gray scale
img = cv2.imread(r"C:\Users\Hp\Desktop\image\building.jpg")
img = cv2.resize(img,(400,500))
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#canny(img,thresh1,thres2)thresh 1 and thresh2 at different lvl
canny = cv2.Canny(img_gray,20,150)
cv2.imshow("original==",img)
cv2.imshow("gray====",img_gray)
cv2.imshow("canny==",canny)
cv2.waitKey(0)
cv2.destroyAllWindows()


Image contours

Image contour is used to analyze the shape and detect the edges of the images. contour can be explained simply as a curve joining all the continuous point (along the boundary ), having same color or intensity. The contour are useful tool for shape  analysis and object detection . For better accuracy in image contour the image should be in grayscale.

Comments

Popular posts from this blog