1

Do you have any idea how can I determine the proportion of Yellow (or Yellowish), Brown, and Red colour in a specific image? I tried to use HSV, but I could not find any threshold for H, S, and V for the aforementioned colours.

I attached a sample image.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
Ebiee
  • 11
  • 1
  • Could you upload the image? Also, please show what you've tried, even if it didn't work. – Mad Physicist Jan 18 '19 at 02:43
  • Hi, I tried to find a boundary for the aforementioned colours (Yellow and Brown) in RGB and HSV. However, I could not find a reliable segmentation using them. Unfortunately, I cannot attach a file here, but it does not matter (any image should be fine). – Ebiee Jan 21 '19 at 00:35

1 Answers1

0

So, let's do this one step at a time.

First how to know which color is represented by what value? For that, I referred to this stackoverflow question, from where you can get this HSV color map,

enter image description here

If you DuckDuckGo/Google/search for "HSV or HSL color map", you could find many examples.

  • Now, we can pick a color from the along the horizontal axis. We can use a single value e.g. 120 for dark blue or we can use a range of values e.g. 45 to 80 for all hues of green.

  • What I wasn't sure about was, how to define the,

proportion of Yellow (or Yellowish), Brown, and Red colour in a specific image

as you ask in your question.

I thought of then two ways to represent the color proportion.

  1. Proportion of pixels containing some portion of that hue.
  2. Proportion of the specific hue relative to all the hues in the image.

Then using the following script, you could get some numbers:

NOTE: (This the Python script, I originally posted. The corresponding Matlab script is further down the post.)

import cv2 
import numpy as np

img = cv2.imread("D:\\lenna.jpg")

height_img, width_img, channels_img = img.shape

# Converts images from RGB to HSV 
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) 

mask1 = cv2.inRange(hsv, (150, 0, 0), (150, 255,255)) #150 seems like pinkish 
mask2 = cv2.inRange(hsv, (1,0,0), (20, 255, 255)) #1 to 20 seems orangish

total_num_of_pixels = height_img * width_img
all_colors = np.sum(hsv[:,:,:] > 0)
num_of_pixels_with_pinkish_component = np.sum(mask1 > 0)
num_of_pixels_with_orangish_component = np.sum(mask2 > 0)

print("%age of pixels with pinkish component:", "{:.2f}".format(num_of_pixels_with_pinkish_component/total_num_of_pixels * 100))
print("%age of pixels with orangish component:", "{:.2f}".format(num_of_pixels_with_orangish_component/total_num_of_pixels * 100))

print("%age of pinkish component in the entire HSV image:", "{:.2f}".format(num_of_pixels_with_pinkish_component/all_colors * 100))
print("%age of orangish in the entire HSV image:", "{:.2f}".format(num_of_pixels_with_orangish_component/all_colors * 100))   

# To visualize the results
res1 = cv2.bitwise_and(img, img, mask=mask1)
res2 = cv2.bitwise_and(img, img, mask=mask2)

cv2.imshow('img', img) 
cv2.imshow('mask1', mask1)
cv2.imshow('mask2', mask2)
cv2.imshow('res1', res1)
cv2.imshow('res2', res2)

# To save the output
cv2.imwrite('D:\\mask1.png', mask1)
cv2.imwrite('D:\\mask2.png', mask2)
cv2.imwrite('D:\\res1.png', res1)
cv2.imwrite('D:\\res2.png', res2) 
  • Output:

%age of pixels with pinkish component: 0.41

%age of pixels with orangish component: 35.58

%age of pinkish component in the entire HSV image: 0.15

%age of orangish in the entire HSV image: 13.27

  • Here is how the output looks then:

MASK1 (150 on the hue axis seems pinkish) enter image description here

MASK2 (1 ~ 20 on the hue axis seems orange) enter image description here

RES1

enter image description here

RES2

enter image description here

Here is the equivalent MATLAB script.

close all;
clear all;
clc;

img = imread("/home/junglefox/Downloads/lenna.png");
figure, imshow(img), title('original image (RGB)');

img_size = size(img);
hsv_img = rgb2hsv(img);
hsv_img = im2uint8(hsv_img);
figure, imshow(hsv_img), title('original image in HSV');

% Orange component between 1 and 20 on the HSV map
minval = [1 0 0]; %// Define three element vector here for each colour plane i.e. [0 128 128];
maxval = [20 255 255]; %// Define three element vector here for each colour plane i.e. [0 128 128];

out = true(img_size(1), img_size(2));
for p = 1 : 3
    out = out & (hsv_img(:,:,p) >= minval(p) & hsv_img(:,:,p) <= maxval(p));
end

figure, imshow(out), title('image of orange component in image only');

total_num_of_pixels = img_size(1) * img_size(2);
all_colors = sum(hsv_img(:,:,:) > 0);

num_of_pixels_with_orangish_component = sum(sum(out > 0));
percentage_orange = num_of_pixels_with_orangish_component/total_num_of_pixels * 100;

printf("percentage of orange component in all pixels:%d\n", percentage_orange);
Duck Dodgers
  • 3,409
  • 8
  • 29
  • 43