WHAT'S THE ALGORITH APPROACH FOR THE BELOW CODE?
Show older comments
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
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
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.
Accepted Answer
More Answers (2)
vv_art
on 10 Jul 2018
0 votes
3 Comments
Image Analyst
on 10 Jul 2018
The code may work for that pure computer graphics image but won't work in general. I agree with the others that the code was written by a complete amateur and you should run fast and far from it. There are other resistor postings here which may have better code (not sure) so you might search for them.
vv_art
on 10 Jul 2018
Guillaume
on 10 Jul 2018
It's unclear what you need help with here
vv_art
on 26 Jul 2018
0 votes
Categories
Find more on Deep Learning Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!