Pier-Alexandre Bouchard
Pier-Alexandre Bouchard

Reputation: 5235

merge two binary images using Matlab

I need to create two different black binary rectangles using Matlab, to overlay a part of both and to extract the insertion.

How can I overlay two binary images?

-------|----------|
|      |     2    |
|    1 |----|-----|
|           |
|-----------|

I created my two binary images using the false(X, Y) Matlab function.

I dont find how to produce the merge the two images and to extract the insertion.

Upvotes: 1

Views: 1893

Answers (1)

engineerC
engineerC

Reputation: 2868

Make a background matrix that can contain both rectangles before you translate them, and assign values of the background matrix to the areas where your rectangles will be. This way you have two matrices of the same size, on which you can do logical or arithmetic operations. If you use different values for each rectangle and the background, things like sums will show up in different colors. Here's a text version that demonstrates:

octave:11> bga = bgb = ones(10,10)
bga =

   1   1   1   1   1   1   1   1   1   1
   1   1   1   1   1   1   1   1   1   1
   1   1   1   1   1   1   1   1   1   1
   1   1   1   1   1   1   1   1   1   1
   1   1   1   1   1   1   1   1   1   1
   1   1   1   1   1   1   1   1   1   1
   1   1   1   1   1   1   1   1   1   1
   1   1   1   1   1   1   1   1   1   1
   1   1   1   1   1   1   1   1   1   1
   1   1   1   1   1   1   1   1   1   1

octave:12> bgb
bgb =

   1   1   1   1   1   1   1   1   1   1
   1   1   1   1   1   1   1   1   1   1
   1   1   1   1   1   1   1   1   1   1
   1   1   1   1   1   1   1   1   1   1
   1   1   1   1   1   1   1   1   1   1
   1   1   1   1   1   1   1   1   1   1
   1   1   1   1   1   1   1   1   1   1
   1   1   1   1   1   1   1   1   1   1
   1   1   1   1   1   1   1   1   1   1
   1   1   1   1   1   1   1   1   1   1

octave:13> a=false(3,4)
a =

   0   0   0   0
   0   0   0   0
   0   0   0   0

octave:14> b=false(5,5)
b =

   0   0   0   0   0
   0   0   0   0   0
   0   0   0   0   0
   0   0   0   0   0
   0   0   0   0   0

octave:15> bga(3:5,4:7) = a
bga =

   1   1   1   1   1   1   1   1   1   1
   1   1   1   1   1   1   1   1   1   1
   1   1   1   0   0   0   0   1   1   1
   1   1   1   0   0   0   0   1   1   1
   1   1   1   0   0   0   0   1   1   1
   1   1   1   1   1   1   1   1   1   1
   1   1   1   1   1   1   1   1   1   1
   1   1   1   1   1   1   1   1   1   1
   1   1   1   1   1   1   1   1   1   1
   1   1   1   1   1   1   1   1   1   1

octave:16> bgb(1:5,1:5) = b
bgb =

   0   0   0   0   0   1   1   1   1   1
   0   0   0   0   0   1   1   1   1   1
   0   0   0   0   0   1   1   1   1   1
   0   0   0   0   0   1   1   1   1   1
   0   0   0   0   0   1   1   1   1   1
   1   1   1   1   1   1   1   1   1   1
   1   1   1   1   1   1   1   1   1   1
   1   1   1   1   1   1   1   1   1   1
   1   1   1   1   1   1   1   1   1   1
   1   1   1   1   1   1   1   1   1   1

octave:17> bga | bgb
ans =

   1   1   1   1   1   1   1   1   1   1
   1   1   1   1   1   1   1   1   1   1
   1   1   1   0   0   1   1   1   1   1
   1   1   1   0   0   1   1   1   1   1
   1   1   1   0   0   1   1   1   1   1
   1   1   1   1   1   1   1   1   1   1
   1   1   1   1   1   1   1   1   1   1
   1   1   1   1   1   1   1   1   1   1
   1   1   1   1   1   1   1   1   1   1
   1   1   1   1   1   1   1   1   1   1

Upvotes: 1

Related Questions