I plant trees in the north of BC for a living and have next to none sp experience, please be gentle :^]

TLDR: The problem is essentially this: tracking and calculating the "height" of three differently-colored regions whose proportions change relative to each other over a number of images using k-means segmentation and color-histogram vector.

(see fig. below)

It's a little chemistry experiment which kicks off with a transparent beaker full of light-blue-aquamarine foam, then a transparent oil is added and over the next few minutes the light blue foams condenses into a distinctively-darker blue homogenous liquid at the bottom of the beaker and there is no light-blue foam left whatsoever. The boundary between the foam and the emulsifier(oil) is.. well.. foamy at all times as i tried to indicate in the drawing, but i think a blur with an appropriate kernel should solves that pretty well. We have the full control over the setup(any lighting, any angle and number of photos, etc.)

They want me to grab the MATLAB image-processing toolbox which I, unfortunately, happened to have and given a set of oh let's say 360 snapshots of the above experiment plot the "height" of the boundary lines between the oil, the light-blue foam and the dark blue liquid at the bottom against time. Without sticking a ruler next to the vessel! Bummer!

How would a signal-processing guru like yourself have gone about this? Terms like "k-means segmentation" and "dominant vectors of a histogram" prevailed in the conversation with the engineer who has imparted this task to me, but knows less MATLAB than I do. He has also made it abundantly clear that he wants it done in MATLAB. Woulda been a simple problem in opencv, but oh well!

Could you possibly throw like a bullet-list of functions/ sequence of steps one would undertake in solving something like this? So i know what to research at least. It has to be k-means, histogram and matlab unfortunately. The difference between going this road and any other for me is like between getting laid and getting shot, folks. I count on your understanding and am indebted in advance!


enter image description here

  • $\begingroup$ In the example you provided, the task is more or less trivial, the colors are clearly separable in RGB (or any other) color space. It would help a lot if you could provide some real-world examples to get a feeling for where the challenges may lie specifically. $\endgroup$
    – Florian
    Jun 7, 2019 at 11:05

1 Answer 1


If the distinct contents/ingredients really have different colors than you just have to take a picture, always in the same location/distance, and then count the amount of pixels for each color.

You can transform your RGB image to the HSV color space to make it easy to identify specific colors.

Some pseudocode:

image_hsv = rgb2hsv(image_rgb)
greenmax = 120+10
greenmin = 120-10
num_green_pixels = sum( and( image_hsv[H]>greenmin, image_hsv[H]<greenmax ) )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.