KevinCameron1337
KevinCameron1337

Reputation: 517

Matlab storing values in array

I would like to read an image pixel by pixel and store how many of each pixel value there are (grayscale 0-255):

img = imread('jetplaneCor.jpg');
imgGray = rgb2gray(img);
sizex = size(imgGray,1);
sizey = size(imgGray,2);
grayArray = [0:0:255]; %Not working

for i=0:1:sizex
   for j=0:1:sizey
       pixelValue = imgGray(i,j);
       grayArray(pixelValue)=grayArray(pixelValue)+1;
   end
end

How can i allocate an array with 256 places?

Upvotes: 0

Views: 3995

Answers (2)

Luis Mendo
Luis Mendo

Reputation: 112759

You can do that easily with hist. No need to use loops:

img = imread('jetplaneCor.jpg');
imgGray = rgb2gray(img);
grayArray = hist(imgGray(:),0:255);

Upvotes: 2

Bucket
Bucket

Reputation: 7521

This will create a 1 x 256 array where every entry is a 0:

grayArray = zeroes(1, 256);

You can reference each element with:

grayArray(1, index);

Upvotes: 0

Related Questions