I am new to Matlab and to Image Processing as well. I am working on separating background and foreground in images like this

I have hundreds of images like this, found here. By trial and error I found out a threshold (in RGB space): the red layer is always less than 150 and the green and blue layers are greater than 150 where the background is.
so if my RGB image is I and my r,g and b layers are
redMatrix = I(:,:,1);
greenMatrix = I(:,:,2);
blueMatrix = I(:,:,3);
by finding coordinates where in red, green and blue the values are greater or less than 150 I can get the coordinates of the background like
[r1 c1] = find(redMatrix < 150);
[r2 c2] = find(greenMatrix > 150);
[r3 c3] = find(blueMatrix > 150);
now I get coordinates of thousands of pixels in r1,c1,r2,c2,r3 and c3.
My questions:
-
How to find common values, like the coordinates of the pixels where red is less than 150 and green and blue are greater than 150?
I have to iterate every coordinate ofr1andc1and check if they occur inr2 c2andr3 c3to check it is a common point. but that would be very expensive.
Can this be achieved without a loop ? -
If somehow I came up with common points like
[commonR commonC]andcommonRandcommonCare both of order5000 X 1, so to access this background pixel of ImageI, I have to access firstcommonRthencommonCand then access imageIlikeI(commonR(i,1),commonC(i,1))
that is expensive too. So again my question is can this be done without loop.
Any help would be appreciated.
I got solution with @Science_Fiction answer’s
Just elaborating his/her answer
I used
mask = I(:,:,1) < 150 & I(:,:,2) > 150 & I(:,:,3) > 150;
Your approach seems basic but decent. Since for this particular image the background is composed of mainly blue so you be crude and do:
This will set those pixels which evaluate to true for > 150 to 0 and false to 1. You will have a black and white image though.
To add colour back
Should give you the colour image of face hopefully, with a pure white background. All this requires some trial and error.