,

Creating a Grayscale Image Filter Using NumPy

Let us consider building a simple image processing application which will apply a grayscale filter to an image. This project will further demonstrate the basics of NumPy arrays and image manipulation.

This project will help you to learn the basics of NumPy arrays and image manipulation.

Before we begin, make sure you have Python and NumPy installed on your system. You can install NumPy using pip:

pip install numpy

Step 1: Importing the Required Libraries
First, we need to import the necessary libraries:

import numpy as np
import cv2

Step 2: Loading and Displaying the Image
Next, we will load an image from disk using OpenCV and display it:

# Load the image
image_path = 'path_to_your_image.jpg'
image = cv2.imread(image_path)

# Display the original image
cv2.imshow('Original Image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

before step 3 let us try to run the above two codes combined and see how the original image looks like

Combined Code (Step 1 & Step 2)

import numpy as np
import cv2

# Load the image
image_path = 'beauty.jpg'
image = cv2.imread(image_path)

# Display the original image
cv2.imshow('Original Image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

Step 3: Converting the Image to Grayscale

Now, let’s convert the image to grayscale using NumPy:

# Convert the image to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Display the grayscale image
cv2.imshow('Grayscale Image', gray_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

Now if you also want to save the gray scale image you can just write the code –

# Save the grayscale image
cv2.imwrite('gray_image.jpg', gray_image)

Full Code:

import numpy as np
import cv2

# Load the image
#i have taken here my image path , let us see the output, which is orginal imgae
image_path = 'beauty.jpg'
image = cv2.imread(image_path)

#now when i run this code first you will see the original image and when i close the original image
#the gray scale image will come up , let us see

# Display the original image
cv2.imshow('Original Image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
#that was the orignal image
#lets move forward to change it to gray scale
# Convert the image to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Display the grayscale image
cv2.imshow('Grayscale Image', gray_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Explanation of the Full Code:

  1. Loading the Image:
    • cv2.imread(image_path): This function loads the image from the specified image_path and stores it in the variable image.
  2. Displaying the Original Image:
    • cv2.imshow('Original Image', image): This function displays the loaded image in a window titled ‘Original Image’.
    • cv2.waitKey(0): This function waits indefinitely until a key is pressed. When a key is pressed, it proceeds to the next step.
    • cv2.destroyAllWindows(): This function closes all the windows created by OpenCV.
  3. Converting to Grayscale:
    • cv2.cvtColor(image, cv2.COLOR_BGR2GRAY): This function converts the color image image to a grayscale image and stores it in the variable gray_image.
  4. Displaying the Grayscale Image:
    • cv2.imshow('Grayscale Image', gray_image): This function displays the grayscale image in a window titled ‘Grayscale Image’.
    • cv2.waitKey(0): This function waits indefinitely until a key is pressed. When a key is pressed, it proceeds to the next step.
    • cv2.destroyAllWindows(): This function closes all the windows created by OpenCV.

How Arrays are Manipulated:

  • In this code, NumPy arrays are used to represent images. NumPy arrays are like Python lists, but they can be multidimensional. In this case, a 2D array represents the grayscale image, where each element represents a pixel’s intensity (brightness).
  • When the color image is converted to grayscale, the RGB values of each pixel are combined to create a single intensity value, which is stored in the corresponding location in the grayscale array.
  • The cv2.cvtColor function efficiently performs this conversion using NumPy arrays, making it easy to manipulate and display images in different color spaces.

Conclusion,

In this tutorial, we have created a simple image processing application using Python and NumPy to apply a grayscale filter to an image. This project demonstrates the basics of NumPy arrays and image manipulation. Feel free to explore further and add more features to enhance your image processing skills!

Note:

Make sure to replace ‘path_to_your_image.jpg’ with the actual path to your image file. This project provides a solid foundation for more advanced image processing tasks using NumPy and OpenCV.

Author

Sona Avatar

Written by

Leave a Reply

Trending

CodeMagnet

Your Magnetic Resource, For Coding Brilliance

Programming Languages

Web Development

Data Science and Visualization

Career Section

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-4205364944170772"
     crossorigin="anonymous"></script>