11-Matlab Images are Matrices
Read image in Matlab
What is MATLAB?Most of you, I hope, will use either MATLAB or Octave for, for the work in this class. We talked about how you can use also Python and OpenCV, etcetera. MATLAB or the open source version of it of Octave makes it easy. If you're an actually student somewhere, you know, really registered somewhere, there is a student edition of MATLAB. It is less than most textbooks and a great thing for you to purchase. So, in MATLAB, images and matrices just work really well.
Read image in Matlab :So, you know, here's all it takes to read an image. Right? So we've got this function, imread, and we're going to read in as a file peppers.png. And by the way, if you don't put these semicolons in there, MATLAB spits out all the numbers, which is incredibly painful. And then what we're going to do here is, I'm going to take just the green channel, and the green channel can be indexed because in, in MATLAB, images have a certain number of rows, certain number of columns, and you can think of this as the color planes or the layers. So when I say im of colon that means all the rows comma colon all the columns. And then 2 that's red, green, blue. So that's green. Now some of you are screaming, no, Professor Bobic, you messed up. I will certainly mess up in this class. Although I will mess up less frequently in this class then in my in class, class. Why? Because Megan hates to see me mess up. That's actually not really true, she likes to put it in there, but anyway. No, unfortunately, I did not mess up, or fortunately? I don't know. MATLAB indexing starts at one. In, sort of, normal computer, zero would be red, one would be green, two would be blue. In MATLAB indices, indexing of arrays, it starts with one. So one is red, two is green, three is blue. That means that we all the time have to be subtracting one off of indices in order to be able to multiply them to get into other locations, the computer scientists out there will know exactly what I'm talking about. Just remember that MATLAab is one based indexing. So we've got our green channel, which is just a single channel. Well, I can just show that. And if I can imshow, we'll talk about that in a minute. Well, green has just got a single layer, so it thinks of it as a grayscale image, and I am showing this case would just display it. And then you'll notice, MATLAB makes it really easy to plot things also. So it says draw a line, and it, can you see that red line in there? It just drew a line right across there. Ain't MATLAB great? In fact, I can also just call plot. So what this is, is this says, give me the 256th row. Give me all the columns and then plot it, and you'll just get a plot. By the way, I recommend highly getting used to sort of exploring your image. Plotting the values and being able to see is what's going on, what really should be going on?Â

Read image in python
There are two main ways to read an image in Python: using the OpenCV library or using the Pillow library.
When we load the image using the cv2 library, the image is by default loaded in BGR colorspace. For our use, we will convert it into RGB and GRAY colorspace using the following code.2160 x 4096 is the dimension of the image I used, where 2160 is the height, and 4096 is the width of the image. The 3 stands for each value of that pixel for a single color. In the latter code, 55 represents the intensity of the blue color, 24 for the green color, and 1 for the red color, since img was by default in BGR colorspace [2].
Using OpenCV
OpenCV is a computer vision library that contains many functions for performing operations on images and videos. To read an image using OpenCV, you can use the cv2.imread() function. This function takes the path to the image file as input and returns a NumPy array containing the pixel values of the image.
The following code shows how to read an image using OpenCV:
import cv2
# Read the image
img = cv2.imread("image.jpg")
# Display the image
cv2.imshow("Image", img)
cv2.waitKey(0)
Using Pillow
Pillow is an image processing library for Python. To read an image using Pillow, you can use the Image.open() function. This function takes the path to the image file as input and returns an Image object.
The following code shows how to read an image using Pillow:
from PIL import Image
# Open the image
img = Image.open("image.jpg")
# Display the image
img.show()
References
- [1] Introduciton of computer Vision (Data Camp)
- [2]Â Computer Vision Tutorial Series M1C1
Â