How to change the background color of an image?

I want to remove/change background color of an image in Matlab.

Does anyone know how to do this?

Here is an example image, I want to remove the background green.


(source: frip.in )

0
source share
1 answer

The simplest answer:

 c = [70 100 70]; thresh = 50; A = imread('image.jpg'); B = zeros(size(A)); Ar = A(:,:,1); Ag = A(:,:,2); Ab = A(:,:,3); Br = B(:,:,1); Bg = B(:,:,2); Bb = B(:,:,3); logmap = (Ar > (c(1) - thresh)).*(Ar < (c(1) + thresh)).*... (Ag > (c(2) - thresh)).*(Ag < (c(2) + thresh)).*... (Ab > (c(3) - thresh)).*(Ab < (c(3) + thresh)); Ar(logmap == 1) = Br(logmap == 1); Ag(logmap == 1) = Bg(logmap == 1); Ab(logmap == 1) = Bb(logmap == 1); A = cat(3 ,Ar,Ag,Ab); imshow(A); 

enter image description here

You should change c (background color) and thresh (threshold value for c ) and find the best one that suits your background.

You can define B as a new background image. For example, adding Bb(:,:) = 255; will give you a blue background.

enter image description here

You can even define B as an image.

To detect the background, you can find the color that is most often used in the image, but this is not necessary, as it seems to me.

+4
source

All Articles