close all;
%clear all;
improc = ImgProcess();

img = imread('test/qr_test_img.jpg');
%img = dfs(1).img;

% binarize image, enhance, find hollow objects
[bwHollow, bwEnhanced, ~, RGBVisual] = improc.AdaptiveBinarizeAndFilterHollowObjects(img,0,3);

% display binarized image fused with image in false colur
% RGBVisual visualizes operations on binarized image (for details open AdaptiveBinarizeA...)
figure, imshowpair(imfuse(im2double(img),RGBVisual,'falsecolor'),RGBVisual,'montage');

% for each centroid of hollow objects take stripe and compare it to QR
% Marker ratio [1 1 3 1 1], option of directinon xofy 
% y = 1 |; y=2 | & _; y=3 | & _  &\; x = how many direction must match
% ratio with given accuracy (0.5)
[labeledMask, markersPosition] = improc.findMarkers(bwHollow,bwEnhanced,'3of3',0.5);

% overlay found qr markers on image
figure, imshow(imfuse(im2double(img),~labeledMask,'blend'));
hold on, plot(markersPosition(:,1),markersPosition(:,2),'r*')

% find transformation
[tform, refP, PlaneRectangle,imScale] = improc.fitGeoTransformationPreservingPhysicalDim(markersPosition);
% apply transformation
[outIm2, markersOutIm2, outIm1, markersOutIm1, rectOutIm1] = improc.WarpAndCropImg(img,tform,markersPosition,PlaneRectangle);

% show warped image
figure;imshow(outIm1); title('Warped image'), hold on;
plot(markersOutIm1(:,1),markersOutIm1(:,2),'r*')
rectangle('Position',rectOutIm1);

% show warped and cropped image
figure; imshow(outIm2); title('Warped and cropped image'),
hold on; plot(markersOutIm2(:,1),markersOutIm2(:,2),'r*');