Instead its type was matlab.gra​phics.prim​itive.Imag​e.

clc;
clear all;
close all;
c0 = 2;
imgID = 5;
Img = imread(char(strcat(int2str(imgID),'.jpg'))) ;
Img = double(Img(:,:,1));
switch imgID
case 1
iterNum = 700;
lambda1 = 1.0;
lambda2 = 1.0;
nu = 0.004*255*255;
initialLSF = ones(size(Img(:,:,1))).*c0;
initialLSF(30:70,20:90) = -c0;
case 2
iterNum = 500;
lambda1 = 1.0;
lambda2 = 0.8;
nu = 0.002*255*255;
initialLSF = ones(size(Img(:,:,1))).*c0;
initialLSF(26:32,28:34) = -c0;
case 3
iterNum =800;
lambda1 = 1.0;
lambda2 = 1.0;
nu = 0.003*255*255;
initialLSF = ones(size(Img(:,:,1))).*c0;
initialLSF(15:78,32:95) = -c0;
case 4
iterNum = 500;
lambda1 = 1.0;
lambda2 = 1.0;
nu = 0.001*255*255;
initialLSF = ones(size(Img(:,:,1))).*c0;
initialLSF(53:77,46:70) = -c0;
case 5
iterNum = 700;
lambda1 = 1.0;
lambda2 = 0.8;
nu = 0.001*255*255;
initialLSF = ones(size(Img(:,:,1))).*c0;
initialLSF(47:60,86:99) = -c0;
end
u = initialLSF;
figure;imagesc(Img, [0, 255]);colormap(gray);hold on;axis off,axis equal
title('Initial contour');
[c,h] = contour(u,[0 0],'r');
pause(0.1);
timestep = .1;
mu = 1;
epsilon = 1.0;
sigma=3.0;
K=fspecial('gaussian',round(2*sigma)*2+1,sigma);
I = Img;
KI=conv2(Img,K,'same');
KONE=conv2(ones(size(Img)),K,'same')
for n=1:iterNum
uold = u;
u=RSF(u,I,K,KI,KONE, nu,timestep,mu,lambda1,lambda2,epsilon,1);
ux = norm(u-uold);
if( ux <= 15)
break
end
if mod(n,20)==0
pause(0.1);
imagesc(Img, [0, 255]);colormap(gray);hold on;axis off,axis equal
[c,h] = contour(u,[0 0],'r');
iterNum=[num2str(n), ' iterations'];
title(iterNum);
hold off;
end
end
g=imagesc(Img, [0, 255]);colormap(gray);hold on;axis off,axis equal %MARKED
[c,h] = contour(u,[0 0],'r'); %MARKED
imwrite(g, 'F:/output/filename.jpg','jpg'); %MARKED
totalIterNum=[num2str(n), ' iterations'];
title(['Final contour, ', totalIterNum]);
I = im2double(imread('output5.jpg'));
c_diag = corrcoef(I(1:end-1, 1:end-1), I(2:end, 2:end))
A=imread('output5.jpg');
ref=imread(char(strcat(int2str(imgID),'.jpg'))) ;
ssimval = ssim(A,ref);
fprintf('The SSIM value is %0.4f.\n',ssimval);

 Accepted Answer

You cannot imwrite() a image() object.
IMG_to_write = uint8(normalize(Img, 'range', [0 255]));
imwrite(IMG_to_write, 'F:/output/filename.jpg', 'jpg'));
If what you want to write is the version with the contour on it, then you can either go through a bunch of trouble to interpret the contour matrix, or you can getframe() to read the rendered data off of the screen and imwrite() that.

7 Comments

