Image Color Filtering
Image files can be converted to "matrix" values in python using the "data.load([full path and name])" function. Assigning the output of this function to a variable provides access to the RGB intensity values for each pixel of the image.
So, for any pixel in the image a list of three intensity values will be given; [Red, Green, Blue]. By removing the values of the red and blue intensities the image is left with only the green values and thus acts as a filter. The following snippet of code contains a crude method of filtering the color of an image, ”image rgb”.
- image_rgb = data.load(full_path)
- colorow = np.array([[0, 1, 0]] * len(image_rgb[0]))
- image_stripped = np.array([colorow] * len(image_rgb))
- image_stripped = image_stripped * image_rgb
In line 1 the code imports an image to the variable name ”image rgb” then in lines 2 and 3 a numpy array of the same size as the ”image rgb” matrix is created, however, the red and blue values are all 0 but the green values are all 1. So that the expanded print out of the variable ”colorow” for a 2 x 3 ”image rgb” will be as follows:
[0 1 0],[0 1 0],[0 1 0] [0 1 0],[0 1 0],[0 1 0]
Because the code creates numpy arrays we are able to use the multiplication function of the two matrices on line 4 so that if we have ”image rgb” as follows:
[16 22 36],[15 25 35],[15 30 35] [17 47 23],[15 45 19],[13 43 17]
The multiplication of ”image rgb” and ”image stripped” will have a print out that will only have the green components to each pixel, as in the follow- ing:
[0 22 0],[0 25 0],[0 30 0] [0 47 0],[0 45 0],[0 43 0]
Leave a Reply