MATLAB: Extracting the XY + RGB value from an image.

animated gifgifImage Processing Toolboximpixelimreadrgb

Hello,
I am working on a program to pull the RGB values of a 22×22 gif. I am using this to turn it into a template to use on a LED matrix. I have been searching high and low and think I might be using the wrong wording or where to look.
What I want to accomplish is read a 22×22 gif(Frame by frame if possible), take out each XY and RGB value and put it in output file. I have found these examples that kind of get me where I want to go, but I cant figure out how to bring it home.
I found that I can use imread to upload the gif or image, then I can use something similar too
rgbImage = permute(rgbImage, [3,2,1]);
serialValues = rgbImage(:)';
This does not seem to return the information I need or I dont understand how to put it together. This is the example output I am getting
al(:,:,1) =
Columns 1 through 9
0.9922 0.5373 0.0667 0.0667 0.0667 0.0667 0.0667 0.1333 0.4471
0.8275 0.2000 0.0667 0.0667 0.0667 0.0667 0.0667 0.0667 0.0235
0.2471 0.1020 0.0667 0.1333 0.1333 0.1333 0.1333 0.1333 0.4627
Columns 10 through 18
0.1333 0.2980 0.3333 0.7608 0.9961 0.5373 0.0667 0.0667 0.0667
0.0667 0.2902 0.2000 0.1725 0.4275 0.2000 0.0667 0.0667 0.0667
0.2078 0.0157 0.1216 0.0157 0.3059 0.1020 0.1333 0.1333 0.1333
Columns 19 through 22
0.0667 0.0667 0.4275 0.2000
0.0667 0.0667 0.0667 0.0667
0.1333 0.0667 0.1373 0.1333
val(:,:,2) =
Columns 1 through 9
0.6314 0.2000 0.0667 0.0667 0.0667 0.0667 0.0667 0.0667 0.1373
0.3059 0.0667 0.0667 0.0667 0.0667 0.0667 0.0667 0.0667 0.1137
0.1176 0.0667 0.1333 0.1333 0.1333 0.1333 0.1333 0.1333 0.3294
Columns 10 through 18
0.2392 0.3176 0.1333 0.6980 1.0000 0.8275 0.2667 0.0667 0.0667
0.0314 0.4196 0.0667 0.4118 0.5529 0.2824 0.0667 0.0667 0.0667
0.4510 0.4235 0 0.4863 0.4431 0.3059 0.0667 0.1333 0.1333
Columns 19 through 22
0.0667 0.2667 0.3333 0.0667
0.0667 0.0667 0.0667 0.0667
0.1333 0.0667 0.0667 0.1333
Which I believe is one value, either the red green or blue. Where are the other values at? I thought it might have been from the image or not getting converted correctly but I even tried this method which I feel did not work.
Read an image and convert it to an RGB image.
Read the first image in the sample indexed image file, corn.tif.
[X,map] = imread('corn.tif');
X is a 415-by-312 array of type uint8.
Verify that the colormap, map, is not empty, and convert the data in X to RGB.
if ~isempty(map)
Im = ind2rgb(X,map);
end
View the size and class of X.
whos Im
Name Size Bytes Class Attributes
Im 415x312x3 3107520 double
X is now a 415-by-312-by-3 array of type double.
I would like it to pull the information, pull all of the RGB colors with X/Y coordinates and then put it into a file to upload into FastLED to light up the specific LEDs I want. Any other help on guiding me to a proper solution is extremely helpful. Also if someone could assist in helping me understand the array of 3,2,1 after the permute, is that column, row and then RGB value?

Best Answer

Assumming your LED array has as many leds as pixels in the GIF, then the following code should translate the GIF into the CRGB array expected by FastLed:
sourceimage = 'corn.tif';
numframes = numel(imfinfo(sourceimage); %get number of frames
for frame = 1:numframes %iterate over the frames
[img, map] = imread(sourceimage, frame); %read frame
rgbimg = im2uint8(ind2rgb(img, map)); %convert to RGB and to 8-bit integer
crgb = permute(rgbimg, [3, 1, 2]); %change memory storage from RRR...GGG...BBB... to RGBRGBRGB....
crgb = crgb(:); %convert to 1D array.
%...
%your code to pass the crgb array to fastled.
%you can copy the whole crgb array directly into your FastLed led array
%it already has the correct memory layout
%note that the pixels are rearranged by column. So led[2] is the 2nd pixel of the first column
end
Note that I don't know anything about FastLed, I've just read its documentation for the first time now, so no guarantee that the above works.