You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Colour segmentation on resistor color band and calculate the value
15 views (last 30 days)
Show older comments
Ng
on 11 Dec 2012
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?
6 Comments
Jan
on 28 Dec 2012
This is a very general question. It is unlikely that you get a specific answer, which matchs your needs exactly. This forum is powerful, when you post, what you have tried so far and ask a specific Matlab related question. But you will most likely not get a running program after a vague description of the problem. Please note, that all information you have offered about the inputs is "a resistor color band" - we do neither know, if this is a real-time video capture, a graphics file, an imported image in indexed colors, an RGB value of a specific pixel or the real-world resistor in your hand only. The less the readers have to guess, the easier and more likely is a useful answer.
Ng
on 29 Dec 2012
Edited: Ng
on 29 Dec 2012
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.
Stephen23
on 23 Mar 2015
Edited: Stephen23
on 12 Feb 2023
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 3rd output returns the full preferred number sequence of values that includes all 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 all preferred values in the E12 range, between one and one million.
reginald kwenda
on 24 Mar 2015
i am currently working on the same project , i have looked at your code but i am finding it hard to understand
Accepted Answer
Image Analyst
on 11 Dec 2012
I have color segmentation tutorials here if you're interested: http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862. 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.
17 Comments
Ng
on 13 Dec 2012
hi sir, I try to used SimpleColorDetection code on my resistor. However when I'm doing segmentation on red colour band, part of the resistor body(which is light brown in colour) was segmented as well. I try to adjust the threshold value but still no improvement on that. Do you have any suggestion on this?
Image Analyst
on 13 Dec 2012
Are you calibrating your image? Do you have a mini-ColorChecker shart in your image so that you can compensate for lighting changes, such as in the exposure and color temperature? Perhaps red changes from image to image so that one image's red looks like another images brown. If that's not the case, then just narrow your threshold so that you're getting just the red, for example, brown is dark red so raise your red threshold to get just bright red.
Ng
on 13 Dec 2012
Edited: Ng
on 13 Dec 2012
Thanks for you quick reply. I change the colourspace into YCbCr, and do canny edging on it. After that apply colour segmentation by changing the threshold value as suggested and successfully retrieved red, green and blue colour.
However, while I'm doing the colourspace changing, I was unable to detected other colour.
Im using
B=imread(A,'JPEG');
[x y z]=size(B);
if z==1
rslt=edge(B,'canny');
elseif z==3
C=rgb2ycbcr(B);
dx1=edge(C(:,:,1),'canny');
dx1=(dx1*255);
D(:,:,1)=dx1;
D(:,:,2)=C(:,:,2);
D(:,:,3)=C(:,:,3);
rgbImage=ycbcr2rgb(uint8(D)); end
Any advice on this?
Is it possible that I can play around with the masking to get other colour for example, if red =1 green =1 blue = 0 then it is yellow
PS: Does the mini-ColourChart refer to the RGB value of different colour?
Ng
on 14 Dec 2012
The original Image
The Image after canny edging
The three colour band
I try to play around with the code to get the gold band, but i fail.
Image Analyst
on 14 Dec 2012
Why are you doing edge detection? Why not just look at ROI's at certain, fixed, pretermined locations? Does your resistor move around in the image (because of poor image capture method), or is it in the same location in all photos (indicating a good image capture situation)?
Ng
on 14 Dec 2012
The resistor will be move around in the image. So if I predetermined the location, wouldn't it out of place sometime? I try to let it automatically identify the colors in randoms coordinate.
Image Analyst
on 14 Dec 2012
If you can locate some of the bands, then just assume the other bands are at the correct spacing, for example, all the bands are 200 pixels apart. Then just measure a small box at the center of each band and calculate the Euclidean distances to the nominal/standard/reference colors for each resistor band's color. Each color will be closest to one of the reference colors and far away from the others. The reference color that it is closest to the actual color is obviously the color that should be assigned to that band.
Jürgen
on 14 Dec 2012
just an idea : could it be helpfull if you did some calibration of the colors before, I mean: use the color values of some test images to compare with instead of the reference values. This could maybe help to take into account the influence of the set-up and the light.
Image Analyst
on 14 Dec 2012
That's an excellent idea and would definitely help. In fact it's essential for nearly all of my projects. If you want something robust for accurate color measurement, and it's not just some "quick and dirty" trial or well controlled student project, then you must do that. The best systems will have a color standard (like the mini-ColorChecker chart) embedded into each image.
Ng
on 19 Dec 2012
I'm a bit confuse here. Is it ok if I segmented the 4 bars with roicolor and then detect the colour by comparing to the rgbvalues? For example,
I assume blue to be R = 0 G = 0 B = 255
Then I threshold it and let it count how many pixel closer to the value. When the count > 1000 ( for example ) blue = 1( true)
Image Analyst
on 28 Dec 2012
See code I gave here http://www.mathworks.com/matlabcentral/answers/57356#comment_119461 for determining the color of Lego blocks. Your situation will be nearly identical except that you will have more reference colors. Simply look at each band and determine which color it's closest to. It's all there in the code - really simple for you to adapt - just simply add more colors.
Ng
on 29 Dec 2012
I do use this code at the first place and identified red,green and blue. I try to apply it to identify yellow color by adding up red and green channel.
However, I was confused
yellowBand=imadd(redBand,greenBand);
figure,imshow(yellowBand);
redThresholdLow = 128;
redThresholdHigh = 255;
greenThresholdLow = 128;
greenThresholdHigh = 255;
blueThresholdLow = 0;
blueThresholdHigh = graythresh(blueBand);
redMask = (redBand >= redThresholdLow) & (redBand <= redThresholdHigh);
greenMask = (greenBand >= greenThresholdLow) & (greenBand <= greenThresholdHigh);
blueMask = (blueBand >= blueThresholdLow) & (blueBand <= blueThresholdHigh);
yellowObjectsMask = uint8(redMask & greenMask & blueMask);
How do I included yellow band inside the mask? Or I'm doing mistakes here.
Image Analyst
on 30 Dec 2012
Edited: Image Analyst
on 11 Mar 2013
Good. Sounds like I helped you solve it.
More Answers (1)
reginald kwenda
on 19 Feb 2015
Edited: 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 ( http://www.mathworks.com/matlabcentral/answers/57356#comment_119461)
I have detected these colors off a resistor, but I don't know how to detect other colors such as yellow or brown.
16 Comments
Image Analyst
on 19 Feb 2015
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.
reginald kwenda
on 26 Feb 2015
Moved: DGM
on 12 Feb 2023
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.
Image Analyst
on 4 Mar 2015
Moved: DGM
on 12 Feb 2023
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. http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862
Image Analyst
on 21 Mar 2015
Moved: DGM
on 12 Feb 2023
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.
Image Analyst
on 23 Mar 2015
Moved: DGM
on 12 Feb 2023
You should use the Comment box instead of creating a brand new original "Answer" to the original poster's original question at the very top. I don't have any video tutorials, just what is in my File Exchange: http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862
reginald kwenda
on 23 Mar 2015
Moved: DGM
on 12 Feb 2023
okay i understand ,i am new to digital image processing . I took on a project on writing a programme in matlab that should extract resistor color bands from an image and do calculations to give resistance values .I saw this as a challenge to learn something new and finding a passion. Through the help that you gave to Ng (the guy that started this poster) i learned how to extract the colours off an image but now i am stuck on how i can give each colour a number and make the calculations to give resistance value.
reginald kwenda
on 23 Mar 2015
Moved: DGM
on 12 Feb 2023
I appreciate you taking your time to reply my questions
Image Analyst
on 23 Mar 2015
Moved: DGM
on 12 Feb 2023
I think the comments above about first finding the stripes, and then comparing the colors to a list of known, predefined colors is a good suggestion. To make it really easy, first make a jig to make sure that your resistors are all positioned in exactly the same position. Then just crop out known rectangles where you know the stripes will be. Look at their color and compare to the predefined list of colors and find out what predefined color is closest to your stripe color.
reginald kwenda
on 23 Mar 2015
Moved: DGM
on 12 Feb 2023
i have used the code above and managed to extract red , green and blue strip from the resistor image but using rgb space.How can i proceed in giving these three colours their resistance value which is 5600 . green represents 5 , blue represent 6 and red represents 2..to get resistance value you read the colours from left to right.in this case 5(green)6(blue)* last colour red which represets the number of zeros = 5600
Image Analyst
on 23 Mar 2015
Moved: DGM
on 12 Feb 2023
You assign the digits when you get the color as you examine the stripes. Then use the formula
resistanceValue = (firstDigit * 10 + secondDigit) * 10^numberOfZeros;
reginald kwenda
on 24 Mar 2015
Moved: DGM
on 12 Feb 2023
rgbImage = imread('resistor.jpg');
imshow(rgbImage);
impixelinfo;
Red=2
Green = 6
Blue=5
firstDigit=0
secondDigit=0
thirdDigit=0
RedSpace = rgbImage(:, :, 1);
GreenSpace = rgbImage(:,:, 2);
BlueSpace = rgbImage(:, :, 3);
if (redMask = (RedSpace > 190) & (GreenSpace < 150) & (BlueSpace < 90));
(redMask = imfill(redMask, 'holes'); % Filling in holes.
(firstDigit= Red)
end
%resistanceValue = (firstDigit * 10 + secondDigit) * 10^numberOfZeros;
if(greenMask=(RedSpace < 167) & (GreenSpace > 160) & (BlueSpace < 150));
(greenMask = imfill(greenMask, 'holes')) % Filling in holes.
(secondDigit= Green)
end
if(blueMask = (RedSpace < 120) & (GreenSpace > 60) & (BlueSpace < 130) & (BlueSpace> 100));
(blueMask = imfill(blueMask, 'holes')); % Filling in holes.
(thirdDigit= Blue)
end
resistanceValue = (firstDigit * 10 + secondDigit) * 10^thirdDigit;
reginald kwenda
on 24 Mar 2015
Moved: DGM
on 12 Feb 2023
@image analyst..that is what i came up with but i keep getting an error with the if statements(i am using the same image that Ng used)
Image Analyst
on 24 Mar 2015
Moved: DGM
on 12 Feb 2023
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.
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)