0

Any MATLAB codes to convert an image into grayscale but keep only the green color from the image.

What I want to be my output is to keep the green color from the image and the rest is still gray

Ryan Livingston
  • 1,898
  • 12
  • 18
Jaja
  • 1
  • 2

1 Answers1

0

If you mean that you want to simply preserve the green component of the image, you can simply set the R and B components of the image to zero.

To do so, simply load the image, say, in variable my_image, and simply do the following:

>> my_image = imread('lena.bmp');
>> imshow(my_image);

enter image description here

Now,

>> [x, y, z] = size(my_image);
>> my_image(:,:,1) = zeros(x,y);
>> my_image(:,:,3) = zeros(x,y);
>> imshow(my_image);

The result would be something like below: enter image description here

If you want to display the green as grayscale, you could just do the following though:

>> my_image = imread('lena.bmp');
>> imshow(my_image(:,:,2));

enter image description here

Roney Michael
  • 3,964
  • 5
  • 30
  • 45
  • What I want to be my output is to keep the green color from the image and the rest is still gray – Jaja Sep 07 '14 at 05:20
  • @Jaja I don't understand what you want. Could you give examples fore some color values? – Daniel Sep 07 '14 at 07:50
  • 1
    @Daniel - I believe the OP wants the image to be grayscale, except for those colours that are green. Those colours should stay intact while the rest are set to their grayscale values. I believe the OP wants [this post](http://stackoverflow.com/questions/4063965/how-can-i-convert-an-rgb-image-to-grayscale-but-keep-one-color) – rayryeng Sep 07 '14 at 08:49
  • @rayryeng - you're right. – Jaja Sep 07 '14 at 09:25
  • @Daniel - what rayryeng said is what want I want to be my output. – Jaja Sep 07 '14 at 09:25
  • 1
    @Jaja - You should go to the link that I referenced. This question has already been asked and solved. – rayryeng Sep 07 '14 at 09:26
  • @rayryeng I've already saw the page. But the thing is, I can't keep the green one. – Jaja Sep 07 '14 at 09:42
  • @Jaja - Take a look at this image: http://www.comfsm.fm/~dleeling/statistics/s53/hue_circle.jpg - In the post I referenced, gnovice segmented the red colours, which is between angles 340 and 20. For green, this approximately looks like it's between 80 and 130. All you have to do is change the code so that you are searching within these angles instead. Good luck! – rayryeng Sep 07 '14 at 16:14
  • @rayryeng can I instead use this code? <<<<< 80 || pic(mm,nn,3) > 100 gsc = 0.3*pic(mm,nn,1) + 0.59*pic(mm,nn,2) + 0.11*pic(mm,nn,3); pic(mm,nn,:) = [gsc gsc gsc];>>>>> do you know how I can use this code to keep the color green??? – Jaja Sep 08 '14 at 03:19
  • @Jaja - You can, but you'd only be able to pick out one specific shade of green. Using the hue will allow you to filter all shades of green. If you want to use the RGB approach, you'd have to know the specific RGB tuple of the shade of green you want. I would honestly use gnovice's code. It's much more intuitive. – rayryeng Sep 08 '14 at 03:23
  • @rayryeng I actually don't understand the code. But still I want to use this code. Can you help me with this one?? – Jaja Sep 08 '14 at 03:32
  • @Jaja - To be honest I really don't understand the code myself. I don't know why they specifically chose the tuple of `(80, 80, 100)` to single out the red colours. I also don't know why the red component should be `<=` while the other two are `>=`. It looks like it was done heuristically, and I think it's better if you ask the actual person who wrote the code instead of me. As I said, it's better to use gnovice's code. It's much more simpler to understand. I'm going to leave it here and this will be my last comment to you. Good luck! – rayryeng Sep 08 '14 at 03:38
  • @rayryeng still thank you so much. you're a big help tho – Jaja Sep 08 '14 at 03:43