Reading image and pulling, RGB and pixel number from GIF format image
3 views (last 30 days)
Show older comments
Hello,
I am working on a code to pull the RGB values from a gif frame by frame, then load them into a file in a specific format to load into a LED lighting array. I cannot seem to get it to label the LED numbers correctly, or how I want them numbered. When running the following code through matlab it seems to label the LED numbers in a different order. Here is the code I am using.
sourceimage = '/Users/tapotter/anime/leda11.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(not working as I thought)
fprintf(fid, 'leds[%d].setRGB(%d, %d, %d);\n', crgb);
fprintf(fid, '\n FastLED.delay(%d)\n', fdelay);
end
fclose(fid);
Here is a sample of the output I am getting,
leds[0].setRGB(0, 0, 0);
leds[0].setRGB(0, 18, 20);
leds[20].setRGB(8, 8, 9);
leds[20].setRGB(34, 36, 20);
leds[34].setRGB(36, 20, 40);
leds[47].setRGB(33, 65, 70);
leds[22].setRGB(52, 54, 33);
leds[65].setRGB(70, 12, 70);
leds[80].setRGB(33, 65, 70);
It should be reading
leds[0].setRGB(0, 0, 0);
leds[1].setRGB(0, 18, 20);
leds[2].setRGB(8, 8, 9);
leds[3].setRGB(34, 36, 20);
When I look at the data that is generated for the "pixcrgb" data I can see it is correct, at least number going through the number of pixels in the image. I am attaching a screen shot of the data I am seeing through matlab. Does anyone know how I might get the required number from pixcrgb? Example at the top it is labeling the 1, 2, 3, 4, 5, followed by the RGB values. Next just to make sure I understand how the gif or image is being read, is it in this format?
// 0 > 1 > 2 > 3 > 4
// |
// .----<----<----<----'
// |
// 5 > 6 > 7 > 8 > 9
// |
// .----<----<----<----'
// |
// 10 > 11 > 12 > 13 > 14
// |
// .----<----<----<----'
// |
// 15 > 16 > 17 > 18 > 19
2 Comments
Jan
on 14 Sep 2018
Perhaps the problem is only the line:
fid = fopen('FastLEDAnimation.txt', 'a+');
This appends the data to the end of the file. If there are some wrong data from an earlier creation, they are not overwritten.
Accepted Answer
More Answers (1)
See Also
Categories
Find more on Language Support in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!