How to stock a previously calculated variables so I can reuse them to draw a curve?

1 次查看(过去 30 天)
Here is my code :
clc;
close all;
%Read color image and convert it to gray level image
%Display the original image
imagefiles = dir('*.jpg');
nfiles = length(imagefiles); % Number of image files found
for ii=1:nfiles
currentfilename = imagefiles(ii).name;
I = imread(currentfilename);
myimage = rgb2gray(I);
subplot(2,2,1);
imshow(myimage);title('Original Image');
treshold = [0.08] ;
sigma = 1;
%Apply Sobel Operator
%Display both Edges
sobel = edge(myimage,'sobel',treshold,'both');
subplot(2,2,2);
imshow(sobel,[]);title('Sobel');
%Apply Canny Operator
cannyedge = edge(myimage,'Canny',treshold,sigma);
subplot(2,2,3);
imshow(cannyedge,[]);title('Canny edge');
%Calcul du PSNR
A = im2double(myimage);
B = im2double(sobel);
C = im2double(cannyedge);
error_diff = A - B;
PSNRsobel = 20*log10(1/(sqrt(mean(mean(error_diff.^2)))));
fprintf('PSNRsobel = +%5.2f dB\n',PSNRsobel);
error_diff = A - C;
PSNRcanny = 20*log10(1/(sqrt(mean(mean(error_diff.^2)))));
fprintf('PSNRcanny = +%5.2f dB\n',PSNRcanny);
figure;
x = [];
y = [];
for i = 1:nfiles
x = [x,PSNRsobel];
y = [y,PSNRcanny];
y1 = linspace(0,1,nfiles);
plot(y1(i),x(i),y1(i),y(i));
end
%Calcul du SSIM
[ssimsobel] = ssim(A,B);
fprintf('The SSIM sobel value is %0.4f.\n',ssimsobel);
[ssimcanny] = ssim(A,C);
fprintf('The SSIM Canny value is %0.4f.\n',ssimcanny);
figure;
Y1 = [];
Y2 = [];
for i = 1 : nfiles
X = linspace(0,1,nfiles);
Y1 = [Y1,ssimsobel;
Y2 = [Y2,ssimcanny];
plot(X(i),Y1(i),X(i),Y2(i));
end
end
I want to get the values of PSNRsobel and PSNRcanny of all images in one array and then plot them and the same thing with the values of ssimsobel and ssimcanny. Please can someone help me out?

采纳的回答

Stephan
Stephan 2018-11-2
编辑:Stephan 2018-11-2
Hi,
this should do the job:
%Read color image and convert it to gray level image
%Display the original image
imagefiles = dir('*.jpg');
nfiles = length(imagefiles); % Number of image files found
stored_values = zeros(nfiles,4); % Preallocate the array for saving the values
for ii=1:nfiles
currentfilename = imagefiles(ii).name;
I = imread(currentfilename);
myimage = rgb2gray(I);
subplot(2,2,1);
imshow(myimage);title('Original Image');
treshold = 0.08;
sigma = 1;
%Apply Sobel Operator
%Display both Edges
sobel = edge(myimage,'sobel',treshold,'both');
subplot(2,2,2);
imshow(sobel,[]);title('Sobel');
%Apply Canny Operator
cannyedge = edge(myimage,'Canny',treshold,sigma);
subplot(2,2,3);
imshow(cannyedge,[]);title('Canny edge');
%Calcul du PSNR
A = im2double(myimage);
B = im2double(sobel);
C = im2double(cannyedge);
error_diff = A - B;
PSNRsobel = 20*log10(1/(sqrt(mean(mean(error_diff.^2)))));
fprintf('PSNRsobel = +%5.2f dB\n',PSNRsobel);
error_diff = A - C;
PSNRcanny = 20*log10(1/(sqrt(mean(mean(error_diff.^2)))));
fprintf('PSNRcanny = +%5.2f dB\n',PSNRcanny);
stored_values(ii,1) = PSNRsobel; % All PSNRsobel values in 1. column
stored_values(ii,2) = PSNRcanny; % All PSNRcanny values in 2. column
%Calcul du SSIM
ssimsobel = ssim(A,B);
fprintf('The SSIM sobel value is %0.4f.\n',ssimsobel);
ssimcanny = ssim(A,C);
fprintf('The SSIM Canny value is %0.4f.\n',ssimcanny);
stored_values(ii,3) = ssimsobel; % All ssimsobel values in 3. column
stored_values(ii,4) = ssimcanny; % All ssimcanny values in 4. column
end
stored_values is an array of size [number files x 4]. For every Picture there is a new row, corresponding to the filenumber in imagefiles. The 4 columns in each row save the values for every file. Then you can plot the values with:
% Plot PSNRsobel and PSNRcanny
figure(1)
plot(1:nfiles, stored_values(:,1),1:nfiles,stored_values(:,2))
% Plot ssimsobel and ssimcanny
figure(2)
plot(1:nfiles, stored_values(:,3),1:nfiles,stored_values(:,4))
Best regards
Stephan
  11 个评论
Stephan
Stephan 2018-11-2
Try:
%Read color image and convert it to gray level image
%Display the original image
imagefiles = dir('*.jpg');
nfiles = length(imagefiles); % Number of image files found
stored_values = zeros(nfiles,4); % Preallocate the array for saving the values
for ii=1:nfiles
currentfilename = imagefiles(ii).name;
I = imread(currentfilename);
myimage = rgb2gray(I);
figure(ii)
subplot(2,2,1);
imshow(myimage);title('Original Image');
treshold = 0.08;
sigma = 1;
%Apply Sobel Operator
%Display both Edges
sobel = edge(myimage,'sobel',treshold,'both');
subplot(2,2,2);
imshow(sobel,[]);title('Sobel');
%Apply Canny Operator
cannyedge = edge(myimage,'Canny',treshold,sigma);
subplot(2,2,3);
imshow(cannyedge,[]);title('Canny edge');
%Calcul du PSNR
A = im2double(myimage);
B = im2double(sobel);
C = im2double(cannyedge);
error_diff = A - B;
PSNRsobel = 20*log10(1/(sqrt(mean(mean(error_diff.^2)))));
fprintf('PSNRsobel = +%5.2f dB\n',PSNRsobel);
error_diff = A - C;
PSNRcanny = 20*log10(1/(sqrt(mean(mean(error_diff.^2)))));
fprintf('PSNRcanny = +%5.2f dB\n',PSNRcanny);
stored_values(ii,1) = PSNRsobel; % All PSNRsobel values in 1. column
stored_values(ii,2) = PSNRcanny; % All PSNRcanny values in 2. column
%Calcul du SSIM
ssimsobel = ssim(A,B);
fprintf('The SSIM sobel value is %0.4f.\n',ssimsobel);
ssimcanny = ssim(A,C);
fprintf('The SSIM Canny value is %0.4f.\n',ssimcanny);
stored_values(ii,3) = ssimsobel; % All ssimsobel values in 3. column
stored_values(ii,4) = ssimcanny; % All ssimcanny values in 4. column
end
% Plot PSNRsobel and PSNRcanny
figure(nfiles+1)
plot(1:nfiles, stored_values(:,1) ,1:nfiles,stored_values(:,2))
% Plot ssimsobel and ssimcanny
figure(nfiles+2)
plot(1:nfiles, stored_values(:,3), 1:nfiles,stored_values(:,4))
I think we got it now.
Nour Sd
Nour Sd 2018-11-2
I really don't know how to thank you enough! It finally works! Thank you a lot! The best version is here now:
clc;
close all;
%Read color image and convert it to gray level image
%Display the original image
imagefiles = dir('*.jpg');
nfiles = length(imagefiles); % Number of image files found
stored_values = zeros(nfiles,4); % Preallocate the array for saving the values
for ii=1:nfiles
currentfilename = imagefiles(ii).name;
I = imread(currentfilename);
myimage = rgb2gray(I);
figure(ii)
subplot(2,2,1);
imshow(myimage);title('Original Image');
treshold = 0.08;
sigma = 1;
%Apply Sobel Operator
%Display both Edges
sobel = edge(myimage,'sobel',treshold,'both');
subplot(2,2,2);
imshow(sobel,[]);title('Sobel edge');
%Apply Canny Operator
cannyedge = edge(myimage,'Canny',treshold,sigma);
subplot(2,2,3);
imshow(cannyedge,[]);title('Canny edge');
%Calcul du PSNR
A = im2double(myimage);
B = im2double(sobel);
C = im2double(cannyedge);
error_diff = A - B;
PSNRsobel = 20*log10(1/(sqrt(mean(mean(error_diff.^2)))));
fprintf('PSNRsobel = +%5.2f dB\n',PSNRsobel);
error_diff = A - C;
PSNRcanny = 20*log10(1/(sqrt(mean(mean(error_diff.^2)))));
fprintf('PSNRcanny = +%5.2f dB\n',PSNRcanny);
stored_values(ii,1) = PSNRsobel; % All PSNRsobel values in 1. column
stored_values(ii,2) = PSNRcanny; % All PSNRcanny values in 2. column
%Calcul du SSIM
ssimsobel = ssim(A,B);
fprintf('The SSIM sobel value is %0.4f.\n',ssimsobel);
ssimcanny = ssim(A,C);
fprintf('The SSIM Canny value is %0.4f.\n',ssimcanny);
stored_values(ii,3) = ssimsobel; % All ssimsobel values in 3. column
stored_values(ii,4) = ssimcanny; % All ssimcanny values in 4. column
end
% Plot PSNRsobel and PSNRcanny
figure(nfiles+1)
plot(1:nfiles, stored_values(:,1) ,1:nfiles,stored_values(:,2))
% Add a legend and axis labels
title('PSNR of images')
legend('PSNRSobel', 'PSNRCanny')
xlabel('Number of images')
ylabel('PSNR')
% Plot ssimsobel and ssimcanny
figure(nfiles+2)
plot(1:nfiles, stored_values(:,3), 1:nfiles,stored_values(:,4))
% Add a legend and axis labels
title('SSIM of images')
legend('SSIMSobel', 'SSIMCanny')
xlabel('Number of images')
ylabel('SSIM')
Thank you again Stephan Jung!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Image Segmentation and Analysis 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by