// FILE: sciLabTracking0_1d.sce - appears to work... have 0_1e written as per 03/16/20 notes
// DATE: 03/16/20 10:16
// AUTH: P.Oh
// VERS: 0.1a: copied from Scilab help example
//       0.1b: use insight from sciLabReadImage0_1b.sce to report pixel value
//       0.1c: use L (64x64 greyscale) and L-template (16x16)
//       0.1d: renamed PNG files to template.png and image.png
// DESC: Goal: Find object in an image.

scicv_Init();

img = imread("M:\00courses\scilabVideo\image.png");
// matplot(img); // Good! This displayed well

// matplot(img); // displays well, whether in root directory or M:\00courses\me7XX\lesson-Z-visualServoing\
// img = imread("M:\00courses\scilabVideo\64x64-L.png", CV_LOAD_IMAGE_GRAYSCALE);

// img_template = imread(getSampleImage("M:\00courses\scilabVideo\64x64-L-24bits.png"));
// img_template = imread(getSampleImage("10x10-L-24bits.png"));
// img_template = imread(getSampleImage("M:\00courses\scilabVideo\10x10-L-Template-24bits.png"));

img_template = imread("template.png");
// matplot(img_template); // Good! This displayed well

// Strange, but Scilab is not displaying 10x10-L-Template-24bits.png; 10x10-L-Template.png; or 64x64-L-24bits.png
// These files are in M:\00courses\me7XX\lesson-Z-visualServoing\
// Try root directory.  Still doesn't display 64x64-L-24bits.png; 10x10-L-24bits.png
// But it does display puffin.png
// Doesn't matter if put these lines before the first imread above, or keeping these lines here

// img_template = imread(getSampleImage("M:\00courses\me7XX\lesson-Z-visualServoing\10x10-L-Template.png"));

// img_result = matchTemplate(img, img_template, CV_TM_SQDIFF_NORMED);
// perfect match is 0; bad match is large
// img_result = matchTemplate(img, img_template, CV_TM_SQDIFF);
img_result = matchTemplate(img, img_template, CV_TM_CCOEFF_NORMED);
// 1: perfect match; -1: perfect mismatch; 0: no correlation

// Scilab crashes if above line img_result is uncommented


// gcf().color_map = graycolormap(255);
// colorbar(0, 255);

img_out = normalize(img_result, 0, 255, NORM_MINMAX);
matplot(img_out);
disp("img_result(1,1) =");
disp(img_result(1:1, 1:1)); // display pixel value at (1,1)

disp("img_result 2 first rows and columns values = ");
disp(img_result(1:2, 1:2));

disp("img_result: entire");
disp(img_result(:,:));


delete_Mat(img);
delete_Mat(img_template);
// delete_Mat(img_result);


