Colour segmentation on resistor color band and calculate the value

hello guys i am trying to segment colour bands of a resistor using image processing and calculate the colours to give resistance . I am new to image processing any of your help is appreciated . i have come up with the code below but i am now stuck on how i can assign each colour band a number and do the calculations. Below the code is picture i am working with.

4 Comments

rgbImage = imread('r1.jpg');
close all
figure(1)
subplot(2,1,1)
imshow(rgbImage);
impixelinfo;
subplot(2,1,2)
HSVimage = rgb2hsv(rgbImage);
imshow(HSVimage);
figure(1)
h = HSVimage(:, :, 1);
s = HSVimage(:, :, 2);
v = HSVimage(:, :, 3);
figure(2)
imshow(h);
rwe= 3;
[ImSizeY,ImSizeX,null] = size(h);
Segment1 = zeros(ImSizeY, ImSizeX);
output = zeros(size(rgbImage));
SearchVal = [(0.3492 - 0.010) , (0.3492 +0.010) ; (0.6218 - 0.010) , (0.6218 +0.010) ; (0.9887 - 0.010) , (0.9887 +0.010);
(0.03509-0.010),(0.03509+0.010);(0.7 - 0.010),(0.7+0.010);(0.9275-0.010 ),(0.9275+0.010);(0.9722-0.010),(0.9722+0.010);(0.9167-0.001),(0.9167);]
for LookupIndex = 1:1:length(SearchVal)
for indexY=1:1:ImSizeY
for indexX=1:1:ImSizeX
if (h(indexY,indexX) > SearchVal(LookupIndex,1)) && (h(indexY,indexX) < SearchVal(LookupIndex,2))
Segment1(indexY,indexX) = 1;
output(indexY,indexX,:) = rgbImage(indexY,indexX,:);
else
Segment1(indexY,indexX) = 0;
output(indexY,indexX) = 0;
end
%output(indexY,indexX) = h(indexY,indexX)*Segment1(indexY,indexX);
end
end
end
figure(3)
imshow(Segment1)
figure(4)
imshow(output)
figure
imshow(rgbImage);
the first resistor is a 5600 and second resistor is 300k

Sign in to comment.

Answers (3)

Neither the lighting nor magnification is good enough to determine color robustly. Sorry, but the image is crummy. Zoom in and provide more light. Also, put the resistor in a jig to make sure it's positioned in the same place in each image.

3 Comments

@image analyst ..i will have new pictures by tomorrow , i dont know if you have noticed that i changed the code from the one that was using rgb space to using hsv..i was finding it difficult to identify colours bands of image taken by a camera . wat do you recommend

Sign in to comment.

What I would do is to convert that image to LAB color space with rgb2lab(). Then find out the lab values of each color by looking at the images in the variable inspector. Then calculate a delta E (color difference) image for each know, predefined, fixed color. For example an image of the delta E between that lab image and the red color, then an image of the delta E between that lab image and the green color, and so on. Classify the pixel according to which delta E is least, meaning the color is closest to that color. I think you can do this if you insert the delta E images into a 3D matrix and then use min() across the third dimension and take the second output argument of min() as the classified image.
Delta E is just the sqrt of the delta L square plus... etc. For example to compute delta E between the image and the LAB of the red stripe:
deltaE = sqrt((L-lRed).^2 + (A - aRed).^2 + (B - bRed).^2);
Delta E is the industry standard metric for measuring color difference. Do the same for the green, blue, yellow, white, silver, gold, etc. colors.

14 Comments

