Getting error in Matlab while try to plot the values from a row of the image

Viewed 52

I want to write a code in Matlab. There is a picture of size (258, 320). It is a color picture. I want to show the image using one color channel. Try to plotting the values from a row of the image.

% Color planes
img = imread('fruit.png');
imshow(img);

disp(size(img));

% TODO: Select a color plane, display it, inspect values from a row
img_blue = img(:,:,2);
imshow(img_blue);
plot(img_blue(150,0));

I got this error error: img_blue(_,0): subscripts must be either integers 1 to (2^31)-1 or logicals error: execution exception in main.m

Can anyone please help m to overcome the problem.

2 Answers

Your problem is the fact that you're using the indexing from, I guess, Python (as in between 0 and N-1) while Matlab uses indexes starting from 1.

So if you want to plot the first plane, it will be plot(img_blue(150, 1)); instead of plot(img_blue(150, 0));. Similarly, blue is the third main color, so it will actually be img_blue = img(:, :, 3); instead of img_blue = img(:, :, 2);.

Typically I would display the image of a blue channel by filling in the other channels/layers of the array with 0 before calling imshow(), but that's just personal preference. To plot the first row of an image you can index the image (1,:). Keep in mind MATLAB’s start index for arrays starts at 1 unlike many languages that are base 0 index. Typically the third channel is the blue channel especially in RGB format. At times in libraries such as OpenCV the BGR convention is alternatively used.

RGB Channel Conventions

RGB Channel Conventions


Plotting the Blue Channel

Plotting Blue Channel of Image

Image = imread('peppers.png');
[Image_Height,Image_Width,Number_Of_Channels] = size(Image);

Blue_Channel = Image(:,:,3);

Blue_Image(:,:,1) = 0;
Blue_Image(:,:,2) = 0;
Blue_Image(:,:,3) = Blue_Channel;

subplot(1,2,1); imshow(Blue_Image);
title("Blue Channel");
xlabel(num2str(Image_Width) + " Columns"); ylabel(num2str(Image_Height) + " Rows");

Row_Number = 1;
subplot(1,2,2); plot(Blue_Channel(Row_Number,:));
title("Blue Channel Intensities (Row " + num2str(Row_Number) + ")");
xlabel("Column Index"); ylabel("Intensity Value");

Ran using MATLAB R2019b

Related