Slide matrix on a larger matrix

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

답변 (1개)

Mohammad Sami
Mohammad Sami 2020년 12월 11일

0 개 추천

You should be using convolution for this instead of for loops.
you can use the conv2 function for 2d matrices.

댓글 수: 3

GIO COR
GIO COR 2020년 12월 11일
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 2020년 12월 11일
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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

질문:

2020년 12월 11일

댓글:

2020년 12월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by