Please explain this QR code program to me.
Show older comments
I wanna someone explain to me what every functions do ,special functions QRkey and im2freqs ?total code will extract Qr code from image
function [ hit ] = QRkey( img,L,RP ) hit=[]; [row ~] = size(img); for i = 1:row freq = im2freqs(img(i,:)); [num ~] = size(freq); for j = 3:num-3 Error = 0.5; if abs((freq(j,2)/3)-freq(j-2,2)) > ceil((freq(j,2)/3)*Error)+1 ... abs((freq(j,2)/3)-freq(j-1,2)) > ceil((freq(j,2)/3)*Error)+1 ... abs((freq(j,2)/3)-freq(j+1,2)) > ceil((freq(j,2)/3)*Error)+1 ... abs((freq(j,2)/3)-freq(j+2,2)) > ceil((freq(j,2)/3)*Error)+1 ... freq(j,3)==0 L(i,freq(j,1))==0 L(i,freq(j+2,1))==0 L(i,freq(j-2,1))==0 continue; end %x = freq(j,1) , y = i Box = RP( L(i,freq(j,1)) ).BoundingBox; Cdif = uint32(sum(abs(RP(L(i,freq(j+2,1))).Centroid - RP(L(i,freq(j,1))).Centroid))); Ar = RP( L(i,freq(j,1)) ).Area;
if Cdif <= 5 && ...
L(i,freq(j-2,1))==L(i,freq(j+2,1)) && L(i,freq(j-2,1))~=L(i,freq(j,1))
hit = [hit; L(i,freq(j,1))];
j = j+2;
end
end
end
end
function [ freq ] = im2freqs( img)
col = numel(img);
freq = [];
co = [1 1 img(1)]; % [center counter color]
for j = 2:col
if co(3) ~= img(j)
co(1) = uint32(co(1))+ uint32(co(2))/2;
freq = [freq ; co];
co = [j 1 img(j)];
else
co(2) = co(2) + 1;
end
end
co(1) = uint32(co(1))+ uint32(co(2))/2;
freq = [freq;co];
end
function QR = CropQR (img, points) %u = [points(2,:)-points(4,:) 0]; %v = [1 0 0]; %c = cross(u,v); %angle = sign(c(3))*180/pi*atan2(norm(c),dot(u,v)); %angle = -(mod(angle +360,360)-135); angle = atand((points(2,2)-points(4,2))/(points(2,1)-points(4,1))); angle = angle - 45; if points(4,1) < points(2,1) angle = angle + 180; end
[h ,w ,~] = size(img);
QR = imrotate(img, angle);
[rh ,rw ,~] = size(QR);
T=[w/2 h/2];
RT=[rw/2 rh/2];
R=[cosd(angle) -sind(angle); sind(angle) cosd(angle)];
T = [T;T;T;T];
RT = [RT;RT;RT;RT];
rotpoints=(points-T)*R+RT ;
rotpoints=uint32(rotpoints);
xymax = max(rotpoints);
xymin = min(rotpoints);
QR = QR(xymin(2):xymax(2), xymin(1):xymax(1), :);
end
function [ ] = QRextraction( Rimg ) Rimg = imread('6.1.bmp'); img = rgb2gray(Rimg);
%sharppining the image
%enhancment image % and convert to binary image (black and white)
img = imsharpen(img,'Radius',10,'Amount',1);
img = ~im2bw(img,0.5);
[L num] = bwlabel(img)
%now we need properties for each 8 component
RP = regionprops(L,'all');
hit = QRkey(img,L,RP);
hit = [unique(hit) histc(hit,unique(hit))]
figure,imshow(L );
flag = zeros((numel(hit)/2));
for i = 1:(numel(hit)/2)-1
if flag(i)==1
continue;
end
ci = RP(hit(i,1)).Centroid;
j = i+1;
if flag(j)==1
continue;
end
cj = RP(hit(j,1)).Centroid;
for k = 1:(numel(hit)/2)
if i == k || j == k || flag(k)==1
continue;
end
ck = RP(hit(k,1)).Centroid;
%start
a = pdist([ci ; cj],'euclidean');
b = pdist([ci ; ck],'euclidean');
c = pdist([cj ; ck],'euclidean');
diff = cj - ci;
if c<a || c<b
continue;
end
d = ck+diff;
zz = abs((a*a + b*b)-(c*c))/(c*c)
center = ((cj+ci+ck+d)/4);
if zz < 0.1
x = [cj(1);ci(1);ck(1);d(1)];
y = [cj(2);ci(2);ck(2);d(2)];
x = x+(x-center(1))*0.5;
y = y+(y-center(2))*0.5;
flag(i)=1;flag(j)=1;flag(k)=1;
final_image=CropQR( Rimg , [x y]);
figure,imshow(final_image);
break;
end
end
end
7 Comments
Walter Roberson
on 3 Jan 2022
This appears to be code from https://github.com/FCIS2020-Projects/Automatic-Segment-QR-Code . Have you consulted the author documentation ?
Image Analyst
on 3 Jan 2022
We can't understand it either (or don't want to take the extensive time to do so).
I suggest you contact the author and ask them why they didn't put in any comments like all good programmers do. This code would get a grade of D or F from me. virtually no comments, cryptic variable names, multiple statements on one line, bad indenting, etc. It's a mess.
S. M
on 3 Jan 2022
Adam Danz
on 3 Jan 2022
You shouldn't share other people's work without at least giving them credit or providing a link to the source.
S. M
on 3 Jan 2022
Walter Roberson
on 3 Jan 2022
The author of the code appears to be Osama Fawzy at Faculty of Computer and Information Science Ain Shams University
S. M
on 3 Jan 2022
Answers (1)
yanqi liu
on 4 Jan 2022
Edited: Walter Roberson
on 4 Jan 2022
yes,sir,i think the key is to judge the square corner,such as

now,we can find the rule
a = pdist([ci; cj],'euclidean');
b = pdist([ci; ck],'euclidean');
c = pdist([cj; ck],'euclidean');
zz = abs((a*a + b*b)-(c*c))/(c*c);
when zz < 0.1 ,the area is QR area,such as

Categories
Find more on MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!