Search code examples
imagematlabcolorspixel

Isolating Pixels by Color


I am new to matlab and trying to teach myself.

My first task is that I am trying to take an image and compare color values in one image to another image. In order to do that I need to gather all R, G, B values between certain thresholds and place them in a vector accordingly. My issue at the moment is how do you go through an image and isolate, for instance, pixels with an R value between [0,31], G value between [0,31], and B value between [0,31]?

Given image X I know how to find intensities within the image but when it comes to looking for the color I am at a loss. Let me know if I need to explain further.

Thanks

edit: The images are 2D .jpegs (don't know if that helps)


Solution

  • First, read your jpeg image into a Matlab 3D array (first two dimensions indicate position, third dimension indicates R,G,B):

    X = imread('image.jpg');
    

    Then:

    index = find(X(:,:,1)<=31 & X(:,:,2)<=31 & X(:,:,3)<=31);
    R = X(index);
    G = X(index + size(X,1)*size(X,2));
    B = X(index + 2*size(X,1)*size(X,2));
    

    does what you want. It uses the concept of linear indexing.