Category Archives: Image Processing

Recording a specific Window using OpenCV-Python

In this blog, we will learn how to record any window using OpenCV-Python.

Installing Libraries:

  1. To install PIL and pywin32, write

    in the cmd(See here). pywin32 will install win32gui, win32api, and win32con.
  2. For installing winGuiAuto, download winGuiAuto.py from here. Save this as winGuiAuto.py in the python -> lib -> site-packages.

Steps:

  1.  Create the window that you want to record (I used cv2.imshow() for that)
  2.  Give the window name that you want to record in winGuiAuto.findTopWindow()
  3.  Keep the window on top and set its position using win32gui.SetWindowPos()
  4.  Get the coordinates of the window using win32gui.GetWindowPlacement()
  5.  Grab an image of the area using ImageGrab.grab()
  6.  Append all these images into a list.
  7.  Create a VideoWriter object using cv2.VideoWriter()
  8.  Convert each image color and save it.

Code:

If you want to make a .gif file, uncomment the last part.

Note: This works well for windows 8.1, but you might find some difficulty in capturing full window in windows 10.

Hope you enjoy reading. If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time.

Extracting and Saving Video Frames using OpenCV-Python

In this tutorial, I will show you how to extract and save frames from a video file. Prerequisites are Chapter 1 and 2.

Steps:

  1.  Open the Video file or camera using cv2.VideoCapture()
  2.  Read frame by frame
  3.  Save each frame using cv2.imwrite()
  4.  Release the VideoCapture and destroy all windows

Now, Let’s code

If you don’t have any video, no need to worry. Open the camera instead of the file using cv2.VideoCapture(0) and start extracting frames.

Use: We can perform a number of operations on these frames like crop, flip, reverse etc. save them into a list and iterate over them to get cropped/flipped/reversed video. (See How you can make Video from images)

Hope you enjoy reading. If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time

Creating Video from Images using OpenCV-Python

In this blog, we will do a small project using OpenCV-Python where we will be creating video from image sequences. This project is entirely based on what we read in Chapter 1 and 2. Let’s start

Steps:

  1.  Fetch all the image file names using glob
  2.  Read all the images using cv2.imread()
  3.  Store all the images into a list
  4.  Create a VideoWriter object using cv2.VideoWriter()
  5.  Save the images to video file using cv2.VideoWriter().write()
  6.  Release the VideoWriter and destroy all windows.

Let’s see the code

glob.glob(Pathname) fetches all the filenames present in that path. ‘*.jpg’ means all the jpg files. So, in code glob.glob() fetches the filename of all the jpg files present in that path.

Hope you enjoy reading.

If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time.

Read, Write and Display Videos with OpenCV Python

In this blog, we will see how to Read, Write and Display Videos using OpenCV. Since a video is made up of images, most of the commands we learned in the previous blog also applies here.

Let’s see by an example how to capture video from the camera and display it.

cv2.VideoCapture(0) will open the default camera. You can select the second camera by passing 1, third by passing 2 and so on. This creates a VideoCapture object (“cap” here).

cap.read() capture frame by frame. This returns two values, frame and ret. If the frame is read correctly, ret will be True otherwise False.

cv2.waitKey(1) & 0xFF == ord(‘q’) will exit the video when ‘q’ is pressed.

cap.release() closes video file or capturing device.

If you want to play a video from a file, just change the cv2.VideoCapture(0) function in the above code by giving the file path as cv2.VideoCapture(‘F:/downloads/Python.mp4’). Also, use the appropriate time for cv2.waitKey() function (25 will be OK).

Saving a Video:

First, create a VideoWriter object with cv2.VideoWriter(output filename, fourcc, fps, frameSize). fourcc(4-character code of codec) is used to compress the frames. After creating this object, use object.write() to save the video. Let’s see an example

Now, you might have got some feeling about the basic video commands in OpenCV. Hope you enjoy reading.

If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time.

Read, Write and Display Images with OpenCV

In this blog, we will see how to Read, Write and Display Images using OpenCV. I hope you have installed OpenCV, numpy, and matplotlib libraries, if not, please refer to this blog.

Read an image:

To read an image, use the function cv2.imread(filename[, flags]) where filename is the full path of image and flags specifies the way image should be read (>0 for color, =0 for greyscale, and <0 for loading image as is (with alpha channel)).

If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format), the function returns an empty matrix, not an error.

Display an image:

To display an image in a window, use the function cv2.imshow(winname, image) where the first argument is the Name of the window and second is the Image to be shown. So, this will first create a window named as image and displays the image in that window.

Note: This function must be followed by cv2.waitkey(delay) function otherwise the image wouldn’t be displayed.

cv2.waitKey(delay) decides for how long the image will be displayed. Its argument delay is the time in milliseconds. If the delay is <=0, the image will be shown forever otherwise destroyed after delay milliseconds.

cv2.destroyAllWindows() simply destroys all the windows we created.

Special Case: We can create a window first and load the image to it later. Just write the below code line before the cv2.imshow() function.

Write an image:

To save an image, use the function cv2.imwrite(filename, image) where the first argument is the file name with which we want to save the image file, the second argument is the image you want to save.

This will save the image in JPEG format in the working directory.

Now, you might have got some feeling about the basic image commands in OpenCV. Hope you enjoy reading.

If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time.

What is a Digital Image?

In this blog, we will try to understand what is a digital image and how it is formed. We will also learn about image sensors and pixels in this blog.

What is a Digital Image?

A Digital Image is a numeric representation of a two-dimensional image and is made of picture elements called pixels, arranged in rows and columns. These numeric values are the intensity or brightness values that are associated with the pixels.

Digital Image

Now, let’s understand what is a pixel and how the intensity value is calculated?

The concept of pixel is closely related with image sensors. So, by understanding image sensors I hope you will get a feel of what a pixel is.

Image Sensor: It is a device that converts the light energy into an electric signal. The figure below shows a single image sensor.

source: R. C. GonzalezR. E. Woods, Digital Image Processing

In digital cameras, these sensors are arranged in the form of a 2D array on a chip. Mostly there is a 1-to-1 correspondence between a pixel and a sensor meaning each sensor produces 1 pixel. Thus, the total number of pixels in an image = total number of sensors on a chip. Sometimes, multiple sensors are used to produce a pixel of information.

8MP camera means there are 8×106 image sensors in the camera chip and if there is a 1-to-1 correspondence between a pixel and a sensor then the image contains 8×106 pixels.

Note: Larger the pixel, better will be the image quality.

How the Digital Image is formed?

There are basically two types of image sensors used in digital cameras: CCD or CMOS. The CCD sensor is a silicon chip that contains a 2D array of photosensitive sites or sensors. Each sensor has a PN diode and a storage cell. Each sensor outputs a pixel value through these steps

  1. Light energy or photons(>1.2eV) falls on the silicon layer of the sensor and electrons are released(stored in potential well until the shutter is open).
  2. After the camera shutter closes, these electrons are moved to the storage cell (by applying +ve voltage on vertical shift register).
  3. These electrons are then transferred to the serial shift register.
  4. These electrons are converted into the analog voltage that is amplified using an amplifier.
  5. The output voltage signal is immediately converted to a digital signal by means of an analog-to-digital converter (ADC) in digital cameras, either on or off-chip. This digital quantity obtained is the intensity value of a pixel.

These steps are for the CCD sensor. For CMOS sensor, step 3 is not performed and voltage conversion and amplification (Step 4) is done in the sensor itself. This voltage is sent to ADC with the help of switch from each sensor and outputs the intensity value of a pixel.

Repeat these steps for all the sensors on a chip to get the intensity values corresponding to each pixel and thus we get a digital image.

Now, you might have got some feeling about the image sensors, pixel, and digital image. Hope you enjoy reading.

If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time.

Installing Python OpenCV and other libraries

Here, we will be installing the libraries that are required to perform image processing operations. We will be using the following Python libraries

  1. OpenCV
  2. Numpy
  3. Matplotlib

Why we are using OpenCV, not Matlab or any other?

  1. Because it is open source, fast(written in C/C++), memory efficient and easy to install (can run on any device that can run C).
  2. if you really want to learn about how computer vision works from the initial steps to the last, I suggest you learn OpenCV first then use any deep learning library like Tensorflow, pytorch etc when you’re ready to train a deep learning algorithm for better performance/accuracy results.

Installing Numpy, OpenCV or any library      (For WINDOWS)

There are two ways to install any library in Python IDLE,

  1. using pip command: In the installed Python folder, go to Scripts folder and open command prompt(press and hold Shift + Right Click and select Open command window here).  Then write pip install library name to get it installed. For example
  2. using .whl file:  First download .whl file of any library (version corresponding to your Python) from here. Then open the command prompt where you have downloaded this file and write pip install filename.whl. For example

Installing Numpy, OpenCV or any library    (For UBUNTU)

First, install pip using apt-get, then you can install any library as shown below

Anaconda: open the Anaconda prompt and write pip install numpy or any other library name which you want to install.

Now, you might have got some feeling about how to install Python libraries. Hope you enjoy reading.

If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time.