-2

I would like to replace the first color in the image with a second color, but I don't know how to do it. Any help would be appreciated! The function prototype is defined below:

student
  • 55
  • 1
  • 9
  • 1
    You may want to read **whathaveyoutried.com** & show some **respect** to the StackOverflow Community, which strongly encourages to post high quality questions, altogether with a MCVE ( a **M**inimum-**C**omplete-**V**erifiable-**E**xample of code ) showing what-you-have-tried so far. You may want to update your post, so as to meet this minimum reasonable level of quality & to show your will to respect other StackOverflow contributing members. They are professionals who love to answer good questions on MCVE-related issues. Enjoy being StackOverflow Member & do support this Community Netiquette – user3666197 Oct 26 '14 at 14:49
  • 1
    It takes no code and just a single line command with ImageMagick (free) at the command line/terminal if you are interested... – Mark Setchell Oct 26 '14 at 15:06
  • The sort of steps you would likely have to follow to do this are discussed in [this closely related question](http://stackoverflow.com/q/4063965/52738). – gnovice Oct 26 '14 at 15:30

1 Answers1

0

If you are looking for a step-by-step code, try this,

A = imread('peppers.png');
thresh = 200;
Ar = A(:,:,1);
Ag = A(:,:,2);
Ab = A(:,:,3);
logmap = zeros([size(A,1),size(A,2)]);
logmap = (Ar > thresh).* (Ab > thresh).* (Ag > thresh);
Ar(logmap == 1) = 255;
Ag(logmap == 1) = 0;
Ab(logmap == 1) = 0;
A = cat(3 ,Ar,Ag,Ab);
imshow(A);

logmap is a logical matrix (means can only have 0 and 1), logmap = (Ar > thresh).* (Ab > thresh).* (Ag > thresh); means that all the pixels with their R,B and G larger than thresh will be set to 1. then for those pixels we will set the red for 255and other colors to0`.

Rashid
  • 4,326
  • 2
  • 29
  • 54
  • oh thank you for the help but in actually i have problem in the code the color picture should not change all to the red it should stay as it is but the white color only should change to the red color . http://www.gulfup.com/?W5hL0x this is what i get – student Oct 26 '14 at 16:20
  • @3hoodM, I changed it a bit, try again and change the value of `thresh`. – Rashid Oct 26 '14 at 16:33
  • Im sorry for asking you again but i try to understand the code you write it for me and Iam not sure of the meaning of this inst logmap = (R > thresh).* (B > thresh).* (G > thresh); // do you mean by logmap finding the white color ?! sorry again and thanks – student Oct 28 '14 at 17:01
  • @3hoodM, No problem. check the answer again. if you open `logmap` you will see that the white pixels have value of `1` and others `0`. – Rashid Oct 28 '14 at 17:15
  • oh yes i try it and logmap only shows the white color and the rest is black in the image . my last question (so sorry) , if we want to control the color that we want for example not the white if we choose another color so how we will do it ?! – student Oct 28 '14 at 17:26
  • @3hoodM You should change the logmap a little bit. I think your classmate has asked the same question, so check my answer [here](http://stackoverflow.com/questions/26575827/replace-defined-colors-of-a-source-image-with-a-background-image/26576724#26576724) – Rashid Oct 28 '14 at 17:53
  • ok i did it thanks again – student Oct 28 '14 at 17:58