WHAT'S THE ALGORITH APPROACH FOR THE BELOW CODE?

1 view (last 30 days)
clc;
clear all;
Photo='Resistor (1M).png'; %Resistor image path
RGB=imread(Photo);
matrixSize=size(RGB);
Y=matrixSize(1,1);
X=matrixSize(1,2);
x1 = Y;
Y=Y/2;
Y=round(Y);
for i=1:x1
for t=1:X
if RGB(i,t)<255
Mask(i,t)=0;
else
Mask(i,t)=255;
end
end
end
figure, imshow(Mask)
for i=1:X
if RGB(Y,i)==0 && RGB(Y,i,2)==0 && RGB(Y,i,3)==0
Temp(i,1)='A';
else
Temp(i,1)=0;
end
end
for i=1:X
if RGB(Y,i)>=185 && RGB(Y,i,2)>=122 && RGB(Y,i,3)<=87
Temp(i,1)='B';
end
end
for i=1:X
if RGB(Y,i)>=200 && RGB(Y,i,2)<=50 && RGB(Y,i,3)<=50
Temp(i,1)='R';
end
end
for i=1:X
if RGB(Y,i)>=240 && RGB(Y,i,2)>=230 && RGB(Y,i,3)<=10
Temp(i,1)='Y';
end
end
for i=1:X
if RGB(Y,i)<=140 && RGB(Y,i,2)<=140 && RGB(Y,i,3)>=200
Temp(i,1)='D';
end
end
for i=1:X
if RGB(Y,i)==163 && RGB(Y,i,2)==73 &&RGB(Y,i,3)==164
Temp(i,1)='V';
end
end
for i=1:X
if RGB(Y,i)==0 && RGB(Y,i,2)==255 &&RGB(Y,i,3)==0
Temp(i,1)='G';
end
end
j=1;
i1=0;
for i=1:X
if Temp(i,1)~=0
colorArray(1,j)=Temp(i,1);
j=j+1;
i1=i1+1;
end
end
i1=floor(i1/3);
i2=floor(i1*2);
i3=floor(i2+i1);
j=1;
for i=1:X
if Temp(i,1)~=0
colorArray(1,j)=Temp(i,1);
j=j+1;
end
end
colorArray(1,1)= colorArray(1,floor(j/6));
colorArray(1,2)= colorArray(1,floor(j/3)+1);
colorArray(1,3)= colorArray(1,floor(j-2));
i=1;
for i=1:3
if colorArray(1,i)==65 %black
colorArray(1,i)=0;
end
if colorArray(1,i)==66 %brown
colorArray(1,i)=1;
end
if colorArray(1,i)==82 %red
colorArray(1,i)=2;
end
if colorArray(1,i)==79 %orange
colorArray(1,i)=3;
end
if colorArray(1,i)==89 %yellow
colorArray(1,i)=4;
end
if colorArray(1,i)==71 %green
colorArray(1,i)=5;
end
if colorArray(1,i)==68 %blue
colorArray(1,i)=6;
end
if colorArray(1,i)==86 %violet
colorArray(1,i)=7;
end
if colorArray(1,i)==72 %grey
colorArray(1,i)=8;
end
if colorArray(1,i)==87 %white
colorArray(1,i)=9;
end
end
hundreds=power(10,colorArray(1,3));
tens=colorArray(1,1)*10;
ones=colorArray(1,2)*1;
resistorValue=(tens+ones)*hundreds;
if resistorValue>=1000 && resistorValue<1000000
resistorValue=resistorValue/1000;
fprintf('Resistor Value: %dk ohm',resistorValue);
elseif resistorValue>=1000000
resistorValue=resistorValue/1000000;
fprintf('Resistor Value: %dM ohm',resistorValue);
else
resistorValue=(tens+ones)*hundreds;
fprintf('Resistor Value: %d ohm',resistorValue);
end
figure, imshow(Photo);
  2 Comments
Adam
Adam on 10 Jul 2018
Edited: Adam on 10 Jul 2018
What exactly are you asking? You just pasted in a load of (very ugly) code with no comment or question to go with it apart from the vague one in the title. It's useful to know where code comes from if you are expecting people to try to make sense of it and explain it. Arbitrary 3rd party code may be total junk that has no sense to it so it is a waste of time trying to make sense of it without having some kind of background as to where it comes from and what its supposed purpose is.
Guillaume
Guillaume on 10 Jul 2018
Arbitrary 3rd party code may be total junk
Exhibit A in the question!
The code scans through the image no less than 8 times. One of these, in order to create a mask that is never used. There's more nonsense later on where things are repeated for no reason and calculations are performed and never used. And in the end the code takes into account the value of only 3 pixels.
Then, we have the nonsense that is X is the number of columns, but x1 (which used to be Y) the number of rows (and Y is now half the number of row). How can anybody keep track of what each variable represent is beyond me.
Not one of the loop is needed. The code is indeed complete junk and should be thrown away.

Sign in to comment.

Accepted Answer

Jan
Jan on 10 Jul 2018
Edited: Jan on 10 Jul 2018
The code classifies the RGB colors of pixels, most likely to identify the color codes on a resistor.
The code is poorly comments and therefore it cannot work reliably - at least its purpose can be guessed only. I would not use such code for serious work.
In addition is is poorly written. Example:
for i=1:x1
for t=1:X
if RGB(i,t)<255
Mask(i,t)=0;
else
Mask(i,t)=255;
end
end
end
can be improved:
Mask = (RGB(1:x1, 1:X) == 255) * 255
  1 Comment
Guillaume
Guillaume on 10 Jul 2018
Edited: Guillaume on 10 Jul 2018
x1 is the height of the image, X is the width (sic!), so even simpler:
Mask = 255 * (RGB(:, :, 1) == 255);
However, since Mask is never used the whole thing is pointless.
The code classifies the RGB colors of pixels, most likely to identify the color codes on a resistor
It will only work on an ideal image of a resistor where black is exactly pure black (RGB [0,0,0]) green is exactly pure green (RGB [0, 255, 0]) and violet is exactly RGB [163, 73, 164]. I'd like to see a photo of a resistor where this is indeed the case.

Sign in to comment.

More Answers (2)

vv_art
vv_art on 10 Jul 2018
here is the image of the resistor hich had no tolerance band.This code is only for this kind of images. I am new to this.sorry for posting unclearly. Thanks for your quick response.
  3 Comments
Guillaume
Guillaume on 10 Jul 2018
It's unclear what you need help with here

Sign in to comment.


vv_art
vv_art on 26 Jul 2018
Please Help me how to approach for finding the colour bands in the resistor image or suggest any alternatative mathod for colour detection of bands in the image

Community Treasure Hunt

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

Start Hunting!