Error using sub2ind (line 43) Out of range subscript.!!

Hi evreyone
I found this program that works well until this line
""Error in test_munitie (line 96)
indTerm=sub2ind([m,n],CentroidTerm(:,1),CentroidTerm(:,2));"""
I did not understand where is the problem ? any help please
thanks
clear all,close all,clc
I=imread('Empreinte.bmp');
imshow(I)
set(gcf,'position',[1 1 600 600]);
J=I(:,:,1)>160;
imshow(J)
set(gcf,'position',[1 1 600 600]);
%% Thining
% Ridge thining is to eliminate the redundant pixels of ridges till the
% ridges are just one pixel wide.
K=bwmorph(~J,'thin','inf');
imshow(~K)
set(gcf,'position',[1 1 600 600]);
fun=@minutie;
L = nlfilter(K,[3 3],fun);
%% Termination
LTerm=(L==1);
imshow(LTerm)
LTermLab=bwlabel(LTerm);
propTerm=regionprops(LTermLab,'Centroid');
CentroidTerm=round(cat(1,propTerm(:).Centroid));
imshow(~K)
set(gcf,'position',[1 1 600 600]);
hold on
plot(CentroidTerm(:,1),CentroidTerm(:,2),'ro')
%% Bifurcation
LBif=(L==3);
LBifLab=bwlabel(LBif);
propBif=regionprops(LBifLab,'Centroid','Image');
CentroidBif=round(cat(1,propBif(:).Centroid));
plot(CentroidBif(:,1),CentroidBif(:,2),'go')
D=6;
%% Process 1
Distance=DistEuclidian(CentroidBif,CentroidTerm);
SpuriousMinutae=Distance<D;
[i,j]=find(SpuriousMinutae);
CentroidBif(i,:)=[];
CentroidTerm(j,:)=[];
%% Process 2
Distance=DistEuclidian(CentroidBif);
SpuriousMinutae=Distance<D;
[i,j]=find(SpuriousMinutae);
CentroidBif(i,:)=[];
%% Process 3
Distance=DistEuclidian(CentroidTerm);
SpuriousMinutae=Distance<D;
[i,j]=find(SpuriousMinutae);
CentroidTerm(i,:)=[];
%%
hold off
imshow(~K)
hold on
plot(CentroidTerm(:,1),CentroidTerm(:,2),'ro')
plot(CentroidBif(:,1),CentroidBif(:,2),'go')
hold off
%% ROI
% We have to determine a ROI. For that, we consider the binary image, and
% we aply an closing on this image and an erosion.
% With the GUI, I allow the use of ROI tools of MATLAB, to define manually
% the ROI.
Kopen=imclose(K,strel('square',7));
KopenClean= imfill(Kopen,'holes');
KopenClean=bwareaopen(KopenClean,5);
imshow(KopenClean)
KopenClean([1 end],:)=0;
KopenClean(:,[1 end])=0;
ROI=imerode(KopenClean,strel('disk',10));
imshow(ROI)
%%
imshow(I)
hold on
imshow(ROI)
alpha(0.5)
hold on
plot(CentroidTerm(:,1),CentroidTerm(:,2),'ro')
plot(CentroidBif(:,1),CentroidBif(:,2),'go')
hold off
%% Suppress extrema minutiae
% Once we defined the ROI, we can suppress minutiae external to this ROI.
[m,n]=size(I(:,:,1));
indTerm=sub2ind([m,n],CentroidTerm(:,1),CentroidTerm(:,2));%%%!!!!!!!ERROR
Z=zeros(m,n);
Z(indTerm)=1;
ZTerm=Z.*ROI';
[CentroidTermX,CentroidTermY]=find(ZTerm);
indBif=sub2ind([m,n],CentroidBif(:,1),CentroidBif(:,2));
Z=zeros(m,n);
Z(indBif)=1;
ZBif=Z.*ROI';
[CentroidBifX,CentroidBifY]=find(ZBif);
imshow(I)
hold on
plot(CentroidTermX,CentroidTermY,'ro','linewidth',2)
plot(CentroidBifX,CentroidBifY,'go','linewidth',2)

 Réponse acceptée

Walter Roberson
Walter Roberson le 27 Mai 2019

0 votes

Centroid information is returned with x and then y. x corresponds to column and y corresponds to rows. When you try to convert the x y to subscript you are passing in the order x y which is column row when you need to be passing in row column, which is y x

3 commentaires

Its works fine thanks
now i juste get another errors here
Table=[3*pi/4 2*pi/3 pi/2 pi/3 pi/4
5*pi/6 0 0 0 pi/6
pi 0 0 0 0
-5*pi/6 0 0 0 -pi/6
-3*pi/4 -2*pi/3 -pi/2 -pi/3 -pi/4];
for ind=1:length(CentroidTermX)
Klocal=K(CentroidTermY(ind)-2:CentroidTermY(ind)+2,CentroidTermX(ind)-2 :CentroidTermX(ind)+2);
Klocal(2:end-1,2:end-1)=0;
[i,j]=find(Klocal);
OrientationTerm(ind,1)=Table(i,j);!!!!!!
end
>>>Error:
Subscripted assignment dimension mismatc
Error in test_munitie (line 122)
OrientationTerm(ind,1)=Table(i,j);
what to do please ??
You take the centroid and extract a 5 x 5 block centered there, and zero out the middle of the block leaving only the edges. Then you assume that exactly one pixel is non-zero on those edges. There might be no non-zero pixels, or there might be several.
Is there a design reson why regionprops uses [x y] and sub2ind uses [y x]?
Or is this a "gotcha" in the way MatLab was written?
(I just happen to be using the same source code and just figured out this problem.)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Produits

Version

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by