// FILE: scilabHelloVision1_2a.sce - Works
// DATE: 02/19/20 18:48
// AUTH: P.Oh
// REFS: video_capture.dem.sce in Scilab (may need install of image tools)
//       Scilab Computer Vision Module (scicv) is the Scilab interface to the 
//       OpenCV library https://atoms.scilab.org/toolboxes/scicv/0.3
// VERS: 1.2a: determine image characteristics e.g. width and height of image
// REFS: scilabHelloVision1_1b.sci
// DESC: Display what USB webcam sees

// (1) initialize the Scilab Computer Vision Module
scicv_Init(); 

// (2) Get ID of the webcam (assumes only 1 webcam connected)
//     Usually 0: computer's build-in webcam; 1: USB webcam
videoCapture = new_VideoCapture(0); 

// (3) Set up a current graphic figure (window) - which will display our video

f = scf(0); 

// Grab one frame
[ret, frame] = VideoCapture_read(videoCapture); // grab and return a frame
subplot(1,3,1); // Set up 1 row and 2 columns of sub-plots.  Draw in Plot 1
matplot(frame);

disp("Size:");
disp(size(frame));
disp("Number of cols:");
disp(Mat_cols_get(frame));

disp("Number of rows:");
disp(Mat_rows_get(frame));

// (4) Endless loop that grabs frame, displays it, and repeats
while is_handle_valid(f)

    [ret, frame] = VideoCapture_read(videoCapture); // grab and return a frame
  
    if is_handle_valid(f) then      
        // ret is TRUE, so display frame
        subplot(1,3,1); // Set up 1 row and 2 columns of sub-plots.  Draw in Plot 1
        matplot(frame);

        // Draw in Plot 2: Grey-scale version of RGB color frame
        greyFrame = cvtColor(frame, CV_BGR2GRAY);
        subplot(1,3,2);
        matplot(greyFrame);

        // Draw in Plot 3
        // thresholded frame
        // THRESH_BINARY
        // THRESH_BINARY_INV
        thresholdValue = 25; // 0 (whiter stuff becomes white) and 255 (blacker stuff becomes black)
        [thresh, thresholdedFrame]  = threshold(greyFrame, thresholdValue, 255, THRESH_BINARY);
        subplot(1,3,3);
        matplot(thresholdedFrame);
    end // end if  
end // end while


delete("all"); // kill all frames
