Changing the limit of for loop give different result

1 visualización (últimos 30 días)
Muhammad Jabir Khan
Muhammad Jabir Khan el 12 de Sept. de 2021
Comentada: Muhammad Jabir Khan el 13 de Sept. de 2021
Hello everyone,
My problem here is with the for loop. If I put the limits of the for loop up to (i=2:100 and j=2:100) it gives us correct result. But if put the same loop for (i=2:r+1and j=2:k+1), then result is [1 1 1; 1 1 1; 1 1 1] for all values. I am dealing with an image, and below is my code.
III = rgb2gray(imread('rice.tif'));
I = double(III);
[r,k] = size(I);
xmax = max(max(max(I)));
r1 = fim(2,:);%Copy of all element in the 2nd row of fim
r2 = fim(r-1,:);c1 = fim(:,2);c2 = fim(:,k-1);b1 = [0 r1 0];b2 = [0 r2 0];b3 = [c1 fim c2];
bfim = [b1;b3;b2];%bfim = Border fuzzy image matix
bfim(1,1) = fim(1,1);bfim(r+2, k+2) = fim(r,k);bfim(1,k+2) = fim(1,k);bfim(r+2,1) = fim(r,1);
for i = 2:r+1
for j = 2:k+1
A = [bfim(i-1,j-1) bfim(i,j-1) bfim(i+1,j-1) ; bfim(i-1,j) bfim(i,j) bfim(i+1,j) ; bfim(i-1,j+1) bfim(i,j+1) bfim(i+1,j+1)]
end
end
  4 comentarios
Muhammad Jabir Khan
Muhammad Jabir Khan el 13 de Sept. de 2021
fim = I/xmax;
I am trying tto upload pic but the system not support tif file.
Muhammad Jabir Khan
Muhammad Jabir Khan el 13 de Sept. de 2021
I honestly appreciate your help.
I have mentioned below the complete code with which I am dealing. The image is in tif file and not uploaded.

Iniciar sesión para comentar.

Respuestas (2)

DGM
DGM el 12 de Sept. de 2021
Without knowing where fim comes from, I don't know. I'm going to guess that fim is another image imported the same way as the given image, or possibly derived from it. If that's the case, using double() instead of im2double() is probably going to cause you problems if you're looking at the image. Using double() will make the image floating-point, but it will still be in the range [0 255]. Tools like imshow() will display the image as solid white, because it expects floating point images to be normalized. Using im2double does both.
If that's not part of the problem, then again, i'll have to assume that there's some other issue with the missing array, since the following test has no such issues. Here's some simplification that might help:
%III = imread('cameraman.tif');
[III fim] = meshgrid(1:10,1:5)
I = im2double(III); % use im2double, not double
[r,k] = size(I);
xmax = max(max(max(I)));
% pad array by mirroring edges
bfim = [fim(2,:); fim; fim(end-1,:)];
bfim = [bfim(:,2) bfim bfim(:,end-1)];
% but treat corners differently
bfim(1,1) = fim(1,1);
bfim(r+2, k+2) = fim(r,k);
bfim(1,k+2) = fim(1,k);
bfim(r+2,1) = fim(r,1);
% the results of this loop aren't used for anything
% A is just overwritten each time
% dunno what it's supposed to do
for i = 2:r+1
for j = 2:k+1
% A = [bfim(i-1,j-1) bfim(i,j-1) bfim(i+1,j-1);
% bfim(i-1,j) bfim(i,j) bfim(i+1,j);
% bfim(i-1,j+1) bfim(i,j+1) bfim(i+1,j+1)];
% this does the same thing.
% are you sure the transpose is intended?
A = bfim(i-1:i+1,j-1:j+1).';
end
end
If this is just going to turn into an averaging filter, the transpose is irrelevant:
III = imread('cameraman.tif');
I = im2double(III);
[r,k] = size(I);
xmax = max(max(max(I)));
fim = I; % idk what fim is
bfim = [fim(2,:); fim; fim(end-1,:)];
bfim = [bfim(:,2) bfim bfim(:,end-1)];
bfim(1,1) = fim(1,1);
bfim(r+2, k+2) = fim(r,k);
bfim(1,k+2) = fim(1,k);
bfim(r+2,1) = fim(r,1);
outpict = zeros(r,k);
for i = 2:r+1
for j = 2:k+1
outpict(i,j) = sum(bfim(i-1:i+1,j-1:j+1),'all');
end
end
% since the window size is unchanging, we can defer the division used by mean()
% splitting the averaging like this avoids a lot of unnecessary divisions
outpict = outpict/9;
outpict = im2uint8(outpict); % let's assume you want it cast back to uint8
imshow(outpict) % it's blurred with a 3x3 flat kernel
If it's for something else, then ignore this.
  1 comentario
