MATLAB: Resize a matrix by finding a hole and selecting a ROI around the hole

center of matrixmatrixresizeroi

I have several matrices of different sizes(see attached) that i would like to be able to take the difference of. The values are of strain computations. They all have a hole at roughly the center of the matrix, characterized by a value of zeros. I would like to the find the center of the hole, then resize the matrix to be the size of a ROI, For Example 80×35.

Best Answer

S = load('Max densities.mat') ;
a = S.a1 ;
a = imresize(a,[100 100]) ; % resize
[nx,ny] = size(a) ;
[X,Y] = meshgrid(1:ny,1:nx) ;
C = round([ny nx]/2) ;
% Select ROI
idx = C(2)-30:C(2)+30 ;
idy = C(1)-30:C(1)+30 ;
Xi = X(idx,idy) ;
Yi = Y(idx,idy) ;
ai = a(idx,idy) ;
% Find center of the hole
idx = find(abs(ai)<=10^-3) ;
Ci = mean([Xi(idx) Yi(idx)]) ;
pcolor(Xi,Yi,ai)
hold on
plot(Ci(1),Ci(2),'*r')