scanned target calibration (part 2)

Contents

image

clear
close all
rgb = imread('Scan_Pic0003.jpg');
%rgb = imread('target.jpg');
img = im2double(rgb2gray(rgb));
sz = size(img);
img = img(400:sz(1),:);
h = fspecial('gaussian',9,2);
scl=2;
im = imresize(imfilter(img,h),1/scl);
imshow(im);
Warning: Image is too big to fit on screen; displaying at 67% 
border = zeros(size(im));
n = 120;
n2 = floor(n/2);
sz = size(im)-n;
border(n2+(1:sz(1)),n2+(1:sz(2)))=1;
clear rgb img
h = filters2();
h0 = h(:,:,1);
h1 = h(:,:,2);
h2 = h(:,:,3);
h11 = h(:,:,4);
h22 = h(:,:,5);
h12 = h(:,:,6);

edges

a1 = imfilter(im,h1);
a2 = imfilter(im,h2);
bs = imfilter(im,h12);
bsmax = max(max(max(bs)),-min(min(bs)));

show vertices

dpi = 599.406; % value from ruler2 (with filtering)
dpmm = dpi/25.4;


c1 = a1/sum(sum(h1.^2));
c2 = a2/sum(sum(h2.^2));
c3 = bs/sum(sum(h12.^2)).*border;

xcen = -c2./c3;
ycen = -c1./c3;
rsq = xcen.^2+ycen.^2;
r = rsq<5;
g = zeros(size(im));
b = abs(bs)>0.2*bsmax;
imshow(cat(3,r,g,b));
Warning: Image is too big to fit on screen; displaying at 67% 
mask = and(r,b);
imshow(mask);
sum(sum(mask))
Warning: Image is too big to fit on screen; displaying at 67% 

ans =

    94

calculate vertices

sz = size(im);
xloc = ones(sz(1),1)*(1:sz(2));
yloc = (1:sz(1))'*ones(1,sz(2));
idx = find(mask(:)>0);
xcen=xcen(:);
xloc=xloc(:);
xpt = xloc(idx)+xcen(idx);
ycen=ycen(:);
yloc=yloc(:);
ypt = yloc(idx)+ycen(idx);
plot(xpt,ypt,'o');

sort vertices

k = 0;
xw = xpt;
yw = ypt;
clear xc yc
while ~isempty(xw)
    test = (xw-xw(1)).^2+(yw-yw(1)).^2;
    [tsort idx] = sort(test);
    xw = xw(idx);
    yw = yw(idx);
    idx = find(tsort<64);
    k = k + 1;
    xc(k) = mean(xw(idx));
    yc(k) = mean(yw(idx));
    idx = find(tsort>64);
%     k
%     idx'
    xw = xw(idx);
    yw = yw(idx);
end
plot(xc,yc,'o');

show results (on image)

imshow(im);
hold on
plot(xc,yc,'x','LineWidth',2,'MarkerSize',10);
hold off
Warning: Image is too big to fit on screen; displaying at 67% 

widths

pts(:,:,1) = reshape(xc,3,3);
pts(:,:,2) = reshape(yc,3,3);
pts = sort(pts,1);
width = scl*diff(pts(:,:,1)')'/dpmm;
ydiff = scl*diff(pts(:,:,2)')'/dpmm;
fprintf('%12s\t %12s\t %12s\t %12s\n','row','width 1','width 2','row angle');
fprintf('%12s\t %12s\t %12s\t %12s\n','index','mm','mm','degrees');
for n=1:3
    c = polyfit(pts(n,:,1),pts(n,:,2),1);
    fprintf('%12g\t %12g\t %12g\t %12g\n',floor(pts(n,1,2)),width(n,1),width(n,2),atan(c(1))*180/pi);
end
fprintf('\n\n');

fprintf('mean width %g mm, std dev %g mm\n',mean(width(:)),std(width(:)));
         row	      width 1	      width 2	    row angle
       index	           mm	           mm	      degrees
         222	      36.5275	      36.5015	    -0.357284
         580	      36.5011	      36.5059	    -0.343459
         938	      36.4948	      36.5091	     -0.27241


mean width 36.5066 mm, std dev 0.0113134 mm

heights

height = scl*diff(pts(:,:,2))/dpmm;
xdiff = scl*diff(pts(:,:,1))/dpmm;
fprintf('%12s\t %12s\t %12s\t %12s\n','col','height 1','height 2','col angle');
fprintf('%12s\t %12s\t %12s\t %12s\n','index','mm','mm','degrees');
for n=1:3
    c = polyfit(pts(:,n,2),pts(:,n,1),1);
    fprintf('%12g\t %12g\t %12g\t %12g\n',floor(pts(1,n,1)),height(1,n),height(2,n),atan(c(1))*180/pi);
end
fprintf('\n\n');


fprintf('mean height %g mm, std dev %g mm\n',mean(height(:)),std(height(:)));
         col	     height 1	     height 2	    col angle
       index	           mm	           mm	      degrees
         282	      30.3638	      30.3121	     0.126876
         713	      30.3216	         30.4	     0.095922
        1143	      30.3816	      30.4026	     0.102925


mean height 30.3636 mm, std dev 0.0389673 mm