info@iobrain.in / iobrainlabz@gmail.com
+91 93536 94456

Blog

Standing Out From The Rest

python opencv(full course)

In this post, I will show you how I worked with opencv and hopefully, teach you how to work with it. So let’s Begin!

The first step is to install opencv. I hope you have downloaded python 3.8 the version I am working with. I also hope you have installed pip, If not, download python file and run it In IDLE. It will install pip on your computer.

After you have installed pip, open command prompt and type this command

py -m pip install opencv-contrib-python

It will install opencv(and numpy) on your computer. To check that type ‘import opencv’ in Idle. If it shows no error, opencv has successfully been installed on your computer. Congrats, you’ve completed the first step.

The second step is to learn the basics. And to my knowledge, these are the examples that are basic.

Show images in opencv:

this is the most basic of all basics in opencv. This will show an image file in three formats: color, grey, and alpha(normal), and it will also close all the image windows when the escape key is pressed. here is the code:

import cv2

esc=27

img=cv2.imread(“standing.png”,0)

img2=cv2.imread(“standing.png”,1)

img3=cv2.imread(“standing.png”,-1)

cv2.imshow(“image”,img)

cv2.imshow(“image_”,img2)

cv2.imshow(“image-“,img3)

key=cv2.waitKey(0)

if key==esc:

cv2.destroyAllWindows

now I’ll explain the code:

first, we import opencv and declare the variable ESC(i.e the escape key) as 27(the ASCII value for the escape key)

second, we read the image file we want to show(standing.png) in different ways :

0 reads the image in grey(black & white), 1 as normal colors and 2 reads it with an alpha channel(do not freak out that just means you can make images translucent)

third, we show the images in different windows and a name for each of the (image, image_, image+).

fourth we read the keys we press (in ASCII) and if it is the escape key (if it has the same value as it.) opencv destroys all the windows it was showing (image, image_, image+)

Editing and Saving images in opencv:

Now that we know how to open and show an image using python opencv its time we actually edit and save the image that we show. In this part of the course, I will show you how to change an image to black&white(grey) color format and save it In the folder that you run the code. here is the code for that

import cv2

image=cv2.imread(“standing.png”)

grey=cv2.cvtColor(image,COLOR_BGR2GRAY)

key=cv2.waitKey(0)

if key==ord(‘s’):

cv2.imwrite(“result.png”,image)

after importing opencv and reading the image, I use the function

cv2.cvtColor and change the color from BGR (color) to(2) GRAY (black & white)

then it reads the keys I press and when I press s key it saves the image(imwrite) as result.png in the same folder as the code which you can open later.

Note: ord(‘s’) gives the ASCII value for s

show video:

In opencv, the videos are captured using the function cv2.VideoCapture(). and are shown as one frame at a time. Now, here is the code for showing a video(file and webcam) in a separate window:

import opencv

from numpy import*

show=cv2.VideoCapture(“traffic.mp4”)

webcam=cv2.VideoCapture(0)

run=True

escape=27

while run:

  ret,frame=show.read()

  ret2,frame2=webcam.read()

  cv2.imshow(‘my video’,frame)

  cv2.imshow(‘webacm’,frame2)

  key=cv2.waitKey(1)

  print(key)

  if key==escape:

    cv2.destroyAllWindows()

So first we read the video we want to show using either a file name or a number for a webcam(cv2.VideoCapture)

Next, we read the video one frame at a time using a forever while loop

the value frame stores the image(one frame) in the captured video and we display it using the imshow command and since the video has the same title the previous image gets replaced very fast, therefore, giving the illusion of a moving video(that’s how all videos work).

then we have the frequently used code that destroys all windows when the escape key is pressed(see the previous example for further info)