Indexing errors with my function
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Abdul Rahim Mohammad
le 23 Mai 2019
Commenté : Abdul Rahim Mohammad
le 25 Mai 2019
Hello, Im trying to implement a morpholigcal hitormiss using erosion(erode_3x3 in the code) and I have a bit of trouble with my indexing, I used the erode function found in this forum post (Link: Erode), Here is my code:
function Imatch = hitormiss_3x3(I,M)
if (ndims(I)==3)
I=I(:,:,2);
end
if (isa(I,'uint8'))
I=double(I)/255;
end
% --------------- INSERT YOUR CODE BELOW -----------------
[r, c] = size(M); % initalise the size to iterate over structure element
B1s = zeros(size(M));
B0s = zeros(size(M));
for i = 1:c
for j = 1:r
if M(i,j)>0 %swapping out -1 to 0s and assign to B1
B1s(i,j) = M(i,j);
else
B1s(i,j) = 0;
end
if M(i,j)<0
B0s(i,j) = 1; %swapping out -1s to 1s and 1s to 0s and assign to B2
else
B0s(i,j) = 0;
end
end
end
imComp = imcomplement(I); %complement of I
Imatch = erode_3x3(I,B1s) & erode_3x3(imComp,B0s); %hitormiss usinng erode
% note use the erode_3x3 function call as part of your solution
return
% erode_3x3(I,S) - apply erosion using the 3x3 structure element S
function E = erode_3x3(I,S)
[p, q]=size(S);
[rows, columns] = size(I);
halfHeight = floor(p/2);
halfWidth = floor(q/2);
ErodedImage = zeros(size(I));
for col = (halfWidth + 1) : (columns - halfWidth)
for row = (halfHeight + 1) : (rows - halfHeight)
row1 = (row-halfHeight);
row2 = (row+halfHeight);
col1 = (col-halfWidth);
col2 = (col+halfWidth);
neighbourhood = I(row1:row2, col1:col2); %this is the part I think is where the indexing is wrong
pixelsInSE = neighbourhood(S);
ErodedImage(row, col) = min(pixelsInSE);
end
end
E = ErodedImage;
return
The main error I have here is, I tried changing the indexing to integers to but that didnt help either, can someone tell me whats wrong with my code and why the indexing is faulty? Thank you for your time and input.
Array indices must be positive integers or logical values.
Error in hitormiss_3x3>erode_3x3 (line 54)
pixelsInSE = neighbourhood(S);
Error in hitormiss_3x3 (line 31)
Imatch = erode_3x3(I,B1) & erode_3x3(imComp,B2); %hitormiss usinng erode
Error in hitormiss_test (line 7)
B1 = hitormiss_3x3(I,T1);
For refrence I have a test script that supplies this function with inputs which is as follows:
I = imread('hitmiss_image.tif');
I = double(I(:,:,1))/255;
T1 = [ -1 1 -1 ; 1 1 1 ; -1 1 -1 ]; % simple cross template
B1 = hitormiss_3x3(I,T1);
T2 = [ -1 0 -1 ; 0 1 0 ; -1 0 -1 ]; % centre set & zero diagonals
B2 = hitormiss_3x3(I,T2);
subplot(2,3,1); imagesc(I); colormap(gray); title('Input Image');
subplot(2,3,2); imagesc(T1); title('Template 1'); caxis([-1 1]);
subplot(2,3,3); imagesc(B1); title('Match 1');
subplot(2,3,5); imagesc(T2); title('Template 2'); caxis([-1 1]);
subplot(2,3,6); imagesc(B2); title('Match 2');
drawnow;
0 commentaires
Réponse acceptée
Jan
le 23 Mai 2019
Some of the indices are zeros.
if M(i,j)>0 %swapping out -1 to 0s and assign to B1
B1s(i,j) = M(i,j);
else
B1s(i,j) = 0;
end
...
erode_3x3(I,B1s)
But indices must be > 0 in Matlab.
3 commentaires
Jan
le 24 Mai 2019
At first I'd start with cleaning the code. You can replace:
[r, c] = size(M); % initalise the size to iterate over structure element
B1s = zeros(size(M));
B0s = zeros(size(M));
for i = 1:c
for j = 1:r
if M(i,j)>0 %swapping out -1 to 0s and assign to B1
B1s(i,j) = M(i,j);
else
B1s(i,j) = 0;
end
if M(i,j)<0
B0s(i,j) = 1; %swapping out -1s to 1s and 1s to 0s and assign to B2
else
B0s(i,j) = 0;
end
end
end
by
B1s = max(0, M);
B0s = double(M < 0);
Perhaps all you want to do is to replace:
pixelsInSE = neighbourhood(S);
by
pixelsInSE = neighbourhood(S(S~=0));
But without any meaningful comments in the code, it is impossible to guess this reliably. Remember, that all we see is the failing code. How could we know, what it should do instead?
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!