Hello All,
I have been working on a porting program to take a gif frame by frame, read each pixel and RGB value, then put it into a formatted text file. I have gotten it to work but I am trying to make the code output smaller. I am trying to read each frame, then read the previous frame to see if the pixel index number and RGB value match, if they do not match, add a new entry into the formatted file. If there is a match just skip adding a entry and check the next pixel index number and RGB value. Here is my code,
sourceimage = '/Users/tapotter/Animations/leda36.gif'; %Pull file to load
numframes = numel(imfinfo(sourceimage)); %Pull the number of frames
fida = fopen('FastLEDAnimation.txt', 'a+'); %Open files to use.
fidp = fopen('previousframe.txt', 'wt+'); %Create and open a temporary file to check values against.
for frame = 1:numframes %Go through each frame of the gif
[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; %Get the frame delay
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
P = textscan(fidp, 'leds[%d].setRGB(%d, %d, %d);\n', pixcrgb'); %Go through and see if textscan can find these values
if isempty(P) %If the textscan returns empty write to the file
fprintf(fida, 'leds[%d].setRGB(%d, %d, %d);\n', pixcrgb); end fprintf(fida, '\nFastLED.delay(%d);\nFastLED.show();\n', fdelay); %Add the delay to the formatted text
fprintf(fidp, 'leds[%d].setRGB(%d, %d, %d);\n', pixcrgb); %Add all of the current pixel 'index' numbers and RGB values for reference as the 'previous' frame next iteration
endfclose(fida); %Close the files and delete the fidp file.
fclose(fidp);delete(fidp);fprintf('\nComplete!\n\n');
Currently it does not return anything during the loop and never writes the 'fprintf(fida, 'leds[%d].setRGB(%d, %d, %d);\n', pixcrgb);' portion to the file. When looking at the previousframe.txt, it continously writes information and append's it. What I was hoping for was it would clear the previousframe.txt and only store the last frames values. I have tried a couple different loops but cant seem to wrap my head around it. Any suggestions on how I might right the loop section? Specifically this part is where I need help,
P = textscan(fidp, 'leds[%d].setRGB(%d, %d, %d);\n', pixcrgb'); %Go through and see if textscan can find these values if isempty(P) %If the textscan returns empty write to the file fprintf(fida, 'leds[%d].setRGB(%d, %d, %d);\n', pixcrgb); end fprintf(fida, '\nFastLED.delay(%d);\nFastLED.show();\n', fdelay); %Add the delay to the formatted text fprintf(fidp, 'leds[%d].setRGB(%d, %d, %d);\n', pixcrgb); %Add all of the current pixel 'index' numbers and RGB values for reference as the 'previous' frame next iteration end
Best Answer