Colour segmentation on resistor color band and calculate the value
87 次查看(过去 30 天)
显示 更早的评论
Ng
2012-12-11
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 个评论
Jan
2012-12-28
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
2012-12-29
编辑:Ng
2012-12-29
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
2015-3-23
编辑:Stephen23
2023-2-12
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
2015-3-24
i am currently working on the same project , i have looked at your code but i am finding it hard to understand
采纳的回答
Image Analyst
2012-12-11
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 个评论
Ng
2012-12-13
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
2012-12-13
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
2012-12-13
编辑:Ng
2012-12-13
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
2012-12-14
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
2012-12-14
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
2012-12-14
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
2012-12-14
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
2012-12-14
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
2012-12-14
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
2012-12-19
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
2012-12-28
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
2012-12-29
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.
更多回答(1 个)
reginald kwenda
2015-2-19
编辑:Image Analyst
2015-2-19
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 个评论
Image Analyst
2015-2-19
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
2015-2-26
移动:DGM
2023-2-12
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
2015-3-4
移动:DGM
2023-2-12
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
2015-3-21
移动:DGM
2023-2-12
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
2015-3-23
移动:DGM
2023-2-12
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
2015-3-23
移动:DGM
2023-2-12
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.
Image Analyst
2015-3-23
移动:DGM
2023-2-12
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
2015-3-23
移动:DGM
2023-2-12
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
2015-3-23
移动:DGM
2023-2-12
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
2015-3-24
移动:DGM
2023-2-12
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
2015-3-24
移动:DGM
2023-2-12
@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
2015-3-24
移动:DGM
2023-2-12
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)