Muhammad Jabir Khan
Muhammad Jabir Khan el 13 de Sept. de 2021
I honestly appreciate your help.
I have mentioned below the complete code with which I am dealing. The image is in tif file and not uploaded.

Iniciar sesión para comentar.


Muhammad Jabir Khan
Muhammad Jabir Khan el 13 de Sept. de 2021
Below is the complete code with which I am dealing.
%% Image Processing and Fuzzy logics to detect the edge of a given image.
%This is a copy right of Krishna Prasad, IIT Delhi,New Delhi
%For further information email to kprasad.iitd@gmail.com
%More implement about the algorithm see the research paper of Tamalika and A.K.Ray
% on Threshold selection using Fuzzy set theory and other research papers
% of same author.
%%Start of coding, Symbols have their usual meaning
%% Input Image
clear all;
clc;
%A=[]; %what is this?
%piA=[]; %what is this?
%Using 16 fuzzy edge templets that show the possible direction of the edge
%s in the image and then calculating the divergence between the origin
%image and the 16 fuzzy templets.
%Take any one example and uncomment it ;
%Reading the pixel of the image using imread function of the matlab
% %for rice image
% III = rgb2gray(imread('rice.tif'));%name of the image
% II = imcrop(III,[80 30 240 200]);
%
%III = rgb2gray(imread('self_fig.tif'));%name of the image
%II = imcrop(III,[50 40 650 400]);
III = rgb2gray(imread('rice.tif'));%name of the image
%II = imcrop(III,[5 5 560 450]);
%%For Tree image;
%III = rgb2gray(imread('1.2.03.tiff'));%name of the image
% %II = imcrop(III,[35 94 430 355]);
%III = imread('1.4.09.tiff');%name of the image
% %For lena photo
% III = rgb2gray(imread('lena.tiff'));%name of the image
% II = imcrop(III,[35 94 430 355]);
I = double(III);
[r,k] = size(I);%no of row and column is I
%% Selection of the 16 fuzzy templets
a=0.3; b=0.8;
t1 = [a a a; 0 0 0; b b b];
t2 = [a a b; a b 0; b 0 0];
t3 = [b b b; 0 0 0; a a a];
t4 = [b a a; 0 b a; 0 0 b];
t5 = [b a 0;b a 0; b a 0];
t6 = [a 0 b;a 0 b; a 0 b];
t7 = [0 0 0; b b b; a a a];
t8 = [0 b a; 0 b a; 0 b a];
t9 = [a a a; b b b;0 0 0];
t10 = [a b 0; a b 0;a b 0];
t11 = [0 0 0; a a a;b b b];
t12 = [0 a b; 0 a b; 0 a b];
t13 = [b b b; a a a; 0 0 0];
t14 = [b 0 a; b 0 a; b 0 a];
t15 = [b 0 0; b 0 a; a a b];
t16 = [0 0 b; 0 b a; b a a];
%% Initization of algo
xmax = max(max(max(I)));%maximum pixel/element of the image; (why apply three max)
%converting into the fuzzy domain from the original image;
fim = I/xmax;%fim is the image data of the input image in the fuzzy domain,all value of the fim in the interval of [0 1];
%initializing the edge image as zeros matrix i.e black box;
fedgeim = zeros(r,k);%in fuzzy domain
%Increaing the boreder line of the iamge i.e to increase the row and column
%by 2 in the first and last by taking the mirror image of the immediate
%existing rows and columns respectively;
r1 = fim(2,:);%Copy of all element in the 2nd row of fim
r2 = fim(r-1,:);
c1 = fim(:,2);c2 = fim(:,k-1);
b1 = [0 r1 0];b2 = [0 r2 0];
b3 = [c1 fim c2];
bfim = [b1;b3;b2];%bfim = Border fuzzy image matix
bfim(1,1) = fim(1,1);
bfim(r+2, k+2) = fim(r,k);
bfim(1,k+2) = fim(1,k);
bfim(r+2,1) = fim(r,1);
T=bfim;
%finding Hesitation degree or intuitionstic fuzzy index
%c = input("Enter the value of pi ");
c= 0.1;
pibfim = c*(1-bfim);
pit1 = c*(1-t1);pit2 = c*(1-t2);pit3 = c*(1-t3);pit4 = c*(1-t4);pit5 = c*(1-t5);pit6 = c*(1-t6);pit7 = c*(1-t7);
pit8 = c*(1-t8);pit9 = c*(1-t9);pit10 = c*(1-t10);pit11 = c*(1-t11);pit12 = c*(1-t12);pit13 = c*(1-t13);
pit14 = c*(1-t14);pit15 = c*(1-t15);pit16 = c*(1-t16);
%Calculation of the maximum of the divergance value between the 16 templets
%and the original image of the same size let the original image denoted by
%A this A arew formed by taking the 3x3 matrix in the border matix i.e from
%bfim
%Considering the fuzzy templats as mask of size 3x3 and then we will slide
%this matix in the fuzzy matrix i.e in the fim not inj the bfim
for i = 2:290%343%%r+1
for j = 2:391%421%%k+1
A = [bfim(i-1,j-1) bfim(i,j-1) bfim(i+1,j-1) ; bfim(i-1,j) bfim(i,j) bfim(i+1,j) ; bfim(i-1,j+1) bfim(i,j+1) bfim(i+1,j+1)]
piA = [pibfim(i-1,j-1) pibfim(i,j-1) pibfim(i+1,j-1) ; pibfim(i-1,j) pibfim(i,j) pibfim(i+1,j) ; pibfim(i-1,j+1) pibfim(i,j+1) pibfim(i+1,j+1)];
B = 1-A-piA;
% [T(i-1,j-1) T(i,j-1) T(i+1,j-1) ; T(i-1,j) T(i,j) T(i+1,j) ; T(i-1,j+1) T(i,j+1) T(i+1,j+1)];
%3x3 matrix for determining the divergence with the tempelets t1,
%t2...15,16.
%we calculate the divergence of 3x3 matrix at a time and then
%taking the minimun element of the matrix for all 16 fuzzy
%tempelets;
%d1 is a matrix of 3x3 = divergence with original matix and
%fuzzy templets 1
d1 = 2 - (1-A+t1).*exp(A-t1)-(1-t1+A).*exp(t1-A)+ 2- (1-(A-t1)+pit1-piA).*exp(A-t1-(pit1-piA))-(1-(pit1-piA)+A-t1).*exp(pit1-piA-(A-t1));
min1 =min(min(d1));
%d2 is the matix of 3x3 = divergence matix with orinigal matrix and fuzzy tempelts 2.
d2 = 2 - (1-A+t2).*exp(A-t2)-(1-t2+A).*exp(t2-A)+2-(1-(A-t2)+pit2-piA).*exp(A-t2-(pit2-piA))-(1-(pit2-piA)+A-t2).*exp(pit2-piA-(A-t2));
min2 =min(min(d2));
d3 = 2 - (1-A+t3).*exp(A-t3)-(1-t3+A).*exp(t3-A)+2-(1-(A-t3)+pit3-piA).*exp(A-t3-(pit3-piA))-(1-(pit3-piA)+A-t3).*exp(pit3-piA-(A-t3));
min3 =min(min(d3));
d4 = 2 - (1-A+t4).*exp(A-t4)-(1-t4+A).*exp(t4-A)+2-(1-(A-t4)+pit4-piA).*exp(A-t4-(pit4-piA))-(1-(pit4-piA)+A-t4).*exp(pit4-piA-(A-t4));
min4 =min(min(d4));
d5 = 2 - (1-A+t5).*exp(A-t5)-(1-t5+A).*exp(t5-A)+2-(1-(A-t5)+pit5-piA).*exp(A-t5-(pit5-piA))-(1-(pit5-piA)+A-t5).*exp(pit5-piA-(A-t5));
min5 =min(min(d5));
d6 = 2 - (1-A+t6).*exp(A-t6)-(1-t6+A).*exp(t6-A)+2-(1-(A-t6)+pit6-piA).*exp(A-t6-(pit6-piA))-(1-(pit6-piA)+A-t6).*exp(pit6-piA-(A-t6));
min6 =min(min(d6));
d7 = 2 - (1-A+t7).*exp(A-t7)-(1-t7+A).*exp(t7-A)+2-(1-(A-t7)+pit7-piA).*exp(A-t7-(pit7-piA))-(1-(pit7-piA)+A-t7).*exp(pit7-piA-(A-t7));
min7 =min(min(d7));
d8 = 2 - (1-A+t8).*exp(A-t8)-(1-t8+A).*exp(t8-A)+2-(1-(A-t8)+pit8-piA).*exp(A-t8-(pit8-piA))-(1-(pit8-piA)+A-t8).*exp(pit8-piA-(A-t8));
min8 =min(min(d8));
d9 = 2 - (1-A+t9).*exp(A-t9)-(1-t9+A).*exp(t9-A)+2-(1-(A-t9)+pit9-piA).*exp(A-t9-(pit9-piA))-(1-(pit9-piA)+A-t9).*exp(pit9-piA-(A-t9));
min9 =min(min(d9));
d10 = 2 - (1-A+t10).*exp(A-t10)-(1-t10+A).*exp(t10-A)+2-(1-(A-t10)+pit10-piA).*exp(A-t10-(pit10-piA))-(1-(pit10-piA)+A-t10).*exp(pit10-piA-(A-t10));
min10 =min(min(d10));
d11 = 2 - (1-A+t11).*exp(A-t11)-(1-t11+A).*exp(t11-A)+2-(1-(A-t11)+pit11-piA).*exp(A-t11-(pit11-piA))-(1-(pit11-piA)+A-t11).*exp(pit11-piA-(A-t11));
min11 =min(min(d11));
d12 = 2 - (1-A+t12).*exp(A-t12)-(1-t12+A).*exp(t12-A)+2-(1-(A-t12)+pit12-piA).*exp(A-t12-(pit12-piA))-(1-(pit12-piA)+A-t12).*exp(pit12-piA-(A-t12));
min12 =min(min(d12));
d13 = 2 - (1-A+t13).*exp(A-t13)-(1-t13+A).*exp(t13-A)+2-(1-(A-t13)+pit13-piA).*exp(A-t13-(pit13-piA))-(1-(pit13-piA)+A-t13).*exp(pit13-piA-(A-t13));
min13 =min(min(d13));
d14 = 2 - (1-A+t14).*exp(A-t14)-(1-t14+A).*exp(t14-A)+2-(1-(A-t14)+pit14-piA).*exp(A-t14-(pit14-piA))-(1-(pit14-piA)+A-t14).*exp(pit14-piA-(A-t14));
min14 =min(min(d14));
d15 = 2 - (1-A+t15).*exp(A-t15)-(1-t15+A).*exp(t15-A)+2-(1-(A-t15)+pit15-piA).*exp(A-t15-(pit15-piA))-(1-(pit15-piA)+A-t15).*exp(pit15-piA-(A-t15));
min15 =min(min(d15));
%d16 is the matix of 3x3 = divergence matix with orinigal matrix and
%fuzzy tempelts 16.
d16 = 2 - (1-A+t16).*exp(A-t16)-(1-t16+A).*exp(t16-A)+2-(1-(A-t16)+pit16-piA).*exp(A-t16-(pit16-piA))-(1-(pit16-piA)+A-t16).*exp(pit16-piA-(A-t16));
min16 =min(min(d16));
%Selecting the minimun divergence among the 16 divergence values
%and is positioned at the center of the templets position for the
%edge iamge i.e in edgeim.
dd = [min1 min2 min3 min4 min5 min6 min7 min8 min9 min10 min11 min12 min13 min14 min15 min16];
fedgeim(i-1,j-1) = max(dd);
end
end
%We wil get the edge image in the fuzzy doamin as edgeim matrix So we have
%to tranforming back in the image pixel domain i.e in the intercal [1
% 255] domain
fedgeimmax = max(max(fedgeim));
edgeim = double((1/fedgeimmax)*(fedgeim));
% edgeimage = uint8(edgeim); %this is the matrix of edge in the 1-255
% figure, imshow(edgeimage);
% figure, imshow(uint8(I));
% T = uint8(I)
%% Out put
tt = 255*edgeim;
ttt = uint8(tt);
subplot(2,2,1),imshow(uint8(I))
title('original image');
%figure, imshow(ttt);
subplot(2,2,2),imshow(ttt)
title('Edge without threshold');
%Set a threshold
for i = 1:r
for j = 1:k
if ttt(i,j)>38
ed(i,j) = 255;
else
ed(i,j) = 0;
end
end
end
subplot(2,2,3),imshow(ed);
title('After applying threshold 45');
%applying the morphological oprators of matlab i.e bwmorph
med = bwmorph(ed,'thin');
subplot(2,2,4), imshow(med);
title('after applying morphological thin fun');
err = sum(sum((I-ed).^2)) ./ (r.*k);
%%err1 = immse(I, ed)
%%fprintf('\n The mean-squared error is %0.4f\n', err)
%%peaksnr = psnr(I, ed)
%%fprintf('\n The Peak-SNR value is %0.4f', peaksnr);;
peaktosnr=(10).*log10((255.^2)./err); %%this formula from Moh Danish paper on div for edge detection

Categorías

Más información sobre Fuzzy Logic Toolbox en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by