Converting gif to mif

39 次查看(过去 30 天)
Hello:
I have obtained a .m file which converts a .gif file to .mif (memory initialization file for FPGAs). I am more on the hardware side of things so I only need matlab for this conversion. The code is as follows:
%%read the image and show it
name = 'leia';
img = imread(strcat(name,'.gif'));
g = im2double(img);
g = g/max(max(g));
rgb = cat(3,g,g,g);
imshow(rgb);
%%insert into 640 x 480 region
out = ones(480,640);
out(10+(1:480),90+(1:480)) = img;
imshow(out);
%%pack pixels
x = out';
x = uint8(x(:));
n = length(x);
y = reshape(x,8,n/8)';
z = y(:,8);
for i=1:7
z = bitor(z,bitshift(y(:,i),8-i));
end
%%count runs
m = 0;
n = 1;
v = z(1);
for i=2:length(z)
if (z(i)~=v)
m = m + 1;
value(m) = v;
runlen(m) = n;
n = 1;
v = z(i);
else
n = n + 1;
end
end
m = m + 1;
value(m) = v;
runlen(m) = n;
%%information
fprintf('total runs %d\n',sum(runlen));
fprintf('number of runs %d\n',length(runlen));
nz = length(find(value==0));
nf = length(find(value==255));
fprintf('zeros %d all ones %d\n',nz,nf);
%%create mif file
dfv = 255;
fid = fopen(strcat(name,'.mif'),'w');
str = 'WIDTH=8;\nDEPTH=38400;\n\nADDRESS_RADIX=HEX;\nDATA_RADIX=HEX;\n\n';
fprintf(fid,str);
str = 'CONTENT BEGIN\n [0000..%04X] : %X;\n';
fprintf(fid,str,sum(runlen)-1,dfv);
n = 0;
for k=1:length(runlen)
if (runlen(k)==1)
str = sprintf(' %04X : %X;\n', n, value(k));
else
str = sprintf(' [%04X..%04X] : %X;\n', n, n+runlen(k)-1, value(k));
end
if (value(k) ~= dfv)
fprintf(fid,str);
end
n = n + runlen(k);
end
fprintf(fid,'END;\n');
fclose(fid);
I get an error as follows:
Subscripted assignment dimension mismatch. Error in xxxxxx (line 15) out(10+(1:480),90+(1:480)) = img;
The image I am using is a 640x480 gif attached to this post. I tried other gifs of the same size with no luck.
Any help would be much appreciated. Thanks!
  1 个评论
Olawale Akinwale
Olawale Akinwale 2019-11-5
The problem comes from the fact that img has three dimensions (one for r, one for g and one for b) but your variable "out" has just two dimensions! Maybe you might want to convert the image first to Y,cr,cb and then resize / use the two-dimensional Y array,

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2014-11-23
编辑:Image Analyst 2014-11-23
1:480 is a 480 element long vector. You need a two element long vector, [row1, row2]:
out(10+[1, size(img, 1)-1], 90 + [1, size(img, 2)-1]) = img;
in order to paste img into out. Or more readable and maintainable is this
row1 = 10;
row2 = row1 + size(img, 1) - 1;
col1 = 90;
col2 = col1 + size(img, 2) - 1;
out(row1 : row2, col1 : col2) = img;
  2 个评论
blueprint
blueprint 2014-11-23
Thanks Image Analyst! The latter approach worked with col2 = col1 + .... (assuming a typo)
Now received this error however: Error using reshape Size arguments must be real integers .
Error in xxxxx y = reshape(x,8,n/8)';
I'm assuming this has to do with the "/8" parameter. Any suggestions on how to avoid this in Matlab based on your expertise (given the above code)?
Thanks for the help in advance.
Image Analyst
Image Analyst 2014-11-23
编辑:Image Analyst 2014-11-23
You're welcome. Please mark the answer as Accepted since it worked.
What is n? It's probably not a multiple of 8. And is 8 rows times (n/8) columns going to equal rows*columns of your original image? If not, then you're changing the number of elements, which you can't do. If your original image has a million pixels, then when it's reshaped it must also have a million pixels, not any other number.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Convert Image Type 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by