problem facing in imwrite syntax

3 次查看(过去 30 天)
if true
folder='C:\Users\Public\Videos\Sample Videos\pro\Ne';
for k = 2 : 10
% Get the base file name.
baseFileName = sprintf('img%d.jpg', k);
% Combine it with the folder to get the full filename.
fullFileName = fullfile(folder, baseFileName);
% Read the image file into an array.
imageArray = imread(fullFileName);
a=imageArray;
figure(1);
imshow(a);
title('color image');
c=rgb2gray(a);
b=imresize(c, [512 512]);
figure(2);
imshow(b);
title('original gray scale image');
n=512;
k=1;
for i=1:1:n
for j=1:2:n
if (j<=n-1)
s(i,k)=(b(i,j)+b(i,j+1))/2;
d(i,k)=b(i,j)-b(i,j+1);
end
k=k+1;
end
k=1;
end
for i=1:1:n
for j=1:1:n/2
b(i,j)=s(i,j);
b(i,j+n/2)=d(i,j);
end
end
for j=1:1:n/2
for i=1:2:n
if i<=n-1
s(k,j)=(b(i,j)+b(i+1,j))/2;
d(k,j)=b(i,j)-b(i+1,j);
end
k=k+1;
end
k=1;
end
for i=1:1:n/2
for j=1:1:n/2
b(i,j)=s(i,j);
b(i+n/2,j)=d(i,j);
end
end
figure(3);
imshow(b);
title('image after one level of compression')
for i=1:1:n/2
for j=1:2:n/2
if (j<=n/2-1)
s(i,k)=(b(i,j)+b(i,j+1))/2;
d(i,k)=b(i,j)-b(i,j+1);
end
k=k+1;
end
k=1;
end
for i=1:1:n/2
for j=1:1:n/4
b(i,j)=s(i,j);
b(i,j+n/4)=d(i,j);
end
end
for j=1:1:n/4
for i=1:2:n/2
if i<=n/2-1
s(k,j)=(b(i,j)+b(i+1,j))/2;
d(k,j)=b(i,j)-b(i+1,j);
end
k=k+1;
end
k=1;
end
for i=1:1:n/4
for j=1:1:n/4
b(i,j)=s(i,j);
b(i+n/4,j)=d(i,j);
end
end
figure(4);
imshow(b);
title('image after two level of compression')
for i=1:1:n/4
for j=1:2:n/4
if (j<=n/4-1)
s(i,k)=(b(i,j)+b(i,j+1))/2;
d(i,k)=b(i,j)-b(i,j+1);
end
k=k+1;
end
k=1;
end
for i=1:1:n/4
for j=1:1:n/8
b(i,j)=s(i,j);
b(i,j+n/8)=d(i,j);
end
end
for j=1:1:n/8
for i=1:2:n/4
if i<=n/4-1
s(k,j)=(b(i,j)+b(i+1,j)/2);
d(k,j)=b(i,j)-b(i+1,j);
end
k=k+1;
end
k=1;
end
for i=1:1:n/8
for j=1:1:n/8
b (i, j)=s(i,j);
b (i+n/8, j)=d(i,j);
end
end
b=imcrop(b,[0 0 64 64]);
figure (5);
imshow(b);
title('image after third level of compression')
b=imresize(b, [3456 4608]);
imshow(b);
imwrite(b,'C:\Users\Public\Videos\Sample Videos\pro\Newr\im%d.jpg',k);
end
end
  3 个评论
