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);

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.

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.
source share