Basic Image Operations in Python and MATLAB: Gaussian Noise, Displaying Images, and More
Computer Vision (Part 19)
📚Chapter: 2-Image As Function
If you want to read more articles about Computer Vision, don’t forget to stay tuned because we reach end to end:) click here.
Introduction
When working with images, some of the most basic operations that are essential to get a grip on include reading the images from disk, displaying them, accessing their pixel values, and converting them from one color space to another.
This tutorial explains these basic operations.
Sections
Effect of sigma on Gaussian Noise
Displaying images in Matlab
Displaying images in Python
Section 1- Effect of sigma on Gaussian Noise
I didn’t say in the previous image what the range of our pictures would tend to be, okay? Remember I told you an image might go from, you know, 0 to 1. And that would be from the darkest black to the brightest white. Well, if I did that and I had a sigma of two. You would get black and white all over the place. And yet, when I go back here, sigma of two is just a small variation.
Whereas sigma of 64 was a big variation. Why? In this image, we have this notion of maybe minus 127 was black and plus 128 was white. When we talk about the amount of noise in an image in terms of the intensity. It has to be with respect to sort of what’s the overall range. So another reason to use doubles in your images. And to think of them as going from 0 to 1, then we can talk about a sigma of, you know, 0.1. Well, that’s a tenth of sort of going from black to white. If you want to use something arbitrary like 0 to 255. You can do that. First of all, use 0.0 to 255.0, so use a floating-point number. And then you’re going to have to say, okay, I guess a sigma of 0.1 in one case would be a sigma of, like, 25 in another case. Right, because I’ve stretched the whole thing out. So you have to worry about the magnitude of sigma with respect to the overall range of your image.
Section 2- Displaying images in Matlab
This will c, catch you number, numerous times when you go to display an image. All right, because now you have to tell the machine, okay I’ve got this image. How do you want it displayed? Matlab has a large number of ways of displaying images. If you have the imshow function which I think actually comes from the Image Processing Toolbox. You can show it this way where you’d tell it low and high. And it will display, anything with the value low or lower as black, anything higher than high as white, okay? You can also do imshow and just give it this empty array. And it will scale the image for you automatically. That is, it’ll find the minimum value in the image and say, okay, that’s going to be black. It’ll find the maximum. It’ll say, that’ll be white, and it’ll scale you. There’s another function called imagesc, for image scale. It’s a much older function. It’s not in Image Processing Toolbox, which will also display it. Don’t get caught between this question of how I display an image versus how I use an image. Just I just finished teaching part of this course here at Georgia Tech. I had some people doing, computing some gradients. They computed some gradients, which involves substructions and derivatives and all that stuff. And then one guy, he normalized his picture to go from 0 to 255, before he computed with it. You would only normalize it in order to display it, not in order to compute with it.
Section 3- Displaying and read Images in python
1- Pillow
In Python, you can use various libraries to display images. One of the commonly used libraries for image processing is Pillow
(PIL), and for displaying images, you can use the Image
module from Pillow. Here's a simple example:
from PIL import Image
from IPython.display import display # Only required if you are using a Jupyter notebook
# Open an image file
image_path = 'path/to/your/image.jpg'
img = Image.open(image_path)
# Display the image
img.show() # This will open the default image viewer
# If you are using a Jupyter notebook, you can use the display function to show the image directly in the notebook
# display(img)
If you’re working in a Jupyter notebook, using IPython.display is a convenient way to display images directly in the notebook. If you’re working in a script or another environment, the img.show() method will open the default image viewer associated with your system.
OpenCV
Let’s start by first importing the imread
method from the OpenCV library in Python [2]:
from cv2 import imread
img = imread('Images/Dog.jpg')
The imread
method returns a NumPy array, img
, that contains the image pixel values. We can check out the array’s data type and dimensions as follows:
print('Datatype:', img.dtype, '\nDimensions:', img.shape)
#
Datatype: uint8
Dimensions: (4000, 6000, 3)
The returned information tells us that the array is of data type uint8, which means that we are working with 8-bit unsigned integer values. This means that the pixels in each channel of the image can take any of 28 = 256 values, within a range from 0 to 255. This tallies exactly with the image formulation that we have reviewed above. We have also learned that the dimensions of the array are 4000 × 6000 × 3, which correspond to the number of image rows, columns and channels, respectively.
Next we shall display the image using the Matplotlib package, as well as OpenCV’s imshow
method. The latter takes as its first argument the name of the window that will contain the image, and the image to be displayed as its second argument. We will also be calling OpenCV’s waitkey
function after the image is displayed, which waits for a keyboard event for a specified amount of milliseconds. If a value of 0 is, otherwise, provided as input, the waitkey
function will wait indefinitely, allowing us to see the displayed window until a keyboard event is generated.
Using Matplotlib:
import matplotlib.pyplot as plt
plt.imshow(img)
plt.title('Displaying image using Matplotlib')
plt.show()
Using OpenCV:
from cv2 import imshow, waitKey
imshow('Displaying image using OpenCV', img)
waitKey(0)
If you are surprised with the output produced by Matplotlib and are wondering how this happened, the reason for this is that OpenCV reads and displays the image in BGR rather than RGB order [2].
Let’s connect: If you found this helpful, share your thoughts or questions in the comments! 😊
👨🏫 Learn More and Join the Community
🚀 Want to dive deeper into:
Python 🐍
Machine Learning 📊
Deep Learning📈
Statistics 📚
Computer Vision 👁️
Research & AI 🧠
Then login and enroll at Coursesteach for exclusive tutorials, notebooks, and mentoring!
👉 Follow the series and give it a 👏 if you found this helpful.
📘 Explore more:
📘 Explore more:
Course: Computer Vision Basics
📝 Notebook: Included in the GitHub Repo
📁 GitHub Repository: Stay synced with the code
💬 Are you a Computer Vision expert? Have suggestions? Drop them in the comments and contribute!
🎓 Remember — learning is a continuous journey. So keep learning, creating, and sharing. 💻✌️