rohan
rohan 2014-4-27
sir i m attaching a file kindly look at it
rohan
rohan 2014-4-27
if true
% folder='C:\Users\Public\Videos\Sample Videos\pro\Ne';
for k = 2 : 10
% Get the base file name.
baseFileName = sprintf('img%d.jpg', k);
% Combine it with the folder to get the full filename.
fullFileName = fullfile(folder, baseFileName);
% Read the image file into an array.
imageArray = imread(fullFileName);
a=imageArray;
figure(1);
imshow(a);
title('color image');
c=rgb2gray(a);
b=imresize(c, [512 512]);
figure(2);
imshow(b);
title('original gray scale image');
n=512;
k=1;
for i=1:1:n
for j=1:2:n
if (j<=n-1)
s(i,k)=(b(i,j)+b(i,j+1))/2;
d(i,k)=b(i,j)-b(i,j+1);
end
k=k+1;
end
k=1;
end
for i=1:1:n
for j=1:1:n/2
b(i,j)=s(i,j);
b(i,j+n/2)=d(i,j);
end
end
for j=1:1:n/2
for i=1:2:n
if i<=n-1
s(k,j)=(b(i,j)+b(i+1,j))/2;
d(k,j)=b(i,j)-b(i+1,j);
end
k=k+1;
end
k=1;
end
for i=1:1:n/2
for j=1:1:n/2
b(i,j)=s(i,j);
b(i+n/2,j)=d(i,j);
end
end
figure(3);
imshow(b);
title('image after one level of compression')
for i=1:1:n/2
for j=1:2:n/2
if (j<=n/2-1)
s(i,k)=(b(i,j)+b(i,j+1))/2;
d(i,k)=b(i,j)-b(i,j+1);
end
k=k+1;
end
k=1;
end
for i=1:1:n/2
for j=1:1:n/4
b(i,j)=s(i,j);
b(i,j+n/4)=d(i,j);
end
end
for j=1:1:n/4
for i=1:2:n/2
if i<=n/2-1
s(k,j)=(b(i,j)+b(i+1,j))/2;
d(k,j)=b(i,j)-b(i+1,j);
end
k=k+1;
end
k=1;
end
for i=1:1:n/4
for j=1:1:n/4
b(i,j)=s(i,j);
b(i+n/4,j)=d(i,j);
end
end
figure(4);
imshow(b);
title('image after two level of compression')
for i=1:1:n/4
for j=1:2:n/4
if (j<=n/4-1)
s(i,k)=(b(i,j)+b(i,j+1))/2;
d(i,k)=b(i,j)-b(i,j+1);
end
k=k+1;
end
k=1;
end
for i=1:1:n/4
for j=1:1:n/8
b(i,j)=s(i,j);
b(i,j+n/8)=d(i,j);
end
end
for j=1:1:n/8
for i=1:2:n/4
if i<=n/4-1
s(k,j)=(b(i,j)+b(i+1,j)/2);
d(k,j)=b(i,j)-b(i+1,j);
end
k=k+1;
end
k=1;
end
for i=1:1:n/8
for j=1:1:n/8
b (i, j)=s(i,j);
b (i+n/8, j)=d(i,j);
end
end
b=imcrop(b,[0 0 64 64]);
figure (5);
imshow(b);
title('image after third level of compression')
b=imresize(b, [3456 4608]);
imshow(b);
imwrite(b,'C:\Users\Public\Videos\Sample Videos\pro\Newr\im%d.jpg',k);
end
end

请先登录,再进行评论。

采纳的回答

Roberto
Roberto 2014-4-27
编辑:Roberto 2014-4-27
i think it migth have to do with the %d in the string
try using a prearranged string
format = 'jpg';
str = ['C:\Users\Public\Videos\Sample Videos\pro\Newr\im' num2str(someVar) '.jpg'] ;
imwrite(b,str,format) ;
or use a uiputfile function
  3 个评论
rohan
rohan 2014-5-4
sir, can u edit my code and then send me..i tried sme ways bt still unsucessful

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2014-5-4
rohan, you can't do this:
imwrite(b,'C:\Users\Public\Videos\Sample Videos\pro\Newr\im%d.jpg',k);
imwrite is not like sprintf. You hav eto do it in two different steps:
filename = sprintf('C:\Users\Public\Videos\Sample Videos\pro\Newr\im%d.jpg',k);
imwrite(b,filename);
as I already showed you a few days ago here: http://www.mathworks.com/matlabcentral/answers/126979#comment_210616 You never answered that so I guess you didn't go back and check or you just ignored it.
  2 个评论
rohan
rohan 2014-5-4
sir ,
i have wrked on ur suggestion and finally moved on further to this code...kindly have a look at it..the images has been sucessfully compressed..what i need is to make video from this image...
can u help me in this sir.
Image Analyst
Image Analyst 2014-5-4
Since I don't have 901 images, I can offer just a few suggestions.
  1. Please indent your code. Type control-a, then control-i to properly indent it.
  2. ADD COMMENTS. No one can follow this alphabet soup of code without help.
  3. The format input argument in imwrite is not needed - it figures it out automatically from the filename.
  4. Use descriptive variable names to make it easier to follow. Names like row, column, originalImage, compressedImage, etc., not single letter variables like i,j,b,s,d,k, n, c, and a. I don't even want to attempt to remember what all those mean.
After you put in those fixes, if you want me to look at your improved code, I can do that.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by