Question

What I want to do is to put one image on a second image, but only those parts that are not completely black. How could i do that in Matlab?

What I have now is this:

image

So what I basically want is the same thing without the black border around the white sheet of paper. This is my code:

resizedBg(startrow:startrow+size(rscrop,1)-1, startcol:startcol+size(rscrop,2)-1, :) = rscrop;

Thanks in advance!

Was it helpful?

Solution

Basically, you can act as if you have a mask.

Let be your first image A and the second one B. You want fill with B everywhere A is completely black. It translates directly into Matlab as:

C = zeros(size(B,1), size(B,2),3);
C(startRow:startRow+size(A,1)-1, startCol:startCol+size(A,2)-1,:) = A;
M = repmat(sum(C,3)==0,1,1,3);
C(M) = B(M);

I expand the mask M over the 3 dimensions to be usable directly as a mask.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top