You can also use impixelinfo() to determine the lab values interactively by mousing around over the image.
rgbImage = imread('H:R6.jpg');
imshow(rgbImage);
cform = makecform('srgb2lab');
lab_he = applycform(rgbImage,cform);
imshow(lab_he)
h=impixelinfo
i have managed to convert from rgb to lab but i am struggling to do delta E (color difference) image for each know, predefined, fixed color
The RGB in the status line will actually be the LAB value. What values do you get for the different color stripes?
i got green as [151 74 173] , blue [75 132 91] , red[117 193 175] and gold [217 132 165]..in this method how do i extract individual colours
Those must be the RGB values, not the LAB values. L does not go past 100. What are the LAB values?
what function can i use to get these values ?
If you displayed the lab image, it should be shown in the Pixel info label. Maybe I'm wrong. I'll have to check the range of lab that it gives. I thought it was always 0-100, which is industry standard but maybe they scale it to 0-255 for some reason. See if it's any different if you cast to double first:
lab_he = applycform(double(rgbImage),cform);
There is also a one-step function you can use: rgb2lab(). Try that too.
yes the pixel information gave me those values ..i used the double and its no good ..
The formula for color difference is
deltaE = sqrt((L-lRed).^2 + (A - aRed).^2 + (B - bRed).^2);
L, A, and B are your lab_he(:,:,1), lab_he(:,:,2), and lab_he(:,:,3) images.
You said the lab for red is [117 193 175] so then
lRed = 117 % The L value of the red stripe.
aRed = 193 % The A value of the red stripe.
bRed = 175 % The B value of the red stripe.
Similarly for the other stripe colors.
rgbImage = imread('R6.jpg');
subplot(2, 2, 1);
figure(1), imshow(rgbImage), title('resistor');
%Convert the image to L*a*b* color space using makecform and applycform.
cform = makecform('srgb2lab');
lab_Image = applycform(rgbImage,cform);
subplot(2, 2, 2);
imshow(lab_Image)
%pixel information
h=impixelinfo
L = lab_Image(:, :, 1);
A = lab_Image(:, :, 2);
B = lab_Image(:, :, 3);
lRed = 117 % The L value of the red stripe.
aRed = 193 % The A value of the red stripe.
bRed = 175 % The B value of the red stripe.
deltaE = sqrt((L-lRed).^2 + (A - aRed).^2 + (B - bRed).^2);
imshow(deltaE)
subplot(2, 2, 3);
i have put it like this but i cant get the image off the extracted red from the formula
rgbImage = imread('C:\Users\ab2123\Desktop\r1.jpg');
close all
figure(1)
subplot(2,1,1)
imshow(rgbImage);
impixelinfo;
subplot(2,1,2)
HSVimage = rgb2hsv(rgbImage);
imshow(HSVimage);
h = HSVimage(:, :, 1);
s = HSVimage(:, :, 2);
v = HSVimage(:, :, 3);
figure(2)
imshow(h);
figure(3)
imshow(s);
Black=10;
Brown=1;
Red = 2;
Orange=3;
Yellow=4;
Green=5;
Blue=6;
violet=7;
Gray=8;
White=9;
colour=0;
[ImSizeY,ImSizeX,null] = size(h);
Segment1 = zeros(ImSizeY, ImSizeX);
output = zeros(size(rgbImage));
HueSearchVal = [(0.3492 - 0.010) , (0.3492 +0.010) ; (0.6218 - 0.050) , (0.6218 +0.050) ; (0.9887 - 0.020) , (0.9887 +0.020)];
SaturationVal = [(0.7188 - 0.050) , (0.7188 +0.050) ; (0.8286 - 0.050) , (0.8286 +0.050) ; (0.8333 - 0.050) ,(0.8333 + 0.050)]
HueSearchValue = [Green, Blue, Red];
% (0.03509-0.010),(0.03509+0.010);(0.7 - 0.010),(0.7+0.010);(0.9275-0.010 ),(0.9275+0.010);(0.9722-0.010),(0.9722+0.010);(0.9167-0.001),(0.9167);]
%SaturationVal = [(0.7188 - 0.010) , (0.7188 +0.010) ; (0.8286 - 0.010) , (0.8286 +0.010) ; (0.8333 - 0.010) ,(0.8333 + 0.010)
%if HueSearchVal == 0
% colour = colour + Green;
%else if HueSearchVal == 0
% colour = colour + Green;
%end
for LookupIndex = 1:1:length(HueSearchVal)
for indexY=1:1:ImSizeY
for indexX=1:1:ImSizeX
if (s(indexY,indexX) > SaturationVal(LookupIndex,1)) && (s(indexY,indexX) < SaturationVal(LookupIndex,2))
if (h(indexY,indexX) > HueSearchVal(LookupIndex,1)) && (h(indexY,indexX) < HueSearchVal(LookupIndex,2))
Segment1(indexY,indexX) = HueSearchValue(LookupIndex);
output(indexY,indexX,:) = rgbImage(indexY,indexX,:);
else
Segment1(indexY,indexX) = 0;
output(indexY,indexX) = 0;
end
end
end
end
end
figure(4)
imshow(Segment1)
figure(5)
imshow(output)
figure(6)
imshow(rgbImage);
came up with this code and it can segment the colour bands with problems but how can any1 with ideas on how can proceed in doing the calculations to get the resistance value.

Sign in to comment.

1 Comment

Wow, people can get a published paper for this kind of super trivial, non-novel application? And the article didn't even say how they found the color bands and compared them to references.

Sign in to comment.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Asked:

on 24 Mar 2015

Moved:

DGM
on 12 Feb 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!