Thank you sir..
But I could not get the same image that is being displayed in imagesc..I have attached the output i got and the image I want in screenshot sir can you help me with this
If what you want to write is the version with the contour on it like you show in Screenshot (114), then you can either go through a bunch of trouble to interpret the contour matrix, or you can getframe() to read the rendered data off of the screen and imwrite() that.
This is what you meant sir
But it is not working
clc;
clear all;
close all;
c0 = 2;
imgID = 5;
Img = imread(char(strcat(int2str(imgID),'.jpg'))) ;
Img = double(Img(:,:,1));
switch imgID
case 1
iterNum = 700;
lambda1 = 1.0;
lambda2 = 1.0;
nu = 0.004*255*255;
initialLSF = ones(size(Img(:,:,1))).*c0;
initialLSF(30:70,20:90) = -c0;
case 2
iterNum = 500;
lambda1 = 1.0;
lambda2 = 0.8;
nu = 0.002*255*255;
initialLSF = ones(size(Img(:,:,1))).*c0;
initialLSF(26:32,28:34) = -c0;
case 3
iterNum =800;
lambda1 = 1.0;
lambda2 = 1.0;
nu = 0.003*255*255;
initialLSF = ones(size(Img(:,:,1))).*c0;
initialLSF(15:78,32:95) = -c0;
case 4
iterNum = 500;
lambda1 = 1.0;
lambda2 = 1.0;
nu = 0.001*255*255;
initialLSF = ones(size(Img(:,:,1))).*c0;
initialLSF(53:77,46:70) = -c0;
case 5
iterNum = 700;
lambda1 = 1.0;
lambda2 = 0.8;
nu = 0.001*255*255;
initialLSF = ones(size(Img(:,:,1))).*c0;
initialLSF(47:60,86:99) = -c0;
end
u = initialLSF;
figure;imagesc(Img, [0, 255]);colormap(gray);hold on;axis off,axis equal
title('Initial contour');
[c,h] = contour(u,[0 0],'r');
pause(0.1);
timestep = .1;
mu = 1;
epsilon = 1.0;
sigma=3.0;
K=fspecial('gaussian',round(2*sigma)*2+1,sigma);
I = Img;
KI=conv2(Img,K,'same');
KONE=conv2(ones(size(Img)),K,'same')
for n=1:iterNum
uold = u;
u=RSF(u,I,K,KI,KONE, nu,timestep,mu,lambda1,lambda2,epsilon,1);
ux = norm(u-uold);
if( ux <= 15)
break
end
if mod(n,20)==0
pause(0.1);
imagesc(Img, [0, 255]);colormap(gray);hold on;axis off,axis equal
[c,h] = contour(u,[0 0],'r');
iterNum=[num2str(n), ' iterations'];
title(iterNum);
hold off;
end
end
g=imagesc(Img, [0, 255]);colormap(gray);hold on;axis off,axis equal %MARKED
[c,h] = contour(u,[0 0],'r'); %MARKED
F=getframe; %MARKED
IMG_to_write = uint8(normalize(F, 'range', [0 255])); %MARKED
imwrite(IMG_to_write, 'F:/output/filename.jpg', 'jpg'); %MARKED
totalIterNum=[num2str(n), ' iterations'];
title(['Final contour, ', totalIterNum]);
I = im2double(imread('output5.jpg'));
c_diag = corrcoef(I(1:end-1, 1:end-1), I(2:end, 2:end))
A=imread('output5.jpg');
ref=imread(char(strcat(int2str(imgID),'.jpg'))) ;
ssimval = ssim(A,ref);
fprintf('The SSIM value is %0.4f.\n',ssimval);
g=imagesc(Img, [0, 255]);colormap(gray);hold on;axis off,axis equal
[c,h] = contour(u,[0 0],'r');
F = getframe();
IMG_to_write = F.cdata;
imwrite(IMG_to_write, 'F:/output/filename.jpg', 'jpg');
By the way, why are you reading output5 multiple times? Just read it once and manipulate the saved data.
A = imread('output5.jpg');
I = im2double(A);
c_diag = corrcoef(I(1:end-1, 1:end-1), I(2:end, 2:end));
ref = imread(sprintf('%d.jpg', imgID));
ssimval = ssim(A,ref);
But be careful: nearly all .jpg files are RGB even if they look gray. You might need to rgb2gray()
Thank you so much sir ....This is helping me a lot in finishing my project.....Can you please explain me what is ssim and how can I use it here sir

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!