Color Identification in Images

5 in our case and the index.Match Images with colorWe now define a method match_image_by_color to filter all images that match the selected color.We first extract the image colors using our previously defined method get_colors in RGB format..We use the method rgb2lab to convert the selected color to a format we can compare..The for loop simply iterates over all the colors retrieved from the image.For each color, the loop changes it to lab, finds the delta (basically difference) between the selected color and the color in iteration and if the delta is less than the threshold, the image is selected as matching with the color..We need to calculate the delta and compare it to the threshold because for each color there are many shades and we cannot always exactly match the selected color with the colors in the image.By saying green, the user can mean light green, green or dark green..We need to scan through all possibilities.If we extract say 5 colors from an image, even if one color matches with the selected color, we select that image..The threshold basically defines how different can the colors of the image and selected color be.Let’s consider the case where we are trying to find images with color Green..If the threshold is too high, we might start seeing blue images in our search..Similarly, on the other hand, if the threshold is too low, then green might not even match images that have dark green in them..It’s all based on what is required in the situation at hand and we can modify the values accordingly..We need to carefully set the threshold value.Show selected imagesWe define a function show_selected_images that iterates over all images, calls the above function to filter them based on color and displays them on the screen using imshow.We will now simply call this method and let it plot the results.Filter ResultsWe call the method as follows..We will just replace the variable selected_color with COLORS['GREEN'] for Green, COLORS['BLUE'] for Blue, and COLORS['YELLOW'] for Yellow..We set the threshold value to be 60 and total colors to be extracted from image to be 5.Searching for GreenSearch for ‘GREEN”Searching for BlueSearch for ‘BLUE’Searching for YellowSearch for ‘YELLOW’ConclusionIn this article, we discussed the methodology to extract colors from an image using KMeans algorithm and then use this to search images based on colors.Hope you liked my work.. More details

Leave a Reply