Capture Image using Python OpenCV Machine Learning Image Processing

in #python8 years ago

Machine Learning is the hot topic now not only in the IT Industry but in the other industries too. Machine Learning we can say is to provide the ability to computers to think and predict using the model defined on it or teaching computers to make and improve predictions based on some data. Imagine a situation that you are getting your X-Rays report of your lungs and what if it will tell you or we say a computer will predict your possible future diseases related to it and give out a suggestion too how to cure it by scanning your lungs image. So you just have to upload your documents or it will be done automatically by the respective centers. So to work with Images that can be moving or still we use OpenCV python library which is an open source computer vision library. OpenCV is capable of recognizing images, gestures, object detection, etc.

What we will learn from this post ?

  • How to capture still Images.
  • How to convert images to Grayscale.
  • How to flip the Images.
Lets's get started I will be working on Sypder IDE using Anaconda I am using Anaconda because packages come with the bundle and module maintenance is handy.

Step 1. Importing cv2 module

OpenCV releases two type of python interface which is cv and cv2 for the operation as cv2 returns everything returned as NumPy objects like ndarray and native Python objects like lists,tuples,dictionary, etc as you can do various NumPy operations on it. NumPy is a highly stable and fast array processing library.

#importing cv2

import cv2

 

Step 2. Create VideoCapture Object

To capture image/video you need to create VideoCapture object. You need to pass parameters 0 for first camera and 1 for second camera. You can also pass the location of the video file.

#create VideoCapture Object

imagecapture=cv2.VideoCapture(0)
 

Step 3. Reads Value

VideoCapture returns a tuple with two value the (boolean,image). With the first item you check whether the reading was successful or not and if it was then you proceed to use the returned image value.

#reading value

val,frame=imagecapture.read()
 

Step 4. Showing Frame

imshow function of the cv2 module consist of two parameters ('Frame Name', actual frame to display).

#showing the image in frame

cv2.imshow('Captured Frame',frame)
 

Step 5. Flipping Image

flip method consist of two parameter first one is frame which will consist the image and second is value for the frame as the value can be 1, 0 ,-1. Value 1 defines the vertical flip whereas value 0 defined the horizontal and -1 for both vertical and horizontal flip.

#Flipping Image

frame=cv2.flip(frame,1)
 

Step 6. Converting Image to Grayscale

#converting to Grayscale

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

cv2.imshow('Captured Frame',gray)
 

Step 7. Destroying Windows

We will assume the window with the image should be destroyed. To to so, we can call the destroyAllWindows function, which will destroy all the windows previously created. So here is our python script will look like for capturing image from camera.

import cv2
import numpy as np

imageCapture=cv2.VideoCapture(0)

val,frame=imageCapture.read()

frame=cv2.flip(frame,1)

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

cv2.imshow('Captured Frame',gray)

cv2.waitKey(0)

imageCapture.release()

cv2.destroyAllWindows()

Output

 

 

Coin Marketplace

STEEM 0.05
TRX 0.33
JST 0.080
BTC 63782.23
ETH 1706.19
USDT 1.00
SBD 0.41