MATLAB Answers


Colour segmentation on resistor color band and calculate the value

Asked by Ng
on 11 Dec 2012
Latest activity Commented on by reginald kwenda on 24 Mar 2015

I'm a beginner to Matlab. I need to do a color segmentation on a resistor color band, identify the color and do a calculation based on the color. Any guideline for this?


Is my bad for not giving enough information. I understand that it is impossible to find an answer that fulfill my need exactly. What I need just a simple guideline as I never learn about this program at all. What I needed now is how to identify all the 10 colors of the resistor band and made a calculation on that. Basically I've tried some code given by ImageAdjust and successfully identify 3 color only which is red,green and blue. The other colors really making me headache.

You can approach this problem from both ends: having a list of known resistor values could help you assign a weight to the color-combinations that you get. Because resistors come in fixed and known combinations you can use this to choose the most likely value out of several similar matches, or confirm/reject the value that you have detected.

You might find my FEX submission useful to help do this:

The main function rounds the input values to the standard resistor values: you can use it to check your detected color combination are the same as a known standard value:

>> round60063(514, 'E24')
ans = 510

Its second output returns the full sequence of values that cover the input values, for example:

>> [~,pns] = round60063([1,1e6],'E12');
pns = [1;1.2;1.5;1.8;2.2;2.7;3.3;...470000;560000;680000;820000;1000000]

returns in pns all values in the E12 range, between one and one million.

i am currently working on the same project , i have looked at your code but i am finding it hard to understand

6 Answers

Answer by Image Analyst
on 11 Dec 2012
 Accepted answer

I have color segmentation tutorials here if you're interested: Basically you need to find the regions of interest first - the bands. You might use a variety of techniques depending on how repeatable the lighting and positioning are from image to image. Then you can look at the mean RGB value of each band and see which "master" color it's closest to. If the band is closer red color than any other, then assume it's red.


oo...I got it d...haha...thanks alot >.<

Good. Sounds like I helped you solve it.

Thanks alot for your help. >.<

Answer by vincent
on 11 Mar 2013

Try not to be intimidated by the colours, I think it should be easy to get round this. Certainly I wouldn't let it put you off electronics, too much fun to be had :-)

You would ideally have them sorted into separate marked drawers anyway. For example these storage cabinets are what we use. It has 44 separate drawers that can be divided themselves into three parts with dividers, and a slot at the front for a label card. There are many types/sizes around so find something that suits your needs.

That gets you "pretty certain" that the resistor you take out of the drawer will be the right one. To make sure though, I would maybe buy a cheap multimeter (or repurpose one) and set it up as a value tester. If you make a little frame to set the probes just the right distance apart, you can quickly place the resistor between them and double check it's value.

This is more for loose resistors, but another option is to keep them in their packets in the drawers until needed, then take out as necessary. Though you can get errors in the packaging/component it's very very rare, and if you test one you can be even more sure all the rest will be the same anyway.

This should ensure you have very little chance of making a mistake (probably about as much as anyone else, many don't go by the colour bands anyway)

Most other components nowadays have values/codes marked on them, and if you are working with SMD (most) resistors do too - it's the unmarked capacitors that are the pain (for everyone) there :-)


Answer by reginald kwenda on 19 Feb 2015
Edited by Image Analyst
on 19 Feb 2015

Hello guys, I am working on the same problem. I have managed to detect Red, Green, and Blue following an example that was given by Image Analyst (

I have detected these colors off a resistor, but I don't know how to detect other colors such as yellow or brown.

  1 Comment

Each color has a hue. Convert your image to hsv space and find the hue and see which of the standard colors it comes closest to. Post your code and image in a new question if you need more help.

Answer by reginald kwenda on 26 Feb 2015
Edited by Image Analyst
on 4 Mar 2015

i have got the code to detect most of the colours except black and white. From there how can i use if statement to give each colour a number and add them up to give resistance value.

Above is the image I am using.


What I would do is to first find the resistor and then get a mask that is a stripe through the color bands. Then convert to hsv and find the color of the brown. And then convert to lab color space and find the delta E (color difference) of all pixels to that standard brown color. Regions with high delta E will be the color stripes. Then segment each of those and get the mean LAB color and compare that to known LAB colors of standard colors like red, green, blue, gold, etc. I have color segmentation programs in my file exchange.

For black and white, you can convert to gray scale and look for pixels with values less than 30 (or whatever) or more than 200 (or whatever). Make sure you filter by size so you don't get the legs or background or reflections.

Answer by reginald kwenda on 21 Mar 2015
Edited by reginald kwenda on 22 Mar 2015

@image analyst i was replying to you


i am getting errors with the if statements

redMask is an image, so why is it the expression of an "if" statement - that doesn't make sense.

Also, you got the wrong values for green and blue - you swapped the values. Green should be 5 and blue is 6.

Answer by reginald kwenda on 24 Mar 2015

i was trying to apply a condition that i can use with variables to assign numbers of the colour


Start a new discussion with you as the author, so we don't keep bugging Ng with emails. Then attach your code (what you have so far), and your image.

thank you for your effort , i have now created my own question and uploaded the picture and code i have so far

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!