MATLAB: Does pixel index counter stop at 255 and repeat

image analysisimage processingImage Processing Toolbox

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.

Best Answer

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: