Reading image and pulling, RGB and pixel number from GIF format image

3 次查看(过去 30 天)
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 个评论
Jan
Jan 2018-9-14
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.
Travis Potter
Travis Potter 2018-9-14
I delete the file after every run. If that was also the case it would still be in the correct order. I believe it has something to do with
pixcrgb = [1:size(crgb, 2); crgb];
This is the part that is outputting the LED number into the data string. I just need to select the right value. Right now it is setting the value as "size" I believe. I Just dont know what to change it to for it to put the 'counter' value instead.

请先登录,再进行评论。

采纳的回答

Jan
Jan 2018-9-14
编辑:Jan 2018-9-14
Change
fprintf(fid, 'leds[%d].setRGB(%d, %d, %d);\n', crgb);
to:
fprintf(fid, 'leds[%d].setRGB(%d, %d, %d);\n', pixcrgb);
Otherwise pixcrgb = [1:size(crgb, 2); crgb]; is not considered anywhere.
  1 个评论
Travis Potter
Travis Potter 2018-9-14
Ok that worked but it seems to stop at 255, it will keep going down the list but repeat 255. Here is an example once it reaches 255.
leds[238].setRGB(8, 8, 9);
leds[239].setRGB(18, 20, 20);
leds[240].setRGB(45, 136, 145);
leds[241].setRGB(0, 0, 0);
leds[242].setRGB(0, 0, 0);
leds[243].setRGB(0, 0, 0);
leds[244].setRGB(0, 0, 0);
leds[245].setRGB(76, 139, 144);
leds[246].setRGB(20, 40, 47);
leds[247].setRGB(8, 8, 9);
leds[248].setRGB(0, 0, 0);
leds[249].setRGB(0, 0, 0);
leds[250].setRGB(0, 0, 0);
leds[251].setRGB(0, 0, 0);
leds[252].setRGB(0, 0, 0);
leds[253].setRGB(0, 0, 0);
leds[254].setRGB(0, 0, 0);
leds[255].setRGB(0, 0, 0);
leds[255].setRGB(0, 0, 0);
leds[255].setRGB(0, 0, 0);
leds[255].setRGB(0, 0, 0);
leds[255].setRGB(0, 0, 0);
leds[255].setRGB(8, 8, 9);
leds[255].setRGB(20, 34, 36);
leds[255].setRGB(74, 152, 165);
leds[255].setRGB(0, 0, 0);
leds[255].setRGB(0, 0, 0);
leds[255].setRGB(0, 0, 0);
leds[255].setRGB(0, 0, 0);
leds[255].setRGB(114, 142, 140);
leds[255].setRGB(46, 100, 96);
leds[255].setRGB(8, 8, 9);
Is there a cap of a value for the '%d'?

请先登录,再进行评论。

更多回答(1 个)

Travis Potter
Travis Potter 2018-9-14
Example gif I am using is attached. Every gif has the same thing happen in the pixcrgb, once it gets to 255 it just repeats. Could it be reading a piece of data wrong, or skipping a value now causing it to skip ?

类别

Help CenterFile Exchange 中查找有关 Computer Vision with Simulink 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by