MATLAB Answers

Slide matrix on a larger matrix

14 views (last 30 days)
GIO COR
GIO COR on 11 Dec 2020
Commented: Mohammad Sami on 11 Dec 2020
hello,
I am to doing some masking.
I am trying to move a matrix(B) of undefine size could be any MxM on a larger matrix (A).
For example I have 10 by 10 matrix(B) fill with zeros and i want to move this matrix through out Matrix(A) 1000x1000.
This matrix(A) 1000X1000 is a binary matrix that have circles represented by 0.
I am stuck on how to do this nested for loops to make this movement, and how to find out one if the circles in matrix(A) overlaps with the matrix(B) if dont know what side this Matrix B will be after computation.
can't use function such as
[x,y]=bwlabel(I);
so far i have this set of steps:
I= imread('circles.JPG');
g = rgb2gray(I);
IB=imbinarize(g);
[ROWS,COLUMNS]=size(IB);
imshow(IB);
temp=0;
d=0;
k=0;
t=0;
A=ones(ROWS,COLUMNS)
for i=1:ROWS % find largest diameter
for j=1:COLUMNS
if IB(i,j)==0
temp=temp+1;
if temp >= d
d=temp;
end
elseif IB(i,j)==1
temp=0;
end
end
end
u=0;
v=0;
for i=1:ROWS
for j=1:COLUMNS
if IB(i,j)==0
B=zeros(d,1);
[r,c]=size(B);
for x=1:r
for y=1:c
if IB(I+u,j+v)==0 && B(r,c)==0
how to find the overlap??
end
end
end
end
end
end

  0 Comments

Sign in to comment.

Answers (1)

Mohammad Sami
Mohammad Sami on 11 Dec 2020
You should be using convolution for this instead of for loops.
you can use the conv2 function for 2d matrices.

  3 Comments

GIO COR
GIO COR on 11 Dec 2020
thank you,
how i will be moving the smaller matrix to each section of the largest to do the convolution at each section of the largest image? or just do conv2(A,B) and all the circles with that match will disapear?
Mohammad Sami
Mohammad Sami on 11 Dec 2020
The smaller matrix is called a kernel (you can think of it as a filter) which you are applying on a bigger matrix (image) to identify a specific feature.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!