#include "DualCoding/DualCoding.h"

using namespace DualCoding;

class VisualRecog : public VisualRoutinesStateNode {
 public:
 VisualRecog() : VisualRoutinesStateNode("VisualRecog") {}
  void DoStart() {
    camSkS.clear();
    NEW_SKETCH(camFrame, uchar, sketchFromSeg());

    //get blue blobs
    NEW_SKETCH(b_stuff, bool, visops::colormask(camFrame,"blue"));
    NEW_SKETCH(b_blobs, bool, visops::minArea(b_stuff,10));

    //count number of blue blobs
    NEW_SKETCH(b_cc, uint, visops::labelcc(b_blobs));
    int const numBlueBlobs = b_cc->max();

    //get pink blobs
    NEW_SKETCH(p_stuff, bool, visops::colormask(camFrame,"pink"));
    NEW_SKETCH(p_blobs, bool, visops::minArea(p_stuff,10));

    //count number of pink blobs
    NEW_SKETCH(p_cc, uint, visops::labelcc(p_blobs));
    int const numPinkBlobs = p_cc->max();

    //get green interior mask
    NEW_SKETCH(g_stuff, bool, visops::colormask(camFrame,"green"));
    //filter to get the tape only
    NEW_SKETCH(g_tape, bool, visops::minArea(g_stuff,10));
    //make sure that the tape is closed
    NEW_SKETCH(g_largetape, bool, visops::fillin(g_tape,1,1,8));  	
    NEW_SKETCH(g_mask, bool, visops::fillInterior(g_largetape));

    //get the number of blue blobs  the green interior
    NEW_SKETCH(blueAndGreen, bool, g_mask & b_blobs);
    NEW_SKETCH(b_blobsInside, uint, visops::labelcc(blueAndGreen));    
    int const numBlueInGreen = b_blobsInside->max();

    //get the number of pink blobs outside green interior
    NEW_SKETCH(pinkAndGreen, bool, g_mask & p_blobs);   
    NEW_SKETCH(p_blobsInside, uint, visops::labelcc(pinkAndGreen)); 
    int const numPinkInGreen = p_blobsInside->max();

    // If at least one color inside the green tape has more blobs than outside
    // display all of the blobs inside the green tape.  Otherwise display all
    // of the blobs outside the green tape.

    //int const totalOutside = (numBlueBlobs-numBlueInGreen)+(numPinkBlobs-numPinkInGreen);

    if((numBlueBlobs-numBlueInGreen < numBlueInGreen) || (numPinkBlobs-numPinkInGreen < numPinkInGreen)){
    		     NEW_SKETCH(result, bool, pinkAndGreen | blueAndGreen);
    }else{
	NEW_SKETCH(result, bool, (b_blobs|p_blobs) & !g_mask);
    }
  }
};
