Why does pixel index counter stop at 255 and repeat?

3 views (last 30 days)
I am trying to extract the RGB value of every pixel from gif images for an LED panel. I have the formula written but once it gets to the pixel counter of 255, it just repeats 255 repeatedly for about 200 times, then restarts as expected on the next frame. Here is the code
sourceimage = '/anime/animetest2.gif'; %Pull file to load
numframes = numel(imfinfo(sourceimage)); %Pull the number of frames
fid = fopen('FastLEDAnimation.txt', 'a+');
for frame = 1:numframes %Go through each frame of the gif
fprintf('Frame: %d Complete', frame); %Post to the console when each frame is complete
[img, map] = imread(sourceimage, frame); % Open the image and create the map
rgbimg = im2uint8(ind2rgb(img, map)); %turn the index into unit8 rgb format
crgb = reshape(permute(rgbimg, [3, 1, 2]), 3, []); %makes a 3xnumpixels array
framedelay = imfinfo(sourceimage); %Pulls the frame delay from imfinfo
fdelay = framedelay.DelayTime;
pixcrgb = [1:size(crgb, 2); crgb]; %Sets up the readout to include what pixel its on but only goes through 255, then repeats 200 times with 255
fprintf(fid, 'leds[%d].setRGB(%d, %d, %d);\n', pixcrgb);
fprintf(fid, '\n FastLED.delay(%d)\n', fdelay);
end
fclose(fid);
The file seems to start off ok but once it gets to count the "255" pixel, it seems to stop counting up.
leds[1].setRGB(38, 38, 38);
leds[2].setRGB(38, 38, 38);
leds[3].setRGB(38, 38, 38);
leds[4].setRGB(38, 38, 38);
leds[5].setRGB(38, 38, 38);
leds[6].setRGB(38, 38, 38);
leds[7].setRGB(38, 38, 38);
leds[8].setRGB(38, 38, 38);
leds[9].setRGB(38, 38, 38);
leds[10].setRGB(38, 38, 38);
leds[11].setRGB(38, 38, 38);
leds[12].setRGB(38, 38, 38);
leds[13].setRGB(38, 38, 38);
Then once it gets to the '255' mark,
leds[245].setRGB(38, 38, 38);
leds[246].setRGB(38, 38, 38);
leds[247].setRGB(38, 38, 38);
leds[248].setRGB(38, 38, 38);
leds[249].setRGB(145, 99, 32);
leds[250].setRGB(229, 156, 17);
leds[251].setRGB(229, 156, 17);
leds[252].setRGB(229, 156, 17);
leds[253].setRGB(229, 156, 17);
leds[254].setRGB(229, 156, 17);
leds[255].setRGB(229, 156, 17);
leds[255].setRGB(229, 156, 17);
leds[255].setRGB(228, 156, 17);
leds[255].setRGB(112, 129, 116);
leds[255].setRGB(38, 38, 38);
leds[255].setRGB(38, 38, 38);
leds[255].setRGB(38, 38, 38);
leds[255].setRGB(38, 38, 38);
leds[255].setRGB(38, 38, 38);
I believe the issue is this part
pixcrgb = [1:size(crgb, 2); crgb];
Is the value stored here only able to go up to 255? Do I need to change the type of variable stored? I tried changing the 'size' variable input but only ended with error. I also tried several images from different sources with the same effect.
  1 Comment
Image Analyst
Image Analyst on 15 Sep 2018
This:
leds[1].setRGB(38, 38, 38);
doesn't look like actual MATLAB syntax.
Which variable are you calling the counter? Is it the for loop iterator, "frame"?
As far as I know, GIF images are 8 bit indexed images which means the value can only go up to 255.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 15 Sep 2018
Edited: Stephen23 on 15 Sep 2018
Try this:
pixcrgb = [1:size(crgb,2); double(crgb)];
^^^^^^ convert to double!
Because crgb is uint8 it forced the whole of pixcrgb to also be uint8, thus limiting the values that it can store to 255. It's one of those quirky design "features" in MATLAB:
  1 Comment
Travis Potter
Travis Potter on 15 Sep 2018
sourceimage = '/Users/tapotter/anime/ledadb.gif'; %Pull file to load
numframes = numel(imfinfo(sourceimage)); %Pull the number of frames
fid = fopen('FastLEDAnimation.txt', 'a+');
for frame = 1:numframes %Go through each frame of the gif
fprintf('Frame: %d Complete', frame); %Post to the console when each frame is complete
[img, map] = imread(sourceimage, frame); % Open the image and create the map
rgbimg = im2uint8(ind2rgb(img, map)); %turn the index into unit8 rgb format
crgb = reshape(permute(rgbimg, [3, 1, 2]), 3, []); %makes a 3xnumpixels array
framedelay = imfinfo(sourceimage); %Pulls the frame delay from imfinfo
fdelay = framedelay.DelayTime;
pixcrgb = [1:size(crgb, 2); double(crgb)]; %Sets up the readout to include what pixel its on but only goes through 255, then repeats 200 times with 255
fprintf(fid, 'leds[%d].setRGB(%d, %d, %d);\n', pixcrgb);
fprintf(fid, '\n FastLED.delay(%d)\n', fdelay);
end
fclose(fid);
This is the code that works. I am going to try and figure out now how to add a condition to check what the previous color was, then if its the same, do not add a new entry, if its a its a different color then add entry for next "frame". Thanks for the help and pointing that out!
Cheers

Sign in to comment.

More Answers (0)

Products


Release

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!