Difference between revisions of "CUVI by Example"

From CUVI Wiki
Line 1: Line 1:
<p>CUVI library comes with a lot of image processing building blocks that can be used to build countless applications. For example the Computer Vision module of CUVI can be used for motion detection in a live video stream, intrusion detection and tracking an object of interest throughout a video stream or series of cameras. The processing pipeline for motion detection goes as follows:</p>
*Read a frame from the camera stream
*Select Strong Features in that Frame using CUVI
*Read next frame
*Track features of first frame in the second frame using CUVI
<p>The CUVI functions used in this example are '''goodFeaturesToTrack()''' and '''trackFeatures()'''. For simplicity we have removed the I/O part on host side from the code</p>
{|
{|
|style="font-size:130%;"|
|style="font-size:130%;"|
Line 4: Line 11:


#include <cuvi.h>
#include <cuvi.h>


static const int width = 640; //Width of video frame
static const int width = 640; //Width of video frame
Line 12: Line 18:
static const int featureMinDistance = 3; //Minimum distance between 2 features
static const int featureMinDistance = 3; //Minimum distance between 2 features
static const float k = -2.0f; //k for Harris Corner detector
static const float k = -2.0f; //k for Harris Corner detector
static const int PyramidLevels = 3; //Level Of Scaling
static const int pyramidLevels = 3; //Level Of Scaling
static const CuviSize trackingWindow = cuviSize(30,30); //Size of tracking window
static const CuviSize trackingWindow = cuviSize(30,30); //Size of tracking window
static const float residue = 20.0f; //Absolute Difference Between Original Location Window & Tracked Location Window
static const float residue = 20.0f; //Absolute Difference Between Original Location Window & Tracked Location Window
Line 18: Line 24:
static const bool smoothBeforeSelecting = false; //Smooth Image Before Feature Selection & Tracking
static const bool smoothBeforeSelecting = false; //Smooth Image Before Feature Selection & Tracking
static const bool adjustImage = false; //Adjust Image Light Before Feature Selection
static const bool adjustImage = false; //Adjust Image Light Before Feature Selection
 
static const float movementThreshold = 0.33f; //Mark as motion if a feature moves 0.33 Pixels
 
CuviFilter* Gauss = Cuvi_Builtin_Filters::Gaussian(3,0.7f); //3x3 Gaussian Filter with Standard Deviation 0.7
 
static const float MovementThreshold = 0.33f; //Mark as motion if a feature moves 0.33 Pixels




//Checks if the feature has moved from is original location.
//Checks if the feature has moved from is original location.
//It can be used in intrusion detection and the sensitivity can be set using 'threshold' parameter
//It can be used in intrusion detection and the sensitivity can be set using 'threshold' parameter
bool featureHasMoved(CuviPointValue2D point1, CuviPointValue2D point2, float threshold){
bool featureHasMoved(CuviPointValue2D point1, CuviPointValue2D point2, float threshold){
         if(point2.val != 0.0f) return false;
         if(point2.val != 0.0f) return false;
Line 34: Line 35:




CuviFilter* Gauss = Cuvi_Builtin_Filters::Gaussian(3,0.7f); //3x3 Gaussian Filter with Standard Deviation 0.7


void main()
void main()
Line 62: Line 65:
feature_count = RequestedFeatures; //Reset feature count to original
feature_count = RequestedFeatures; //Reset feature count to original


if(AdjustImage)
if(AdjustImage){
{
cuvi::colorOperations::adjust(gimg1);
cuvi::adjust(gimg1);
cuvi::colorOperations::adjust(gimg2);
cuvi::adjust(gimg2);
}
}


if(SmoothBeforeSelecting)
 
{
if(SmoothBeforeSelecting){
//Apply Gaussian Smoothing Filter On Both The Images
//Apply Gaussian Smoothing Filter On Both The Images
cuvi::imFilter(gimg1,roi,Gauss);
cuvi::imageFiltering::imageFilter(gimg1,roi,Gauss);
cuvi::imFilter(gimg2,roi,Gauss);
cuvi::imageFiltering::imageFilter(gimg2,roi,Gauss);
}
}
//Call A Feature Detector ( KLT | HARRIS | PETER )
//Call A Feature Detector ( KLT | HARRIS | PETER )
cuvi::goodFeaturesToTrack(gimg1,roi,features1,&feature_count,CUVI_FEATURES_HARRIS,FeatureQuality,FeatureMinDistance,3,k);
cuvi::computerVision::goodFeaturesToTrack(gimg1,roi,features1,&feature_count,CUVI_FEATURES_HARRIS,FeatureQuality,FeatureMinDistance,3,k);
//Track Features Using KLT Method
//Track Features Using KLT Method
cuvi::trackFeatures(gimg1,gimg2,features1,features2,feature_count,PyramidLevels,TrackingWindow,Residue,Iterations);
cuvi::trackFeatures(gimg1,gimg2,features1,features2,feature_count,PyramidLevels,TrackingWindow,Residue,Iterations);


//Plot The Tracked Features
//At this point you can indetify whether the selected features in frame one moved in frame two or not
for(int i=0; i<feature_count; i++)
for(int i=0; i<feature_count; i++)
if(FeatureHasMoved(features1[i],features2[i],MovementThreshold)) //Plot Only If The Feature Has Moved From Its Location
      if(FeatureHasMoved(features1[i],features2[i],MovementThreshold)) //Plot Only If The Feature Has Moved From Its Location


//You can also plot the tracked features on the screen




}


}
//Freeing GPU Memory
gFrame->release();
gimg1->release();
gimg2->release();


gFrame->release();
gimg1->release();
gimg2->release();
}
}


