Clear Filters
Clear Filters

Can anybody help me to solve the error from this code function it will be a nice help

3 views (last 30 days)
clc;
close all;
clear variables;
clear all;
function inv_transformed_img= KT(I)
I=imread("cameraman.tif");
I=im2double(I);
m=1;
for i=1:8:256
for j=1:8:256
for x=0:7
for y=0:7
img(x+1,y+1)=i(i+x,j+y);
end
end
k=0;
for l=1:8
img_expect{k+1}=img(:,l)*img(:,l)';
k=k+1;
end
imgexp=zeros(8:8);
for l=1:8
imgexp=imgexp+(1/8)*img_expect{l};%expectation of E[xx']
end
img_mean=zeros(8,1);
for l=1:8
img_mean=img_mean+(1/8)*img(:,l);
end
img_mean_trans=img_mean*img_mean';
img_covariance=imgexp - img_mean_trans;
[v{m},d{m}]=eig(img_covariance);
temp=v{m};
m=m+1;
for l=1:8
v{m-1}(:,l)=temp(:,8-(l-1));
end
for l=1:8
trans_img1(:,l)=v{m-1}*img(:,l);
end
for x=0:7
for y=0:7
transformed_img(i+x,j+y)=trans_img1(x+1,y+1);
end
end
mask=[1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 ];
trans_img=trans_img1.*mask;
for l=1:8
inv_trans_img(:,l)=v{m-1}'*trans_img(:,l);
end
for x=0:7
for y=0:7
inv_transformed_img(i+x,j+y)=inv_trans_img(x+1,y+1);
end
end
end
end
figure(1)
imshow(transformed_img);
figure(2)
imshow(inv_transformed_img);
end
  4 Comments
Guillaume
Guillaume on 24 Jul 2018
Edited: Guillaume on 24 Jul 2018
It's not been uploaded. However, assuming that it's the standard 'cameraman.tif' that comes with matlab, it's not needed since as said, it comes with matlab.
What is needed however, is a description of "the error from this code function". If it does issue an error, then what is the whole text of the error (everything in red). If it does not produce the right result, then what does it produce and what was wanted instead.
Guillaume
Guillaume on 24 Jul 2018
In addition, why does the function takes input I to immediately overwrite that input by an image.

Sign in to comment.

Answers (2)

Guillaume
Guillaume on 24 Jul 2018
If "the error from this code function" is caused by imread, that would be because imread does not accept string inputs, only char array:
I = imread('cameraman.tif'); %use ' instead of "
  2 Comments
Guillaume
Guillaume on 24 Jul 2018
Yes, I'm surprised it's already not supported. Strangely enough, if the second time in a few days that I see somebody on the forum passing a string to imread or imwrite.

Sign in to comment.


Walter Roberson
Walter Roberson on 24 Jul 2018
Edited: Walter Roberson on 24 Jul 2018
You have
for i=1:8:256
so i is a scalar.
Then you have
img(x+1,y+1)=i(i+x,j+y);
so you are trying to index i as if it were a 2D array.
Your error about duplicate function names is happening because your code
clc;
close all;
clear variables;
clear all;
is converting the .m file from being a function into being a script. Your script file name is KT.m . It is not permitted to have a function named the same thing as the script name when you have a function inside a script.
What you should be doing is removing the four lines I indicated.

Products

Community Treasure Hunt

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

Start Hunting!