Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5

Box Color Sorting


I am developing a program for my master thesis and just recently started using RoboDK. I am a newbie in Python, actually programming in general. 

I develop an environment with 8 boxes using RoboDK, two of which are different colors. I want to be able to use the robotic arm to detect the color of the boxes using RGB values or whatever is simpler and make it able to pick the one that is out-of-spec and place it out of the line. For example, in a single straight line I have 6 green boxes, 1 red box and 1 blue box. I want to robotic arm with the camera to go over the boxes in a straight path and if it sees the red or blue box, to place it out of the line since it is not green. I also would like to do it with predominantly red boxes and in another case, predominantly blue boxes. 

I have attached the RoboDK environment with the targets and everything but have found it difficult to program into the robot this "color sorting" capacity.

Thanks in advance, 

.rdk   ColourSorting Project.rdk (Size: 524.77 KB / Downloads: 342)
Hi Manisan,

We added some OpenCV examples in our Python documentation that you might find useful, see:

OpenCV has a nice tutorial on colour segmentation, that you can adapt for your needs:
Please read the Forum Guidelines before posting!
Find useful information about RoboDK by visiting our Online Documentation.
Hello Sam!

Thank you for your response. I have been able to create a program to use with the colour sorting aspect of the project and have finishhed the RoboDK environment for the experiment. The colour code is written in python and I am struggling to add this code or integrate it with the RoboDK environment. 

My initial approach was to generate a code from the RoboDK software, edit it by adding my color processing code and then run it in the simulator. For some reason the code will not run. Ive attached en simulation environment. You can see that PP#, PL# etc are different trajectories. I also attahced the colour procesing code. I would like to integrate the two and do some sort of a "If block is X color, do PP1 or do PA12, etc"

In summary, Im just trying to find a way to integrate the colour processing code of python with the RoboDK  environment.

Attached Files
.rdk   ColourSorting Project.rdk (Size: 526.83 KB / Downloads: 268)
.py (Size: 1.43 KB / Downloads: 255)
Nice job!

You can save parameters to the station (or an item) and reuse them in other program calls, see:

For instance, you could run your ColorSorting script once at the beginning of your sequence to request the colour to the user. You can then have a second script that process the image using the recorded colour.

reject_color = 'RED'
RDK.setParam('MyColor', reject_color)

PP1 = RDK.Item('PPI1')
PP12 = RDK.Item('PPI12')

detected_color = detectColor(img)
reject_color = RDK.getParam('MyColor')
if detected_color !- reject_color:

You should take a look at our YouTube videos and our Python documentation!
Please read the Forum Guidelines before posting!
Find useful information about RoboDK by visiting our Online Documentation.
Hello Sam, 

Took a little different approach but I managed to do it. Your reply really took me on the right path and point me to the right resources. I have NO programming experience whatsoever and I have managed to do it. I dont know if you can tell but I am excited and RoboDK has just saved my Master degree haha. 

Here is the code and environment so people and play around and definitely improve it. 

Armani Caban

Attached Files
.py (Size: 4.89 KB / Downloads: 274)
.rdk   ColourSorting Project.rdk (Size: 527.84 KB / Downloads: 339)
I'm glad you were able to achieve your goals with no programming experience using RoboDK. This is what we aim for!

I see you are using one pixel at the image centre to get the colour. In reality, the image will be affected by ambient lights, shadows, reflection, etc

You can use colour segmentation to retrieve the dominating colour. Here's a sample code:

lower_b = [160, 100, 20]  # red lower bound in HSV
upper_b = [179, 255, 255]  # red upper bound in HSV
red_mask = cv.inRange(self.img, np.array(lower_b), np.array(upper_b))
red_pixels = red_mask[red_mask > 0].size
Please read the Forum Guidelines before posting!
Find useful information about RoboDK by visiting our Online Documentation.

Users browsing this thread:
1 Guest(s)