Is there a program to extract the the RGB values from an image according

gplus
(Travis Potter) #1

Is there a program to extract the the RGB values from an image according to how FastLED read’s the image(Serpentine)? I started writing a matlab function to do this but have been stuck for awhile. This is what I wrote,

sourceimage = ‘/Users/tapotter/anime/ledadb.gif’; %Pull gif 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);

Which almost returns the correct information. For a gif I have it starts off going through the ‘pixel’ numbers but stops at 255. Example output,

leds[1].setRGB(28, 28, 28);
leds[2].setRGB(28, 28, 28);
leds[3].setRGB(28, 28, 28);
leds[4].setRGB(28, 28, 28);
leds[5].setRGB(28, 28, 28);
leds[6].setRGB(28, 28, 28);


leds[247].setRGB(112, 134, 117);
leds[248].setRGB(112, 134, 117);
leds[249].setRGB(112, 134, 117);
leds[250].setRGB(112, 134, 117);
leds[251].setRGB(112, 134, 117);
leds[252].setRGB(112, 134, 117);
leds[253].setRGB(90, 75, 55);
leds[254].setRGB(90, 75, 55);
leds[255].setRGB(112, 134, 117);
leds[255].setRGB(112, 134, 117);
leds[255].setRGB(112, 134, 117);
leds[255].setRGB(103, 98, 90);
leds[255].setRGB(112, 134, 117);
leds[255].setRGB(103, 98, 90);

Then just repeats about 200 more times, the LED Panel I am using is a 22x22 and the gifs are the same size, so that would make it 484 pixels or LED’s to light up. I know of the ‘LEDMatrix’ software by Tyler Jones but it does not go above 16x16.

(Daniel Garcia) #2

You are making the index a unit8 - which only has a value range of 0-255

(Travis Potter) #3

Thank you very much! Here is the code if anyone wants to use it. I am going to work on it a little more to create an array, check if the pixel from the last frame is the same, if yes do not add a new entry, if no then add new pixel color to change into.

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);

(Chris Parton) #4

I made a little Codepen to convert an image to a 2D byte array: https://codepen.io/chrisparton1991/pen/vaBYmE

There are a couple of changes that could be made to make it more efficient, but it worked fine for my use case.