Thursday, August 19, 2021

Python OpenCV: Converting an image to grayscale image

 Introduction

In this tutorial, we will discuss how the color image is converted to grayscale image using two OpenCV functions: imread() and cvtColor().

Method 1: Using imread() function

imread() function is used to read an image from the disk but while loading the image, it can be directly converted to grayscale image. The imread function is already discussed in Python OpenCV: Read/Display/Write Image

Python Code

# import the library
import cv2 as cv

# Load the image as grayscale
img = cv.imread("lena.jpg", cv.IMREAD_GRAYSCALE)
# Display the image
cv.imshow("Grayscale", img)
cv.waitKey(0)

Output


Method 1: Using cvtColor() function

cvtColor() is one the functions used to convert color channels from one to other format to other. It is also used to convert the image to grayscale.

Python Code

# import the library
import cv2 as cv

# Load the image as grayscale
img = cv.imread("lena.jpg")
# Convert to grayscale
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
# Display the image
cv.imshow("Original Lena", img)

# Display the grayscale image
cv.imshow("Grayscale", gray)
cv.waitKey(0)

Output



Python OpenCV: Read/Display/Write Image

 Read/Display/Write Image

  • Reading, displaying and writing images are the basic to image processing and computer vision task.
  • In OpenCV, three inbuilt functions are used to load, display and write the images. The inbuilt functions are:
    1. imread() - helps to load image from the disk
    2. imshow() - helps to display image in a window
    3. imwrite() - helps to write image in the directory

Read/Display an Image

For reading the image, imread() is used. The functions takes two parameters:

imread(image, flags)

  • image: first argument is the name of the image/path.
  • flags: is an optional flag that lets you to specify how you want to load the image. It offers three constants:
    1. IMREAD_COLOR: Loads the image in the BGR format (default)
    2. IMREAD_UNCHANGED: Loads the image as (including the alpha channel if present)
    3. IMREAD_GRAYSCALE : Loads the image as an grayscale format

For display the image, imshow() function is used. The function takes two parameters:

imshow(name of the window, image)

Python Code



Line 2: Import the OpenCV package
Line 4: Loads "lena.jpg" from the disk and assign to img variable
Line 6: Display the image in the window using imshow() function
Line 8: Waits for the use to press any key 

Output

Writing an Image

To write the image, imwrite() function is used. The function takes two parameters:

imwrite(filename, image)

  • filename: the first argument is the filename, which must include filename extension
  • image: Image that you want to save
Python Code

cv2.imwrite('lena.jpg',img)

Sunday, August 15, 2021

Python OpenCV: Image Basics

 What are Images?

  • 2-dimensional representation of the visible light spectrum
SourceClick here

How do OpenCV store images?

  • OpenCV used RGB color space by default. However, it actually stores color in the BGR format.
  • Each pixel coordinate (x, y) contains 3 values ranging from 0 to 255 (8-bit)
    • RED
    • GREEN
    • BLUE
  • Mixing those primary colors produces different color spectrum.


Pixel

  • Each image consists of a set of pixels.
  • Pixels are the raw building block of an image.
  • Each pixel in the image represent intensity of the light that appears in the given place in our image.
  • Image resolution of 300 x 200 means there are 300 rows and 200 columns. Overall, there are 300 x 200 = 6000 pixels in the image.
  • Pixels are represent in two ways: grayscale and color
  • Color pixels are normally represented in the RGB color space – one value for the Red component, one for Green, and one for Blue

Grayscale

  • In grayscale, each pixel value has a value between 0 (black) and 255 (white).
  • The values in between 0 and 255 are varying shades of gray, where values closer to 0 are darker and values closer to 255 are lighter.


Friday, August 13, 2021

Virtual Environment Packages

 What is Virtual Environment?

A virtual environment is a tool that helps to keep dependencies required by different projects in separate places by creating an isolated Python virtual environments. Python virtual environment is mostly used by Python developers. The virtual environment can be created in Windows, Linux and Mac OS. 


Source: https://blog.debugeverything.com/virtual-environments-with-python-virtualenv/


Create Virtual Environment in MacOS/Linux

        python3 -m venv "name of the environment"

  • Now navigate to the environment that you have created using cd command:
  • Activate the environment using:
           source bin\activate

Create Virtual Environment in Windows

                python3 -m venv "name of the environment"
  • Now navigate to the environment that you have created using cd command:
  • Activate the environment using:
           \Scripts\activate.bat