MATLAB Answers

0

How can i get a RGB values from a picture?

Asked by caner kaya on 10 Oct 2017
Latest activity Edited by caner kaya on 11 Oct 2017

Hi,

My goal is to get the RGB values of the image taken with the webcam and I need the matlab code to accomplish this purpose. I am a new matlab user, please help me.

Best regards.

  0 Comments

Log in to comment.

3 Answers

Answer by Walter Roberson
on 10 Oct 2017
 Accepted Answer

See https://www.mathworks.com/help/supportpkg/usbwebcams/ug/acquire-images-from-webcams.html on how to work with webcams.

Once you have done the snapshot(), then just index into the resulting array to get the RGB values. For example,

img(183, 17, :)

would extract the R, G, and B components (in that order) of row 183 column 17 of the image.

  5 Comments

thank you so much, To summarize, I will associate the photo taken with the webcam with matlab using the code above and then use an appropriate function to get the RGB value of this photo.

oke, thank you and lastly, can you give me some more information on getting RGB values, please. I ask many questions, forgive me:)

Log in to comment.


Answer by Chad Greene
on 10 Oct 2017

Hi Caner,

The imread function will read any image and give you the RGB values like this:

A = imread('myimage.jpg'); 

Then the red layer is the first slice of A, blue is the second slice, and green is the third:

R = A(:,:,1); 
G = A(:,:,2); 
B = A(:,:,3); 

If you need the RGB values of a specific pixel, say, row 22, column 50, it would be:

[R(22,50) G(22,50) B(22,50)]

If you're not sure what the row and column numbers are of the pixel you want, display the image with the image function, then use ginput to click around and get the coordinates you want:

image(A) 
[col,row] = ginput

  1 Comment

Note: imread() applies to image files, not to getting images from webcams, for which you should use snap(). However, the information about color slices is relevant.

Log in to comment.


Answer by Image Analyst
on 11 Oct 2017

To build on what the others said, you should look at impixelinfo() which is a function that puts a status bar on your figure that gives you the (x,y) location and RGB values as you mouse over the displayed image.

There is also a function called impixel() that gives you the RGB value but I never use it.

  6 Comments

No, there are other ways to get RGB values from an image. But which RGB values do you want to get?

imshow(YourImage);
datacursormode('on')

Now left click on the image and notice that a box comes up showing your location and the pixels. If you left click somewhere else, it will show the values for that location. If you left click and hold the mouse and move, it will update the information as you move.

These are useful if you want to manually examine an image, but if want compute something with the image, such as average brightness, then you would not use these at all: you would access array locations like Chad and I showed.

I need to find R G B values between 0 and 255 for a picture for example if the picture pure red then R=255, G=0, B=0, I should get these values

I also have a problem, the RGB values I get are not affected by the environment. I mean I should get the * same* values even if I'm in the dark or the bright environment with the using the webcam

Log in to comment.


Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today