|}
|}

Revision as of 19:00, 30 April 2012

CUVI library comes with a lot of image processing building blocks that can be used to build countless applications. For example the Computer Vision module of CUVI can be used for motion detection in a live video stream, intrusion detection and tracking an object of interest throughout a video stream or series of cameras. The processing pipeline for motion detection goes as follows:

  • Read a frame from the camera stream
  • Select Strong Features in that Frame using CUVI
  • Read next frame
  • Track features of first frame in the second frame using CUVI

The CUVI functions used in this example are goodFeaturesToTrack() and trackFeatures(). For simplicity we have removed the I/O part on host side from the code

<syntaxhighlight lang="c">

  1. include <cuvi.h>

static const int width = 640; //Width of video frame static const int height = 480; //Height of video frame static const int requestedFeatures = 150; //Number of features to look for static const float featureQuality = 0.006f; //Quality of a feature static const int featureMinDistance = 3; //Minimum distance between 2 features static const float k = -2.0f; //k for Harris Corner detector static const int pyramidLevels = 3; //Level Of Scaling static const CuviSize trackingWindow = cuviSize(30,30); //Size of tracking window static const float residue = 20.0f; //Absolute Difference Between Original Location Window & Tracked Location Window static const int iterations = 10; //Maximum number of iterations before a feature is found static const bool smoothBeforeSelecting = false; //Smooth Image Before Feature Selection & Tracking static const bool adjustImage = false; //Adjust Image Light Before Feature Selection static const float movementThreshold = 0.33f; //Mark as motion if a feature moves 0.33 Pixels


//Checks if the feature has moved from is original location. //It can be used in intrusion detection and the sensitivity can be set using 'threshold' parameter bool featureHasMoved(CuviPointValue2D point1, CuviPointValue2D point2, float threshold){

       if(point2.val != 0.0f)	return false;

return ((fabsf(point1.x - point2.x)>threshold) || (fabsf(point1.y - point2.y)>threshold)); }


CuviFilter* Gauss = Cuvi_Builtin_Filters::Gaussian(3,0.7f); //3x3 Gaussian Filter with Standard Deviation 0.7

void main() {

      //Read a Video Frame

CuviImage* gFrame = new CuviImage(Width,Height,GetOpenCVPitch(Width,Height,8,3),8,3);

CuviImage* gimg1 = new CuviImage(Width,Height,GetOpenCVPitch(Width,Height,8,1),8,1); CuviImage* gimg2 = new CuviImage(Width,Height,GetOpenCVPitch(Width,Height,8,1),8,1);

CuviROI roi = cuviROI(0,0,Width,Height);

CuviPointValue2D *features1, *features2;

int feature_count = 0;

do {

               //Read Frame

gFrame->upload(frame->imageData); cuvi::colorConvert(gFrame,gimg1);

//Read Next Frame gFrame->upload(frame->imageData); cuvi::colorConvert(gFrame,gimg2);

feature_count = RequestedFeatures; //Reset feature count to original

if(AdjustImage){ cuvi::colorOperations::adjust(gimg1); cuvi::colorOperations::adjust(gimg2); }


if(SmoothBeforeSelecting){ //Apply Gaussian Smoothing Filter On Both The Images cuvi::imageFiltering::imageFilter(gimg1,roi,Gauss); cuvi::imageFiltering::imageFilter(gimg2,roi,Gauss); }

//Call A Feature Detector ( KLT | HARRIS | PETER ) cuvi::computerVision::goodFeaturesToTrack(gimg1,roi,features1,&feature_count,CUVI_FEATURES_HARRIS,FeatureQuality,FeatureMinDistance,3,k);

//Track Features Using KLT Method cuvi::trackFeatures(gimg1,gimg2,features1,features2,feature_count,PyramidLevels,TrackingWindow,Residue,Iterations);

//At this point you can indetify whether the selected features in frame one moved in frame two or not for(int i=0; i<feature_count; i++)

     if(FeatureHasMoved(features1[i],features2[i],MovementThreshold)) //Plot Only If The Feature Has Moved From Its Location

//You can also plot the tracked features on the screen


}

//Freeing GPU Memory gFrame->release(); gimg1->release(); gimg2->release();

}