# Sorting based on Colors

Hi guys,

I am trying to sort based on colors of parts (black/blue). I found the count color info and am able to get only limited information from it.

In my process I am segmenting into regions with “Calc Color Image for Highest Layer” then splitting each part into a cluster. The challenge is the “image” that is created there appears to have a value of 0 on every masked pixel. Since my parts take up a small portion of the unmasked image, the values in this function are not very helpful.

I would really like to have access to both 1 & 2 below. 3 would also be helpful if I can’t get 1 and 2 since the Image output in the “Calc Results by Python” has no dimensions to the data:

1. Perform a pixel count on a channel
2. Apply a min / max threshold for the above / all operations. (So I can factor out all 0 and 255 values).
3. Ability to export the raw image data (R,G,B, Pixel) to do python processing

I’m open to other paths to solve/sort this by color. Hoping to get some insight.

My system is a Mech-Mind IPC + LSR S on the EOAT of a Denso Robot VMB-2518. Version 1.8.0 of all the software.

Hi Peter,

You can use the “Count Color Info” Step in Mech-Vision. This Step calculates the pixel values in the specified channel. Please refer to the Count Color Info documentation for detailed instructions.

Hi Peter,

two suggestions:

1. Have a look at the step Scale Image in 2D ROI. In Color ROI settings set the ROI (e.g. ROIByParam and set With and Height to the Maximum size, or set this via file). And set Method to Update Color ROI to Bound Rect. The image is reduced to the area of the object (i.e. its bounding box). Afterwards, use Count Color Info.
2. As far as I understand, you want to calculate some statistics on the color of the image but only on the part of the image where your object is (exluding the background where the pixels have value zero). Unfortunately, this only works with a custom Python script.
Use the step Calc Results By Python to achieve this:

Enable Reload File such that when you make changes to the python script they are immediately taken over in Mech-Vision and not only when you relaod the Solution.
In your Vision program save a python script (e.g. named “calc_color_statisticy.py”)
and use this python function in it to calculate the statistics to your liking.
``````import numpy as np

def main(image):
mask = np.logical_and(0<image[...,0], image[...,0]<255)
foreground = image[mask]
B_mean = np.mean(foreground[:,0])
G_mean = np.mean(foreground[:,1])
R_mean = np.mean(foreground[:,2])
return [R_mean], [G_mean], [B_mean]
``````

calc_color_statistics.py (289 Bytes)

This will calculate the mean of each channel and return it as NumberList. Of course you can also adjust the Python function to your liking. S
Have a look here for more information:

Thank you Florian,

I will need to investigate this as it looks helpful. I also appreciate you showing how to use the image output in python. I thought it was sending a greyscale value.

I was up late last night and figured out I could eliminate the non-masked regions and convert to 0-255 values of only the masked region’s channel with the below formula:

“Mean Pixel Value Channel of Mask”= “Mean Pixel Value Channel” * (Qty of Non-masked pixels/Qty of Masked Pixels + 1)

PS. It might be helpful to include the specific data type being exported to python in it’s documentation:

Peter, thanks for your feedback on the documentation. We have received it and will make the necessary adjustments accordingly.