how to generate two random binary images with remove overlapping ?

조회 수: 7 (최근 30일)
hi,
I am looking to creat two random binary images with remove overlapping. the second image should be random and not contact or overlape with first one.I have did this code:
clc; clear all;
A=[0 0 0 0 ;...
0 0 0 0 ;...
0 0 0 0 ;...
0 0 0 0 ;...
0 0 0 0];
N =2;
for k= 1:N
if k>1
(A(y0:y1,x0:x1)+1)==[];
if A>=0
X(k,:) = randi([1,min(size(A))],1,2);
Y(k,:) = randi([1,min(size(A))],1,2);
x0= min(X(k,:));
x1= max(X(k,:));
y0= min(Y(k,:));
y1= max(Y(k,:));
A(y0:y1,x0:x1)=1;
xyStart(k,:) = [y0,x0];
xyEnd(k,:) = [y1,x1];
end
else
X(k,:) = randi([1,min(size(A))],1,2);
Y(k,:) = randi([1,min(size(A))],1,2);
x0= min(X(k,:));
x1= max(X(k,:));
y0= min(Y(k,:));
y1= max(Y(k,:));
A(y0:y1,x0:x1)=1;
xyStart(k,:) = [y0,x0];
xyEnd(k,:) = [y1,x1];
end
end
this code work with first one but does not work with the seconed image, could you please help me?
one example of expect answer:(Note it are random)
A=[1 1 0 0 ;...
1 1 0 0 ;...
0 0 0 0 ;...
0 1 1 1 ;...
0 1 1 1];
  댓글 수: 2
Akira Agata
Akira Agata 2019년 12월 5일
I'm not clearly understainding your question.
As for generating random binary matrix, you can use randi function to easily generate it.
For example, if you want to generate two 5-by-4 random binary matrix, say A and B, you can do this by the following two-line code:
A = randi([0 1],5,4);
B = randi([0 1],5,4);
But what do you mean by 'with remove overlapping' ?
Mohammed Alammar
Mohammed Alammar 2019년 12월 5일
Dear,
I would like to thank you. I mean when I have to create random two binary images using the code below:
clc; clear all;
A=[0 0 0 0 ;...
0 0 0 0 ;...
0 0 0 0 ;...
0 0 0 0 ;...
0 0 0 0];
N =2;
for k= 1:N
X(k,:) = randi([1,min(size(A))],1,2);
Y(k,:) = randi([1,min(size(A))],1,2);
x0= min(X(k,:));
x1= max(X(k,:));
y0= min(Y(k,:));
y1= max(Y(k,:));
A(y0:y1,x0:x1)=1;
xyStart(k,:) = [y0,x0];
xyEnd(k,:) = [y1,x1];
end
However, there are possible overlap between the two binary images that i need to remove it. Also, ther are possible to be two binary image connecting together in the one or more boundries which I do not need it ,too.
I need to create two completely separate random binary images like this:
A=[1 1 0 0 ;... or may be A=[0 1 1 1 ;...
1 1 0 0 ;... 0 0 0 0 ;...
0 0 0 0 ;... 0 0 0 0 ;...
0 1 1 1 ;... 1 1 1 0 ;...
0 1 1 1]; 1 1 1 0];

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

채택된 답변

Akira Agata
Akira Agata 2019년 12월 6일
OK, maybe I could understand your intention.
How about the following way?
nRow = 5;
nCol = 4;
while true
% Prepare a blank binary image
A = false(nRow,nCol);
% Put two rectangle randomly
for kk2 = 1:2
r = randi(nRow);
c = randi(nCol);
h = randi(nRow - r + 1);
w = randi(nCol - c + 1);
A(r:r+h-1,c:c+w-1) = true;
end
% If these two rectangles are separated, then stop the process
s = regionprops(A);
if numel(s) == 2
break;
end
end
  댓글 수: 1
Image Analyst
Image Analyst 2019년 12월 6일
OK, since he accepted, I guess that's what he meant. I'll try to rephrase it for others because it was confusing when he's asking about two binary images and then to "remove it". I believe it should say:
"I'd like to create one binary image with two distinct (non-overlapping) regions in it by randomly creating these regions in the image. The algorithm should create two regions in an image, and if these regions overlap or touch each other in the image, then discard that image and generate a new image (try again)."

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

추가 답변 (2개)

Image Analyst
Image Analyst 2019년 12월 5일
You can just do this, if I understood correctly:
% Make a binary image of the combination of the images, but without the overlapping pixels
bw3 = xor(bw1, bw2);
% Or if you want each without the union...
overlapping = bw1 & bw2;
bw1a = bw1 & ~overlapping;
bw2a = bw2 & ~overlapping;

Image Analyst
Image Analyst 2019년 12월 5일
Still not sure what you want. Please give the two input binary images, and give the output you want.
Do you just want to crop out each 1 region into its own image, like I do in my Image Processing Tutorial in my File Exchange?
  댓글 수: 1
Mohammed Alammar
Mohammed Alammar 2019년 12월 6일
dear,
when the input is
A=zeros(5,4);
I need to create two squares or rectangles each one has random location and size in (A) matrix without any contact together or overlap at all.
like this:
A=[1 1 0 0 ;... or may be A=[0 1 1 1 ;...
1 1 0 0 ;... 0 0 0 0 ;...
0 0 0 0 ;... 0 0 0 0 ;...
0 1 1 1 ;... 1 1 1 0 ;...
0 1 1 1]; 1 1 1 0];

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

카테고리

Help CenterFile Exchange에서 Sparse Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by