converted some OpenCV C samples to C++
This commit is contained in:
@@ -1,224 +0,0 @@
|
||||
#include <opencv2/video/tracking.hpp>
|
||||
#include <opencv2/highgui/highgui.hpp>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
|
||||
IplImage *image = 0, *hsv = 0, *hue = 0, *mask = 0, *backproject = 0, *histimg = 0;
|
||||
CvHistogram *hist = 0;
|
||||
|
||||
int backproject_mode = 0;
|
||||
int select_object = 0;
|
||||
int track_object = 0;
|
||||
int show_hist = 1;
|
||||
CvPoint origin;
|
||||
CvRect selection;
|
||||
CvRect track_window;
|
||||
CvBox2D track_box;
|
||||
CvConnectedComp track_comp;
|
||||
int hdims = 16;
|
||||
float hranges_arr[] = {0,180};
|
||||
float* hranges = hranges_arr;
|
||||
int vmin = 10, vmax = 256, smin = 30;
|
||||
|
||||
void on_mouse( int event, int x, int y, int flags, void* param )
|
||||
{
|
||||
if( !image )
|
||||
return;
|
||||
|
||||
if( image->origin )
|
||||
y = image->height - y;
|
||||
|
||||
if( select_object )
|
||||
{
|
||||
selection.x = MIN(x,origin.x);
|
||||
selection.y = MIN(y,origin.y);
|
||||
selection.width = selection.x + CV_IABS(x - origin.x);
|
||||
selection.height = selection.y + CV_IABS(y - origin.y);
|
||||
|
||||
selection.x = MAX( selection.x, 0 );
|
||||
selection.y = MAX( selection.y, 0 );
|
||||
selection.width = MIN( selection.width, image->width );
|
||||
selection.height = MIN( selection.height, image->height );
|
||||
selection.width -= selection.x;
|
||||
selection.height -= selection.y;
|
||||
}
|
||||
|
||||
switch( event )
|
||||
{
|
||||
case CV_EVENT_LBUTTONDOWN:
|
||||
origin = cvPoint(x,y);
|
||||
selection = cvRect(x,y,0,0);
|
||||
select_object = 1;
|
||||
break;
|
||||
case CV_EVENT_LBUTTONUP:
|
||||
select_object = 0;
|
||||
if( selection.width > 0 && selection.height > 0 )
|
||||
track_object = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
CvScalar hsv2rgb( float hue )
|
||||
{
|
||||
int rgb[3], p, sector;
|
||||
static const int sector_data[][3]=
|
||||
{{0,2,1}, {1,2,0}, {1,0,2}, {2,0,1}, {2,1,0}, {0,1,2}};
|
||||
hue *= 0.033333333333333333333333333333333f;
|
||||
sector = cvFloor(hue);
|
||||
p = cvRound(255*(hue - sector));
|
||||
p ^= sector & 1 ? 255 : 0;
|
||||
|
||||
rgb[sector_data[sector][0]] = 255;
|
||||
rgb[sector_data[sector][1]] = 0;
|
||||
rgb[sector_data[sector][2]] = p;
|
||||
|
||||
return cvScalar(rgb[2], rgb[1], rgb[0],0);
|
||||
}
|
||||
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
CvCapture* capture = 0;
|
||||
|
||||
if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
|
||||
capture = cvCaptureFromCAM( argc == 2 ? argv[1][0] - '0' : 0 );
|
||||
else if( argc == 2 )
|
||||
capture = cvCaptureFromAVI( argv[1] );
|
||||
|
||||
if( !capture )
|
||||
{
|
||||
fprintf(stderr,"Could not initialize capturing...\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf( "Hot keys: \n"
|
||||
"\tESC - quit the program\n"
|
||||
"\tc - stop the tracking\n"
|
||||
"\tb - switch to/from backprojection view\n"
|
||||
"\th - show/hide object histogram\n"
|
||||
"To initialize tracking, select the object with mouse\n" );
|
||||
|
||||
cvNamedWindow( "Histogram", 1 );
|
||||
cvNamedWindow( "CamShiftDemo", 1 );
|
||||
cvSetMouseCallback( "CamShiftDemo", on_mouse, 0 );
|
||||
cvCreateTrackbar( "Vmin", "CamShiftDemo", &vmin, 256, 0 );
|
||||
cvCreateTrackbar( "Vmax", "CamShiftDemo", &vmax, 256, 0 );
|
||||
cvCreateTrackbar( "Smin", "CamShiftDemo", &smin, 256, 0 );
|
||||
|
||||
for(;;)
|
||||
{
|
||||
IplImage* frame = 0;
|
||||
int i, bin_w, c;
|
||||
|
||||
frame = cvQueryFrame( capture );
|
||||
if( !frame )
|
||||
break;
|
||||
|
||||
if( !image )
|
||||
{
|
||||
/* allocate all the buffers */
|
||||
image = cvCreateImage( cvGetSize(frame), 8, 3 );
|
||||
image->origin = frame->origin;
|
||||
hsv = cvCreateImage( cvGetSize(frame), 8, 3 );
|
||||
hue = cvCreateImage( cvGetSize(frame), 8, 1 );
|
||||
mask = cvCreateImage( cvGetSize(frame), 8, 1 );
|
||||
backproject = cvCreateImage( cvGetSize(frame), 8, 1 );
|
||||
hist = cvCreateHist( 1, &hdims, CV_HIST_ARRAY, &hranges, 1 );
|
||||
histimg = cvCreateImage( cvSize(320,200), 8, 3 );
|
||||
cvZero( histimg );
|
||||
}
|
||||
|
||||
cvCopy( frame, image, 0 );
|
||||
cvCvtColor( image, hsv, CV_BGR2HSV );
|
||||
|
||||
if( track_object )
|
||||
{
|
||||
int _vmin = vmin, _vmax = vmax;
|
||||
|
||||
cvInRangeS( hsv, cvScalar(0,smin,MIN(_vmin,_vmax),0),
|
||||
cvScalar(180,256,MAX(_vmin,_vmax),0), mask );
|
||||
cvSplit( hsv, hue, 0, 0, 0 );
|
||||
|
||||
if( track_object < 0 )
|
||||
{
|
||||
float max_val = 0.f;
|
||||
cvSetImageROI( hue, selection );
|
||||
cvSetImageROI( mask, selection );
|
||||
cvCalcHist( &hue, hist, 0, mask );
|
||||
cvGetMinMaxHistValue( hist, 0, &max_val, 0, 0 );
|
||||
cvConvertScale( hist->bins, hist->bins, max_val ? 255. / max_val : 0., 0 );
|
||||
cvResetImageROI( hue );
|
||||
cvResetImageROI( mask );
|
||||
track_window = selection;
|
||||
track_object = 1;
|
||||
|
||||
cvZero( histimg );
|
||||
bin_w = histimg->width / hdims;
|
||||
for( i = 0; i < hdims; i++ )
|
||||
{
|
||||
int val = cvRound( cvGetReal1D(hist->bins,i)*histimg->height/255 );
|
||||
CvScalar color = hsv2rgb(i*180.f/hdims);
|
||||
cvRectangle( histimg, cvPoint(i*bin_w,histimg->height),
|
||||
cvPoint((i+1)*bin_w,histimg->height - val),
|
||||
color, -1, 8, 0 );
|
||||
}
|
||||
}
|
||||
|
||||
cvCalcBackProject( &hue, backproject, hist );
|
||||
cvAnd( backproject, mask, backproject, 0 );
|
||||
cvCamShift( backproject, track_window,
|
||||
cvTermCriteria( CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 10, 1 ),
|
||||
&track_comp, &track_box );
|
||||
track_window = track_comp.rect;
|
||||
|
||||
if( backproject_mode )
|
||||
cvCvtColor( backproject, image, CV_GRAY2BGR );
|
||||
if( !image->origin )
|
||||
track_box.angle = -track_box.angle;
|
||||
cvEllipseBox( image, track_box, CV_RGB(255,0,0), 3, CV_AA, 0 );
|
||||
}
|
||||
|
||||
if( select_object && selection.width > 0 && selection.height > 0 )
|
||||
{
|
||||
cvSetImageROI( image, selection );
|
||||
cvXorS( image, cvScalarAll(255), image, 0 );
|
||||
cvResetImageROI( image );
|
||||
}
|
||||
|
||||
cvShowImage( "CamShiftDemo", image );
|
||||
cvShowImage( "Histogram", histimg );
|
||||
|
||||
c = cvWaitKey(10);
|
||||
if( (char) c == 27 )
|
||||
break;
|
||||
switch( (char) c )
|
||||
{
|
||||
case 'b':
|
||||
backproject_mode ^= 1;
|
||||
break;
|
||||
case 'c':
|
||||
track_object = 0;
|
||||
cvZero( histimg );
|
||||
break;
|
||||
case 'h':
|
||||
show_hist ^= 1;
|
||||
if( !show_hist )
|
||||
cvDestroyWindow( "Histogram" );
|
||||
else
|
||||
cvNamedWindow( "Histogram", 1 );
|
||||
break;
|
||||
default:
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
cvReleaseCapture( &capture );
|
||||
cvDestroyWindow("CamShiftDemo");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef _EiC
|
||||
main(1,"camshiftdemo.c");
|
||||
#endif
|
@@ -1,97 +0,0 @@
|
||||
#include <opencv2/imgproc/imgproc.hpp>
|
||||
#include <opencv2/highgui/highgui.hpp>
|
||||
|
||||
#define ARRAY 1
|
||||
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
IplImage* img = cvCreateImage( cvSize( 500, 500 ), 8, 3 );
|
||||
#if !ARRAY
|
||||
CvMemStorage* storage = cvCreateMemStorage(0);
|
||||
#endif
|
||||
|
||||
cvNamedWindow( "hull", 1 );
|
||||
|
||||
for(;;)
|
||||
{
|
||||
char key;
|
||||
int i, count = rand()%100 + 1, hullcount;
|
||||
CvPoint pt0;
|
||||
#if !ARRAY
|
||||
CvSeq* ptseq = cvCreateSeq( CV_SEQ_KIND_GENERIC|CV_32SC2, sizeof(CvContour),
|
||||
sizeof(CvPoint), storage );
|
||||
CvSeq* hull;
|
||||
|
||||
for( i = 0; i < count; i++ )
|
||||
{
|
||||
pt0.x = rand() % (img->width/2) + img->width/4;
|
||||
pt0.y = rand() % (img->height/2) + img->height/4;
|
||||
cvSeqPush( ptseq, &pt0 );
|
||||
}
|
||||
hull = cvConvexHull2( ptseq, 0, CV_CLOCKWISE, 0 );
|
||||
hullcount = hull->total;
|
||||
#else
|
||||
CvPoint* points = (CvPoint*)malloc( count * sizeof(points[0]));
|
||||
int* hull = (int*)malloc( count * sizeof(hull[0]));
|
||||
CvMat pointMat = cvMat( 1, count, CV_32SC2, points );
|
||||
CvMat hullMat = cvMat( 1, count, CV_32SC1, hull );
|
||||
|
||||
for( i = 0; i < count; i++ )
|
||||
{
|
||||
pt0.x = rand() % (img->width/2) + img->width/4;
|
||||
pt0.y = rand() % (img->height/2) + img->height/4;
|
||||
points[i] = pt0;
|
||||
}
|
||||
cvConvexHull2( &pointMat, &hullMat, CV_CLOCKWISE, 0 );
|
||||
hullcount = hullMat.cols;
|
||||
#endif
|
||||
cvZero( img );
|
||||
for( i = 0; i < count; i++ )
|
||||
{
|
||||
#if !ARRAY
|
||||
pt0 = *CV_GET_SEQ_ELEM( CvPoint, ptseq, i );
|
||||
#else
|
||||
pt0 = points[i];
|
||||
#endif
|
||||
cvCircle( img, pt0, 2, CV_RGB( 255, 0, 0 ), CV_FILLED, CV_AA, 0 );
|
||||
}
|
||||
|
||||
#if !ARRAY
|
||||
pt0 = **CV_GET_SEQ_ELEM( CvPoint*, hull, hullcount - 1 );
|
||||
#else
|
||||
pt0 = points[hull[hullcount-1]];
|
||||
#endif
|
||||
|
||||
for( i = 0; i < hullcount; i++ )
|
||||
{
|
||||
#if !ARRAY
|
||||
CvPoint pt = **CV_GET_SEQ_ELEM( CvPoint*, hull, i );
|
||||
#else
|
||||
CvPoint pt = points[hull[i]];
|
||||
#endif
|
||||
cvLine( img, pt0, pt, CV_RGB( 0, 255, 0 ), 1, CV_AA, 0 );
|
||||
pt0 = pt;
|
||||
}
|
||||
|
||||
cvShowImage( "hull", img );
|
||||
|
||||
key = (char) cvWaitKey(0);
|
||||
if( key == 27 || key == 'q' || key == 'Q' ) // 'ESC'
|
||||
break;
|
||||
|
||||
#if !ARRAY
|
||||
cvClearMemStorage( storage );
|
||||
#else
|
||||
free( points );
|
||||
free( hull );
|
||||
#endif
|
||||
}
|
||||
|
||||
cvDestroyWindow( "hull" );
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef _EiC
|
||||
main(1,"convexhull.c");
|
||||
#endif
|
||||
|
@@ -1,120 +0,0 @@
|
||||
#include <opencv2/imgproc/imgproc_c.h>
|
||||
#include <opencv2/highgui/highgui.hpp>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
char file_name[] = "baboon.jpg";
|
||||
|
||||
int _brightness = 100;
|
||||
int _contrast = 100;
|
||||
|
||||
int hist_size = 64;
|
||||
float range_0[]={0,256};
|
||||
float* ranges[] = { range_0 };
|
||||
IplImage *src_image = 0, *dst_image = 0, *hist_image = 0;
|
||||
CvHistogram *hist;
|
||||
uchar lut[256];
|
||||
CvMat* lut_mat;
|
||||
|
||||
/* brightness/contrast callback function */
|
||||
void update_brightcont( int arg )
|
||||
{
|
||||
int brightness = _brightness - 100;
|
||||
int contrast = _contrast - 100;
|
||||
int i, bin_w;
|
||||
float max_value = 0;
|
||||
|
||||
/*
|
||||
* The algorithm is by Werner D. Streidt
|
||||
* (http://visca.com/ffactory/archives/5-99/msg00021.html)
|
||||
*/
|
||||
if( contrast > 0 )
|
||||
{
|
||||
double delta = 127.*contrast/100;
|
||||
double a = 255./(255. - delta*2);
|
||||
double b = a*(brightness - delta);
|
||||
for( i = 0; i < 256; i++ )
|
||||
{
|
||||
int v = cvRound(a*i + b);
|
||||
if( v < 0 )
|
||||
v = 0;
|
||||
if( v > 255 )
|
||||
v = 255;
|
||||
lut[i] = (uchar)v;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
double delta = -128.*contrast/100;
|
||||
double a = (256.-delta*2)/255.;
|
||||
double b = a*brightness + delta;
|
||||
for( i = 0; i < 256; i++ )
|
||||
{
|
||||
int v = cvRound(a*i + b);
|
||||
if( v < 0 )
|
||||
v = 0;
|
||||
if( v > 255 )
|
||||
v = 255;
|
||||
lut[i] = (uchar)v;
|
||||
}
|
||||
}
|
||||
|
||||
cvLUT( src_image, dst_image, lut_mat );
|
||||
cvShowImage( "image", dst_image );
|
||||
|
||||
cvCalcHist( &dst_image, hist, 0, NULL );
|
||||
cvZero( dst_image );
|
||||
cvGetMinMaxHistValue( hist, 0, &max_value, 0, 0 );
|
||||
cvScale( hist->bins, hist->bins, ((double)hist_image->height)/max_value, 0 );
|
||||
/*cvNormalizeHist( hist, 1000 );*/
|
||||
|
||||
cvSet( hist_image, cvScalarAll(255), 0 );
|
||||
bin_w = cvRound((double)hist_image->width/hist_size);
|
||||
|
||||
for( i = 0; i < hist_size; i++ )
|
||||
cvRectangle( hist_image, cvPoint(i*bin_w, hist_image->height),
|
||||
cvPoint((i+1)*bin_w, hist_image->height - cvRound(cvGetReal1D(hist->bins,i))),
|
||||
cvScalarAll(0), -1, 8, 0 );
|
||||
|
||||
cvShowImage( "histogram", hist_image );
|
||||
}
|
||||
|
||||
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
// Load the source image. HighGUI use.
|
||||
src_image = cvLoadImage( argc == 2 ? argv[1] : file_name, 0 );
|
||||
|
||||
if( !src_image )
|
||||
{
|
||||
printf("Image was not loaded.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
dst_image = cvCloneImage(src_image);
|
||||
hist_image = cvCreateImage(cvSize(320,200), 8, 1);
|
||||
hist = cvCreateHist(1, &hist_size, CV_HIST_ARRAY, ranges, 1);
|
||||
lut_mat = cvCreateMatHeader( 1, 256, CV_8UC1 );
|
||||
cvSetData( lut_mat, lut, 0 );
|
||||
|
||||
cvNamedWindow("image", 0);
|
||||
cvNamedWindow("histogram", 0);
|
||||
|
||||
cvCreateTrackbar("brightness", "image", &_brightness, 200, update_brightcont);
|
||||
cvCreateTrackbar("contrast", "image", &_contrast, 200, update_brightcont);
|
||||
|
||||
update_brightcont(0);
|
||||
cvWaitKey(0);
|
||||
|
||||
cvReleaseImage(&src_image);
|
||||
cvReleaseImage(&dst_image);
|
||||
|
||||
cvReleaseHist(&hist);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef _EiC
|
||||
main(1,"demhist.c");
|
||||
#endif
|
||||
|
136
samples/c/dft.c
136
samples/c/dft.c
@@ -1,136 +0,0 @@
|
||||
#include <opencv2/core/core.hpp>
|
||||
#include <opencv2/highgui/highgui.hpp>
|
||||
|
||||
// Rearrange the quadrants of Fourier image so that the origin is at
|
||||
// the image center
|
||||
// src & dst arrays of equal size & type
|
||||
void cvShiftDFT(CvArr * src_arr, CvArr * dst_arr )
|
||||
{
|
||||
CvMat * tmp=0;
|
||||
CvMat q1stub, q2stub;
|
||||
CvMat q3stub, q4stub;
|
||||
CvMat d1stub, d2stub;
|
||||
CvMat d3stub, d4stub;
|
||||
CvMat * q1, * q2, * q3, * q4;
|
||||
CvMat * d1, * d2, * d3, * d4;
|
||||
|
||||
CvSize size = cvGetSize(src_arr);
|
||||
CvSize dst_size = cvGetSize(dst_arr);
|
||||
int cx, cy;
|
||||
|
||||
if(dst_size.width != size.width ||
|
||||
dst_size.height != size.height){
|
||||
cvError( CV_StsUnmatchedSizes, "cvShiftDFT", "Source and Destination arrays must have equal sizes", __FILE__, __LINE__ );
|
||||
}
|
||||
|
||||
if(src_arr==dst_arr){
|
||||
tmp = cvCreateMat(size.height/2, size.width/2, cvGetElemType(src_arr));
|
||||
}
|
||||
|
||||
cx = size.width/2;
|
||||
cy = size.height/2; // image center
|
||||
|
||||
q1 = cvGetSubRect( src_arr, &q1stub, cvRect(0,0,cx, cy) );
|
||||
q2 = cvGetSubRect( src_arr, &q2stub, cvRect(cx,0,cx,cy) );
|
||||
q3 = cvGetSubRect( src_arr, &q3stub, cvRect(cx,cy,cx,cy) );
|
||||
q4 = cvGetSubRect( src_arr, &q4stub, cvRect(0,cy,cx,cy) );
|
||||
d1 = cvGetSubRect( src_arr, &d1stub, cvRect(0,0,cx,cy) );
|
||||
d2 = cvGetSubRect( src_arr, &d2stub, cvRect(cx,0,cx,cy) );
|
||||
d3 = cvGetSubRect( src_arr, &d3stub, cvRect(cx,cy,cx,cy) );
|
||||
d4 = cvGetSubRect( src_arr, &d4stub, cvRect(0,cy,cx,cy) );
|
||||
|
||||
if(src_arr!=dst_arr){
|
||||
if( !CV_ARE_TYPES_EQ( q1, d1 )){
|
||||
cvError( CV_StsUnmatchedFormats, "cvShiftDFT", "Source and Destination arrays must have the same format", __FILE__, __LINE__ );
|
||||
}
|
||||
cvCopy(q3, d1, 0);
|
||||
cvCopy(q4, d2, 0);
|
||||
cvCopy(q1, d3, 0);
|
||||
cvCopy(q2, d4, 0);
|
||||
}
|
||||
else{
|
||||
cvCopy(q3, tmp, 0);
|
||||
cvCopy(q1, q3, 0);
|
||||
cvCopy(tmp, q1, 0);
|
||||
cvCopy(q4, tmp, 0);
|
||||
cvCopy(q2, q4, 0);
|
||||
cvCopy(tmp, q2, 0);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char ** argv)
|
||||
{
|
||||
const char* filename = argc >=2 ? argv[1] : "lena.jpg";
|
||||
IplImage * im;
|
||||
|
||||
IplImage * realInput;
|
||||
IplImage * imaginaryInput;
|
||||
IplImage * complexInput;
|
||||
int dft_M, dft_N;
|
||||
CvMat* dft_A, tmp;
|
||||
IplImage * image_Re;
|
||||
IplImage * image_Im;
|
||||
double m, M;
|
||||
|
||||
im = cvLoadImage( filename, CV_LOAD_IMAGE_GRAYSCALE );
|
||||
if( !im )
|
||||
return -1;
|
||||
|
||||
realInput = cvCreateImage( cvGetSize(im), IPL_DEPTH_64F, 1);
|
||||
imaginaryInput = cvCreateImage( cvGetSize(im), IPL_DEPTH_64F, 1);
|
||||
complexInput = cvCreateImage( cvGetSize(im), IPL_DEPTH_64F, 2);
|
||||
|
||||
cvScale(im, realInput, 1.0, 0.0);
|
||||
cvZero(imaginaryInput);
|
||||
cvMerge(realInput, imaginaryInput, NULL, NULL, complexInput);
|
||||
|
||||
dft_M = cvGetOptimalDFTSize( im->height - 1 );
|
||||
dft_N = cvGetOptimalDFTSize( im->width - 1 );
|
||||
|
||||
dft_A = cvCreateMat( dft_M, dft_N, CV_64FC2 );
|
||||
image_Re = cvCreateImage( cvSize(dft_N, dft_M), IPL_DEPTH_64F, 1);
|
||||
image_Im = cvCreateImage( cvSize(dft_N, dft_M), IPL_DEPTH_64F, 1);
|
||||
|
||||
// copy A to dft_A and pad dft_A with zeros
|
||||
cvGetSubRect( dft_A, &tmp, cvRect(0,0, im->width, im->height));
|
||||
cvCopy( complexInput, &tmp, NULL );
|
||||
if( dft_A->cols > im->width )
|
||||
{
|
||||
cvGetSubRect( dft_A, &tmp, cvRect(im->width,0, dft_A->cols - im->width, im->height));
|
||||
cvZero( &tmp );
|
||||
}
|
||||
|
||||
// no need to pad bottom part of dft_A with zeros because of
|
||||
// use nonzero_rows parameter in cvDFT() call below
|
||||
|
||||
cvDFT( dft_A, dft_A, CV_DXT_FORWARD, complexInput->height );
|
||||
|
||||
cvNamedWindow("win", 0);
|
||||
cvNamedWindow("magnitude", 0);
|
||||
cvShowImage("win", im);
|
||||
|
||||
// Split Fourier in real and imaginary parts
|
||||
cvSplit( dft_A, image_Re, image_Im, 0, 0 );
|
||||
|
||||
// Compute the magnitude of the spectrum Mag = sqrt(Re^2 + Im^2)
|
||||
cvPow( image_Re, image_Re, 2.0);
|
||||
cvPow( image_Im, image_Im, 2.0);
|
||||
cvAdd( image_Re, image_Im, image_Re, NULL);
|
||||
cvPow( image_Re, image_Re, 0.5 );
|
||||
|
||||
// Compute log(1 + Mag)
|
||||
cvAddS( image_Re, cvScalarAll(1.0), image_Re, NULL ); // 1 + Mag
|
||||
cvLog( image_Re, image_Re ); // log(1 + Mag)
|
||||
|
||||
|
||||
// Rearrange the quadrants of Fourier image so that the origin is at
|
||||
// the image center
|
||||
cvShiftDFT( image_Re, image_Re );
|
||||
|
||||
cvMinMaxLoc(image_Re, &m, &M, NULL, NULL, NULL);
|
||||
cvScale(image_Re, image_Re, 1.0/(M-m), 1.0*(-m)/(M-m));
|
||||
cvShowImage("magnitude", image_Re);
|
||||
|
||||
cvWaitKey(-1);
|
||||
return 0;
|
||||
}
|
@@ -1,184 +0,0 @@
|
||||
#include <opencv2/imgproc/imgproc_c.h>
|
||||
#include <opencv2/highgui/highgui.hpp>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
char wndname[] = "Distance transform";
|
||||
char tbarname[] = "Threshold";
|
||||
int mask_size = CV_DIST_MASK_5;
|
||||
int build_voronoi = 0;
|
||||
int edge_thresh = 100;
|
||||
int dist_type = CV_DIST_L1;
|
||||
|
||||
// The output and temporary images
|
||||
IplImage* dist = 0;
|
||||
IplImage* dist8u1 = 0;
|
||||
IplImage* dist8u2 = 0;
|
||||
IplImage* dist8u = 0;
|
||||
IplImage* dist32s = 0;
|
||||
|
||||
IplImage* gray = 0;
|
||||
IplImage* edge = 0;
|
||||
IplImage* labels = 0;
|
||||
|
||||
// threshold trackbar callback
|
||||
void on_trackbar( int dummy )
|
||||
{
|
||||
static const uchar colors[][3] =
|
||||
{
|
||||
{0,0,0},
|
||||
{255,0,0},
|
||||
{255,128,0},
|
||||
{255,255,0},
|
||||
{0,255,0},
|
||||
{0,128,255},
|
||||
{0,255,255},
|
||||
{0,0,255},
|
||||
{255,0,255}
|
||||
};
|
||||
|
||||
int msize = mask_size;
|
||||
int _dist_type = build_voronoi ? CV_DIST_L2 : dist_type;
|
||||
|
||||
cvThreshold( gray, edge, (float)edge_thresh, (float)edge_thresh, CV_THRESH_BINARY );
|
||||
|
||||
if( build_voronoi )
|
||||
msize = CV_DIST_MASK_5;
|
||||
|
||||
if( _dist_type == CV_DIST_L1 )
|
||||
{
|
||||
cvDistTransform( edge, edge, _dist_type, msize, NULL, NULL );
|
||||
cvConvert( edge, dist );
|
||||
}
|
||||
else
|
||||
cvDistTransform( edge, dist, _dist_type, msize, NULL, build_voronoi ? labels : NULL );
|
||||
|
||||
if( !build_voronoi )
|
||||
{
|
||||
// begin "painting" the distance transform result
|
||||
cvConvertScale( dist, dist, 5000.0, 0 );
|
||||
cvPow( dist, dist, 0.5 );
|
||||
|
||||
cvConvertScale( dist, dist32s, 1.0, 0.5 );
|
||||
cvAndS( dist32s, cvScalarAll(255), dist32s, 0 );
|
||||
cvConvertScale( dist32s, dist8u1, 1, 0 );
|
||||
cvConvertScale( dist32s, dist32s, -1, 0 );
|
||||
cvAddS( dist32s, cvScalarAll(255), dist32s, 0 );
|
||||
cvConvertScale( dist32s, dist8u2, 1, 0 );
|
||||
cvMerge( dist8u1, dist8u2, dist8u2, 0, dist8u );
|
||||
// end "painting" the distance transform result
|
||||
}
|
||||
else
|
||||
{
|
||||
int i, j;
|
||||
for( i = 0; i < labels->height; i++ )
|
||||
{
|
||||
int* ll = (int*)(labels->imageData + i*labels->widthStep);
|
||||
float* dd = (float*)(dist->imageData + i*dist->widthStep);
|
||||
uchar* d = (uchar*)(dist8u->imageData + i*dist8u->widthStep);
|
||||
for( j = 0; j < labels->width; j++ )
|
||||
{
|
||||
int idx = ll[j] == 0 || dd[j] == 0 ? 0 : (ll[j]-1)%8 + 1;
|
||||
int b = cvRound(colors[idx][0]);
|
||||
int g = cvRound(colors[idx][1]);
|
||||
int r = cvRound(colors[idx][2]);
|
||||
d[j*3] = (uchar)b;
|
||||
d[j*3+1] = (uchar)g;
|
||||
d[j*3+2] = (uchar)r;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cvShowImage( wndname, dist8u );
|
||||
}
|
||||
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
char* filename = argc == 2 ? argv[1] : (char*)"stuff.jpg";
|
||||
|
||||
if( (gray = cvLoadImage( filename, 0 )) == 0 )
|
||||
return -1;
|
||||
|
||||
printf( "Hot keys: \n"
|
||||
"\tESC - quit the program\n"
|
||||
"\tC - use C/Inf metric\n"
|
||||
"\tL1 - use L1 metric\n"
|
||||
"\tL2 - use L2 metric\n"
|
||||
"\t3 - use 3x3 mask\n"
|
||||
"\t5 - use 5x5 mask\n"
|
||||
"\t0 - use precise distance transform\n"
|
||||
"\tv - switch Voronoi diagram mode on/off\n"
|
||||
"\tSPACE - loop through all the modes\n" );
|
||||
|
||||
dist = cvCreateImage( cvGetSize(gray), IPL_DEPTH_32F, 1 );
|
||||
dist8u1 = cvCloneImage( gray );
|
||||
dist8u2 = cvCloneImage( gray );
|
||||
dist8u = cvCreateImage( cvGetSize(gray), IPL_DEPTH_8U, 3 );
|
||||
dist32s = cvCreateImage( cvGetSize(gray), IPL_DEPTH_32S, 1 );
|
||||
edge = cvCloneImage( gray );
|
||||
labels = cvCreateImage( cvGetSize(gray), IPL_DEPTH_32S, 1 );
|
||||
|
||||
cvNamedWindow( wndname, 1 );
|
||||
|
||||
cvCreateTrackbar( tbarname, wndname, &edge_thresh, 255, on_trackbar );
|
||||
|
||||
for(;;)
|
||||
{
|
||||
int c;
|
||||
|
||||
// Call to update the view
|
||||
on_trackbar(0);
|
||||
|
||||
c = cvWaitKey(0);
|
||||
|
||||
if( (char)c == 27 )
|
||||
break;
|
||||
|
||||
if( (char)c == 'c' || (char)c == 'C' )
|
||||
dist_type = CV_DIST_C;
|
||||
else if( (char)c == '1' )
|
||||
dist_type = CV_DIST_L1;
|
||||
else if( (char)c == '2' )
|
||||
dist_type = CV_DIST_L2;
|
||||
else if( (char)c == '3' )
|
||||
mask_size = CV_DIST_MASK_3;
|
||||
else if( (char)c == '5' )
|
||||
mask_size = CV_DIST_MASK_5;
|
||||
else if( (char)c == '0' )
|
||||
mask_size = CV_DIST_MASK_PRECISE;
|
||||
else if( (char)c == 'v' )
|
||||
build_voronoi ^= 1;
|
||||
else if( (char)c == ' ' )
|
||||
{
|
||||
if( build_voronoi )
|
||||
{
|
||||
build_voronoi = 0;
|
||||
mask_size = CV_DIST_MASK_3;
|
||||
dist_type = CV_DIST_C;
|
||||
}
|
||||
else if( dist_type == CV_DIST_C )
|
||||
dist_type = CV_DIST_L1;
|
||||
else if( dist_type == CV_DIST_L1 )
|
||||
dist_type = CV_DIST_L2;
|
||||
else if( mask_size == CV_DIST_MASK_3 )
|
||||
mask_size = CV_DIST_MASK_5;
|
||||
else if( mask_size == CV_DIST_MASK_5 )
|
||||
mask_size = CV_DIST_MASK_PRECISE;
|
||||
else if( mask_size == CV_DIST_MASK_PRECISE )
|
||||
build_voronoi = 1;
|
||||
}
|
||||
}
|
||||
|
||||
cvReleaseImage( &gray );
|
||||
cvReleaseImage( &edge );
|
||||
cvReleaseImage( &dist );
|
||||
cvReleaseImage( &dist8u );
|
||||
cvReleaseImage( &dist8u1 );
|
||||
cvReleaseImage( &dist8u2 );
|
||||
cvReleaseImage( &dist32s );
|
||||
cvReleaseImage( &labels );
|
||||
|
||||
cvDestroyWindow( wndname );
|
||||
|
||||
return 0;
|
||||
}
|
@@ -1,177 +0,0 @@
|
||||
#include <opencv2/core/core.hpp>
|
||||
#include <opencv2/highgui/highgui.hpp>
|
||||
|
||||
#define NUMBER 100
|
||||
#define DELAY 5
|
||||
char wndname[] = "Drawing Demo";
|
||||
|
||||
CvScalar random_color(CvRNG* rng)
|
||||
{
|
||||
int icolor = cvRandInt(rng);
|
||||
return CV_RGB(icolor&255, (icolor>>8)&255, (icolor>>16)&255);
|
||||
}
|
||||
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
int line_type = CV_AA; // change it to 8 to see non-antialiased graphics
|
||||
int i;
|
||||
CvPoint pt1,pt2;
|
||||
double angle;
|
||||
CvSize sz;
|
||||
CvPoint ptt[6];
|
||||
CvPoint* pt[2];
|
||||
int arr[2];
|
||||
CvFont font;
|
||||
CvRNG rng;
|
||||
int width = 1000, height = 700;
|
||||
int width3 = width*3, height3 = height*3;
|
||||
CvSize text_size;
|
||||
int ymin = 0;
|
||||
// Load the source image
|
||||
IplImage* image = cvCreateImage( cvSize(width,height), 8, 3 );
|
||||
IplImage* image2;
|
||||
|
||||
// Create a window
|
||||
cvNamedWindow(wndname, 1 );
|
||||
cvZero( image );
|
||||
cvShowImage(wndname,image);
|
||||
cvWaitKey(DELAY);
|
||||
|
||||
rng = cvRNG((unsigned)-1);
|
||||
pt[0] = &(ptt[0]);
|
||||
pt[1] = &(ptt[3]);
|
||||
|
||||
arr[0] = 3;
|
||||
arr[1] = 3;
|
||||
|
||||
for (i = 0; i< NUMBER; i++)
|
||||
{
|
||||
pt1.x=cvRandInt(&rng) % width3 - width;
|
||||
pt1.y=cvRandInt(&rng) % height3 - height;
|
||||
pt2.x=cvRandInt(&rng) % width3 - width;
|
||||
pt2.y=cvRandInt(&rng) % height3 - height;
|
||||
|
||||
cvLine( image, pt1, pt2, random_color(&rng), cvRandInt(&rng)%10, line_type, 0 );
|
||||
cvShowImage(wndname,image);
|
||||
if(cvWaitKey(DELAY) >= 0) return 0;
|
||||
}
|
||||
|
||||
for (i = 0; i< NUMBER; i++)
|
||||
{
|
||||
pt1.x=cvRandInt(&rng) % width3 - width;
|
||||
pt1.y=cvRandInt(&rng) % height3 - height;
|
||||
pt2.x=cvRandInt(&rng) % width3 - width;
|
||||
pt2.y=cvRandInt(&rng) % height3 - height;
|
||||
|
||||
cvRectangle( image,pt1, pt2, random_color(&rng), cvRandInt(&rng)%10-1, line_type, 0 );
|
||||
cvShowImage(wndname,image);
|
||||
if(cvWaitKey(DELAY) >= 0) return 0;
|
||||
}
|
||||
|
||||
for (i = 0; i< NUMBER; i++)
|
||||
{
|
||||
pt1.x=cvRandInt(&rng) % width3 - width;
|
||||
pt1.y=cvRandInt(&rng) % height3 - height;
|
||||
sz.width =cvRandInt(&rng)%200;
|
||||
sz.height=cvRandInt(&rng)%200;
|
||||
angle = (cvRandInt(&rng)%1000)*0.180;
|
||||
|
||||
cvEllipse( image, pt1, sz, angle, angle - 100, angle + 200,
|
||||
random_color(&rng), cvRandInt(&rng)%10-1, line_type, 0 );
|
||||
cvShowImage(wndname,image);
|
||||
if(cvWaitKey(DELAY) >= 0) return 0;
|
||||
}
|
||||
|
||||
for (i = 0; i< NUMBER; i++)
|
||||
{
|
||||
pt[0][0].x=cvRandInt(&rng) % width3 - width;
|
||||
pt[0][0].y=cvRandInt(&rng) % height3 - height;
|
||||
pt[0][1].x=cvRandInt(&rng) % width3 - width;
|
||||
pt[0][1].y=cvRandInt(&rng) % height3 - height;
|
||||
pt[0][2].x=cvRandInt(&rng) % width3 - width;
|
||||
pt[0][2].y=cvRandInt(&rng) % height3 - height;
|
||||
pt[1][0].x=cvRandInt(&rng) % width3 - width;
|
||||
pt[1][0].y=cvRandInt(&rng) % height3 - height;
|
||||
pt[1][1].x=cvRandInt(&rng) % width3 - width;
|
||||
pt[1][1].y=cvRandInt(&rng) % height3 - height;
|
||||
pt[1][2].x=cvRandInt(&rng) % width3 - width;
|
||||
pt[1][2].y=cvRandInt(&rng) % height3 - height;
|
||||
|
||||
cvPolyLine( image, pt, arr, 2, 1, random_color(&rng), cvRandInt(&rng)%10, line_type, 0 );
|
||||
cvShowImage(wndname,image);
|
||||
if(cvWaitKey(DELAY) >= 0) return 0;
|
||||
}
|
||||
|
||||
for (i = 0; i< NUMBER; i++)
|
||||
{
|
||||
pt[0][0].x=cvRandInt(&rng) % width3 - width;
|
||||
pt[0][0].y=cvRandInt(&rng) % height3 - height;
|
||||
pt[0][1].x=cvRandInt(&rng) % width3 - width;
|
||||
pt[0][1].y=cvRandInt(&rng) % height3 - height;
|
||||
pt[0][2].x=cvRandInt(&rng) % width3 - width;
|
||||
pt[0][2].y=cvRandInt(&rng) % height3 - height;
|
||||
pt[1][0].x=cvRandInt(&rng) % width3 - width;
|
||||
pt[1][0].y=cvRandInt(&rng) % height3 - height;
|
||||
pt[1][1].x=cvRandInt(&rng) % width3 - width;
|
||||
pt[1][1].y=cvRandInt(&rng) % height3 - height;
|
||||
pt[1][2].x=cvRandInt(&rng) % width3 - width;
|
||||
pt[1][2].y=cvRandInt(&rng) % height3 - height;
|
||||
|
||||
cvFillPoly( image, pt, arr, 2, random_color(&rng), line_type, 0 );
|
||||
cvShowImage(wndname,image);
|
||||
if(cvWaitKey(DELAY) >= 0) return 0;
|
||||
}
|
||||
|
||||
for (i = 0; i< NUMBER; i++)
|
||||
{
|
||||
pt1.x=cvRandInt(&rng) % width3 - width;
|
||||
pt1.y=cvRandInt(&rng) % height3 - height;
|
||||
|
||||
cvCircle( image, pt1, cvRandInt(&rng)%300, random_color(&rng),
|
||||
cvRandInt(&rng)%10-1, line_type, 0 );
|
||||
cvShowImage(wndname,image);
|
||||
if(cvWaitKey(DELAY) >= 0) return 0;
|
||||
}
|
||||
|
||||
for (i = 1; i< NUMBER; i++)
|
||||
{
|
||||
pt1.x=cvRandInt(&rng) % width3 - width;
|
||||
pt1.y=cvRandInt(&rng) % height3 - height;
|
||||
|
||||
cvInitFont( &font, cvRandInt(&rng) % 8,
|
||||
(cvRandInt(&rng)%100)*0.05+0.1, (cvRandInt(&rng)%100)*0.05+0.1,
|
||||
(cvRandInt(&rng)%5)*0.1, cvRound(cvRandInt(&rng)%10), line_type );
|
||||
|
||||
cvPutText( image, "Testing text rendering!", pt1, &font, random_color(&rng));
|
||||
cvShowImage(wndname,image);
|
||||
if(cvWaitKey(DELAY) >= 0) return 0;
|
||||
}
|
||||
|
||||
cvInitFont( &font, CV_FONT_HERSHEY_COMPLEX, 3, 3, 0.0, 5, line_type );
|
||||
|
||||
cvGetTextSize( "OpenCV forever!", &font, &text_size, &ymin );
|
||||
|
||||
pt1.x = (width - text_size.width)/2;
|
||||
pt1.y = (height + text_size.height)/2;
|
||||
image2 = cvCloneImage(image);
|
||||
|
||||
for( i = 0; i < 255; i++ )
|
||||
{
|
||||
cvSubS( image2, cvScalarAll(i), image, 0 );
|
||||
cvPutText( image, "OpenCV forever!", pt1, &font, CV_RGB(255,i,i));
|
||||
cvShowImage(wndname,image);
|
||||
if(cvWaitKey(DELAY) >= 0) return 0;
|
||||
}
|
||||
|
||||
// Wait for a key stroke; the same function arranges events processing
|
||||
cvWaitKey(0);
|
||||
cvReleaseImage(&image);
|
||||
cvReleaseImage(&image2);
|
||||
cvDestroyWindow(wndname);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef _EiC
|
||||
main(1,"drawing.c");
|
||||
#endif
|
@@ -1,62 +0,0 @@
|
||||
#include <opencv2/imgproc/imgproc_c.h>
|
||||
#include <opencv2/highgui/highgui.hpp>
|
||||
|
||||
char wndname[] = "Edge";
|
||||
char tbarname[] = "Threshold";
|
||||
int edge_thresh = 1;
|
||||
|
||||
IplImage *image = 0, *cedge = 0, *gray = 0, *edge = 0;
|
||||
|
||||
// define a trackbar callback
|
||||
void on_trackbar(int h)
|
||||
{
|
||||
cvSmooth( gray, edge, CV_BLUR, 3, 3, 0, 0 );
|
||||
cvNot( gray, edge );
|
||||
|
||||
// Run the edge detector on grayscale
|
||||
cvCanny(gray, edge, (float)edge_thresh, (float)edge_thresh*3, 3);
|
||||
|
||||
cvZero( cedge );
|
||||
// copy edge points
|
||||
cvCopy( image, cedge, edge );
|
||||
|
||||
cvShowImage(wndname, cedge);
|
||||
}
|
||||
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
char* filename = argc == 2 ? argv[1] : (char*)"fruits.jpg";
|
||||
|
||||
if( (image = cvLoadImage( filename, 1)) == 0 )
|
||||
return -1;
|
||||
|
||||
// Create the output image
|
||||
cedge = cvCreateImage(cvSize(image->width,image->height), IPL_DEPTH_8U, 3);
|
||||
|
||||
// Convert to grayscale
|
||||
gray = cvCreateImage(cvSize(image->width,image->height), IPL_DEPTH_8U, 1);
|
||||
edge = cvCreateImage(cvSize(image->width,image->height), IPL_DEPTH_8U, 1);
|
||||
cvCvtColor(image, gray, CV_BGR2GRAY);
|
||||
|
||||
// Create a window
|
||||
cvNamedWindow(wndname, 1);
|
||||
|
||||
// create a toolbar
|
||||
cvCreateTrackbar(tbarname, wndname, &edge_thresh, 100, on_trackbar);
|
||||
|
||||
// Show the image
|
||||
on_trackbar(0);
|
||||
|
||||
// Wait for a key stroke; the same function arranges events processing
|
||||
cvWaitKey(0);
|
||||
cvReleaseImage(&image);
|
||||
cvReleaseImage(&gray);
|
||||
cvReleaseImage(&edge);
|
||||
cvDestroyWindow(wndname);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef _EiC
|
||||
main(1,"edge.c");
|
||||
#endif
|
@@ -1,47 +0,0 @@
|
||||
#include <opencv2/video/tracking.hpp>
|
||||
#include <opencv2/imgproc/imgproc.hpp>
|
||||
#include <opencv2/highgui/highgui.hpp>
|
||||
|
||||
using namespace cv;
|
||||
|
||||
void drawOptFlowMap(const Mat& flow, Mat& cflowmap, int step,
|
||||
double, const Scalar& color)
|
||||
{
|
||||
for(int y = 0; y < cflowmap.rows; y += step)
|
||||
for(int x = 0; x < cflowmap.cols; x += step)
|
||||
{
|
||||
const Point2f& fxy = flow.at<Point2f>(y, x);
|
||||
line(cflowmap, Point(x,y), Point(cvRound(x+fxy.x), cvRound(y+fxy.y)),
|
||||
color);
|
||||
circle(cflowmap, Point(x,y), 2, color, -1);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int, char**)
|
||||
{
|
||||
VideoCapture cap(0);
|
||||
|
||||
if( !cap.isOpened() )
|
||||
return -1;
|
||||
|
||||
Mat prevgray, gray, flow, cflow, frame;
|
||||
namedWindow("flow", 1);
|
||||
|
||||
for(;;)
|
||||
{
|
||||
cap >> frame;
|
||||
cvtColor(frame, gray, CV_BGR2GRAY);
|
||||
|
||||
if( prevgray.data )
|
||||
{
|
||||
calcOpticalFlowFarneback(prevgray, gray, flow, 0.5, 3, 15, 3, 5, 1.2, 0);
|
||||
cvtColor(prevgray, cflow, CV_GRAY2BGR);
|
||||
drawOptFlowMap(flow, cflow, 16, 1.5, CV_RGB(0, 255, 0));
|
||||
imshow("flow", cflow);
|
||||
}
|
||||
if(waitKey(30)>=0)
|
||||
break;
|
||||
std::swap(prevgray, gray);
|
||||
}
|
||||
return 0;
|
||||
}
|
@@ -1,177 +0,0 @@
|
||||
#include <opencv2/imgproc/imgproc_c.h>
|
||||
#include <opencv2/highgui/highgui.hpp>
|
||||
|
||||
IplImage* color_img0;
|
||||
IplImage* mask;
|
||||
IplImage* color_img;
|
||||
IplImage* gray_img0 = NULL;
|
||||
IplImage* gray_img = NULL;
|
||||
int ffill_case = 1;
|
||||
int lo_diff = 20, up_diff = 20;
|
||||
int connectivity = 4;
|
||||
int is_color = 1;
|
||||
int is_mask = 0;
|
||||
int new_mask_val = 255;
|
||||
|
||||
void on_mouse( int event, int x, int y, int flags, void* param )
|
||||
{
|
||||
if( !color_img )
|
||||
return;
|
||||
|
||||
switch( event )
|
||||
{
|
||||
case CV_EVENT_LBUTTONDOWN:
|
||||
{
|
||||
CvPoint seed = cvPoint(x,y);
|
||||
int lo = ffill_case == 0 ? 0 : lo_diff;
|
||||
int up = ffill_case == 0 ? 0 : up_diff;
|
||||
int flags = connectivity + (new_mask_val << 8) +
|
||||
(ffill_case == 1 ? CV_FLOODFILL_FIXED_RANGE : 0);
|
||||
int b = rand() & 255, g = rand() & 255, r = rand() & 255;
|
||||
CvConnectedComp comp;
|
||||
|
||||
if( is_mask )
|
||||
cvThreshold( mask, mask, 1, 128, CV_THRESH_BINARY );
|
||||
|
||||
if( is_color )
|
||||
{
|
||||
CvScalar color = CV_RGB( r, g, b );
|
||||
cvFloodFill( color_img, seed, color, CV_RGB( lo, lo, lo ),
|
||||
CV_RGB( up, up, up ), &comp, flags, is_mask ? mask : NULL );
|
||||
cvShowImage( "image", color_img );
|
||||
}
|
||||
else
|
||||
{
|
||||
CvScalar brightness = cvRealScalar((r*2 + g*7 + b + 5)/10);
|
||||
cvFloodFill( gray_img, seed, brightness, cvRealScalar(lo),
|
||||
cvRealScalar(up), &comp, flags, is_mask ? mask : NULL );
|
||||
cvShowImage( "image", gray_img );
|
||||
}
|
||||
|
||||
printf("%g pixels were repainted\n", comp.area );
|
||||
|
||||
if( is_mask )
|
||||
cvShowImage( "mask", mask );
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
char* filename = argc >= 2 ? argv[1] : (char*)"fruits.jpg";
|
||||
|
||||
if( (color_img0 = cvLoadImage(filename,1)) == 0 )
|
||||
return 0;
|
||||
|
||||
printf( "Hot keys: \n"
|
||||
"\tESC - quit the program\n"
|
||||
"\tc - switch color/grayscale mode\n"
|
||||
"\tm - switch mask mode\n"
|
||||
"\tr - restore the original image\n"
|
||||
"\ts - use null-range floodfill\n"
|
||||
"\tf - use gradient floodfill with fixed(absolute) range\n"
|
||||
"\tg - use gradient floodfill with floating(relative) range\n"
|
||||
"\t4 - use 4-connectivity mode\n"
|
||||
"\t8 - use 8-connectivity mode\n" );
|
||||
|
||||
color_img = cvCloneImage( color_img0 );
|
||||
gray_img0 = cvCreateImage( cvSize(color_img->width, color_img->height), 8, 1 );
|
||||
cvCvtColor( color_img, gray_img0, CV_BGR2GRAY );
|
||||
gray_img = cvCloneImage( gray_img0 );
|
||||
mask = cvCreateImage( cvSize(color_img->width + 2, color_img->height + 2), 8, 1 );
|
||||
|
||||
cvNamedWindow( "image", 0 );
|
||||
cvCreateTrackbar( "lo_diff", "image", &lo_diff, 255, NULL );
|
||||
cvCreateTrackbar( "up_diff", "image", &up_diff, 255, NULL );
|
||||
|
||||
cvSetMouseCallback( "image", on_mouse, 0 );
|
||||
|
||||
for(;;)
|
||||
{
|
||||
int c;
|
||||
|
||||
if( is_color )
|
||||
cvShowImage( "image", color_img );
|
||||
else
|
||||
cvShowImage( "image", gray_img );
|
||||
|
||||
c = cvWaitKey(0);
|
||||
switch( (char) c )
|
||||
{
|
||||
case '\x1b':
|
||||
printf("Exiting ...\n");
|
||||
goto exit_main;
|
||||
case 'c':
|
||||
if( is_color )
|
||||
{
|
||||
printf("Grayscale mode is set\n");
|
||||
cvCvtColor( color_img, gray_img, CV_BGR2GRAY );
|
||||
is_color = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Color mode is set\n");
|
||||
cvCopy( color_img0, color_img, NULL );
|
||||
cvZero( mask );
|
||||
is_color = 1;
|
||||
}
|
||||
break;
|
||||
case 'm':
|
||||
if( is_mask )
|
||||
{
|
||||
cvDestroyWindow( "mask" );
|
||||
is_mask = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
cvNamedWindow( "mask", 0 );
|
||||
cvZero( mask );
|
||||
cvShowImage( "mask", mask );
|
||||
is_mask = 1;
|
||||
}
|
||||
break;
|
||||
case 'r':
|
||||
printf("Original image is restored\n");
|
||||
cvCopy( color_img0, color_img, NULL );
|
||||
cvCopy( gray_img0, gray_img, NULL );
|
||||
cvZero( mask );
|
||||
break;
|
||||
case 's':
|
||||
printf("Simple floodfill mode is set\n");
|
||||
ffill_case = 0;
|
||||
break;
|
||||
case 'f':
|
||||
printf("Fixed Range floodfill mode is set\n");
|
||||
ffill_case = 1;
|
||||
break;
|
||||
case 'g':
|
||||
printf("Gradient (floating range) floodfill mode is set\n");
|
||||
ffill_case = 2;
|
||||
break;
|
||||
case '4':
|
||||
printf("4-connectivity mode is set\n");
|
||||
connectivity = 4;
|
||||
break;
|
||||
case '8':
|
||||
printf("8-connectivity mode is set\n");
|
||||
connectivity = 8;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
exit_main:
|
||||
|
||||
cvDestroyWindow( "test" );
|
||||
cvReleaseImage( &gray_img );
|
||||
cvReleaseImage( &gray_img0 );
|
||||
cvReleaseImage( &color_img );
|
||||
cvReleaseImage( &color_img0 );
|
||||
cvReleaseImage( &mask );
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
#ifdef _EiC
|
||||
main(1,"ffilldemo.c");
|
||||
#endif
|
@@ -14,113 +14,70 @@
|
||||
*
|
||||
*
|
||||
********************************************************************************/
|
||||
#include "opencv2/imgproc/imgproc_c.h"
|
||||
#include "opencv2/imgproc/imgproc.hpp"
|
||||
#include "opencv2/highgui/highgui.hpp"
|
||||
|
||||
int slider_pos = 70;
|
||||
#include <iostream>
|
||||
|
||||
// Load the source image. HighGUI use.
|
||||
IplImage *image02 = 0, *image03 = 0, *image04 = 0;
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
|
||||
void process_image(int h);
|
||||
int sliderPos = 70;
|
||||
|
||||
Mat image;
|
||||
|
||||
void processImage(int, void*);
|
||||
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
const char* filename = argc == 2 ? argv[1] : (char*)"stuff.jpg";
|
||||
image = imread(filename, 0);
|
||||
if( image.empty() )
|
||||
{
|
||||
cout << "Usage: fitellipse <image_name>\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
// load image and force it to be grayscale
|
||||
if( (image03 = cvLoadImage(filename, 0)) == 0 )
|
||||
return -1;
|
||||
|
||||
// Create the destination images
|
||||
image02 = cvCloneImage( image03 );
|
||||
image04 = cvCloneImage( image03 );
|
||||
|
||||
// Create windows.
|
||||
cvNamedWindow("Source", 1);
|
||||
cvNamedWindow("Result", 1);
|
||||
|
||||
// Show the image.
|
||||
cvShowImage("Source", image03);
|
||||
|
||||
imshow("source", image);
|
||||
namedWindow("result", 1);
|
||||
|
||||
// Create toolbars. HighGUI use.
|
||||
cvCreateTrackbar( "Threshold", "Result", &slider_pos, 255, process_image );
|
||||
|
||||
process_image(0);
|
||||
createTrackbar( "threshold", "result", &sliderPos, 255, processImage );
|
||||
processImage(0, 0);
|
||||
|
||||
// Wait for a key stroke; the same function arranges events processing
|
||||
cvWaitKey(0);
|
||||
cvReleaseImage(&image02);
|
||||
cvReleaseImage(&image03);
|
||||
|
||||
cvDestroyWindow("Source");
|
||||
cvDestroyWindow("Result");
|
||||
|
||||
waitKey();
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Define trackbar callback functon. This function find contours,
|
||||
// draw it and approximate it by ellipses.
|
||||
void process_image(int h)
|
||||
void processImage(int h, void*)
|
||||
{
|
||||
CvMemStorage* storage;
|
||||
CvSeq* contour;
|
||||
vector<vector<Point> > contours;
|
||||
Mat bimage = image >= sliderPos;
|
||||
|
||||
findContours(bimage, contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE);
|
||||
|
||||
// Create dynamic structure and sequence.
|
||||
storage = cvCreateMemStorage(0);
|
||||
contour = cvCreateSeq(CV_SEQ_ELTYPE_POINT, sizeof(CvSeq), sizeof(CvPoint) , storage);
|
||||
Mat cimage = Mat::zeros(bimage.size(), CV_8UC3);
|
||||
|
||||
// Threshold the source image. This needful for cvFindContours().
|
||||
cvThreshold( image03, image02, slider_pos, 255, CV_THRESH_BINARY );
|
||||
|
||||
// Find all contours.
|
||||
cvFindContours( image02, storage, &contour, sizeof(CvContour),
|
||||
CV_RETR_LIST, CV_CHAIN_APPROX_NONE, cvPoint(0,0));
|
||||
|
||||
// Clear images. IPL use.
|
||||
cvZero(image02);
|
||||
cvZero(image04);
|
||||
|
||||
// This cycle draw all contours and approximate it by ellipses.
|
||||
for(;contour;contour = contour->h_next)
|
||||
for(size_t i = 0; i < contours.size(); i++)
|
||||
{
|
||||
int count = contour->total; // This is number point in contour
|
||||
CvPoint center;
|
||||
CvSize size;
|
||||
CvBox2D box;
|
||||
|
||||
// Number point must be more than or equal to 6 (for cvFitEllipse_32f).
|
||||
size_t count = contours[i].size();
|
||||
if( count < 6 )
|
||||
continue;
|
||||
|
||||
Mat pointsf;
|
||||
Mat(contours[i]).convertTo(pointsf, CV_32F);
|
||||
RotatedRect box = fitEllipse(pointsf);
|
||||
|
||||
box.angle = -box.angle;
|
||||
if( MAX(box.size.width, box.size.height) > MIN(box.size.width, box.size.height)*30 )
|
||||
continue;
|
||||
drawContours(cimage, contours, (int)i, Scalar::all(255), 1, 8);
|
||||
|
||||
CvMat* points_f = cvCreateMat( 1, count, CV_32FC2 );
|
||||
CvMat points_i = cvMat( 1, count, CV_32SC2, points_f->data.ptr );
|
||||
cvCvtSeqToArray( contour, points_f->data.ptr, CV_WHOLE_SEQ );
|
||||
cvConvert( &points_i, points_f );
|
||||
|
||||
// Fits ellipse to current contour.
|
||||
box = cvFitEllipse2( points_f );
|
||||
|
||||
// Draw current contour.
|
||||
cvDrawContours(image04,contour,CV_RGB(255,255,255),CV_RGB(255,255,255),0,1,8,cvPoint(0,0));
|
||||
|
||||
// Convert ellipse data from float to integer representation.
|
||||
center = cvPointFrom32f(box.center);
|
||||
size.width = cvRound(box.size.width*0.5);
|
||||
size.height = cvRound(box.size.height*0.5);
|
||||
|
||||
// Draw ellipse.
|
||||
cvEllipse(image04, center, size,
|
||||
-box.angle, 0, 360,
|
||||
CV_RGB(0,0,255), 1, CV_AA, 0);
|
||||
|
||||
cvReleaseMat(&points_f);
|
||||
ellipse(cimage, box, Scalar(0,0,255), 1, CV_AA);
|
||||
}
|
||||
|
||||
// Show image. HighGUI use.
|
||||
cvShowImage( "Result", image04 );
|
||||
imshow("result", cimage);
|
||||
}
|
||||
|
||||
#ifdef _EiC
|
||||
main(1,"fitellipse.c");
|
||||
#endif
|
||||
|
@@ -1,318 +0,0 @@
|
||||
#include "opencv2/highgui/highgui.hpp"
|
||||
#include "opencv2/imgproc/imgproc.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
using namespace cv;
|
||||
|
||||
const Scalar RED = Scalar(0,0,255);
|
||||
const Scalar PINK = Scalar(230,130,255);
|
||||
const Scalar BLUE = Scalar(255,0,0);
|
||||
const Scalar LIGHTBLUE = Scalar(255,255,160);
|
||||
const Scalar GREEN = Scalar(0,255,0);
|
||||
|
||||
const int BGD_KEY = CV_EVENT_FLAG_CTRLKEY;
|
||||
const int FGD_KEY = CV_EVENT_FLAG_SHIFTKEY;
|
||||
|
||||
void getBinMask( const Mat& comMask, Mat& binMask )
|
||||
{
|
||||
if( comMask.empty() || comMask.type()!=CV_8UC1 )
|
||||
CV_Error( CV_StsBadArg, "comMask is empty or has incorrect type (not CV_8UC1)" );
|
||||
if( binMask.empty() || binMask.rows!=comMask.rows || binMask.cols!=comMask.cols )
|
||||
binMask.create( comMask.size(), CV_8UC1 );
|
||||
binMask = comMask & 1;
|
||||
}
|
||||
|
||||
class GCApplication
|
||||
{
|
||||
public:
|
||||
enum{ NOT_SET = 0, IN_PROCESS = 1, SET = 2 };
|
||||
static const int radius = 2;
|
||||
static const int thickness = -1;
|
||||
|
||||
void reset();
|
||||
void setImageAndWinName( const Mat& _image, const string& _winName );
|
||||
void showImage() const;
|
||||
void mouseClick( int event, int x, int y, int flags, void* param );
|
||||
int nextIter();
|
||||
int getIterCount() const { return iterCount; }
|
||||
private:
|
||||
void setRectInMask();
|
||||
void setLblsInMask( int flags, Point p, bool isPr );
|
||||
|
||||
const string* winName;
|
||||
const Mat* image;
|
||||
Mat mask;
|
||||
Mat bgdModel, fgdModel;
|
||||
|
||||
uchar rectState, lblsState, prLblsState;
|
||||
bool isInitialized;
|
||||
|
||||
Rect rect;
|
||||
vector<Point> fgdPxls, bgdPxls, prFgdPxls, prBgdPxls;
|
||||
int iterCount;
|
||||
};
|
||||
|
||||
void GCApplication::reset()
|
||||
{
|
||||
if( !mask.empty() )
|
||||
mask.setTo(Scalar::all(GC_BGD));
|
||||
bgdPxls.clear(); fgdPxls.clear();
|
||||
prBgdPxls.clear(); prFgdPxls.clear();
|
||||
|
||||
isInitialized = false;
|
||||
rectState = NOT_SET;
|
||||
lblsState = NOT_SET;
|
||||
prLblsState = NOT_SET;
|
||||
iterCount = 0;
|
||||
}
|
||||
|
||||
void GCApplication::setImageAndWinName( const Mat& _image, const string& _winName )
|
||||
{
|
||||
if( _image.empty() || _winName.empty() )
|
||||
return;
|
||||
image = &_image;
|
||||
winName = &_winName;
|
||||
mask.create( image->size(), CV_8UC1);
|
||||
reset();
|
||||
}
|
||||
|
||||
void GCApplication::showImage() const
|
||||
{
|
||||
if( image->empty() || winName->empty() )
|
||||
return;
|
||||
|
||||
Mat res;
|
||||
Mat binMask;
|
||||
if( !isInitialized )
|
||||
image->copyTo( res );
|
||||
else
|
||||
{
|
||||
getBinMask( mask, binMask );
|
||||
image->copyTo( res, binMask );
|
||||
}
|
||||
|
||||
vector<Point>::const_iterator it;
|
||||
for( it = bgdPxls.begin(); it != bgdPxls.end(); ++it )
|
||||
circle( res, *it, radius, BLUE, thickness );
|
||||
for( it = fgdPxls.begin(); it != fgdPxls.end(); ++it )
|
||||
circle( res, *it, radius, RED, thickness );
|
||||
for( it = prBgdPxls.begin(); it != prBgdPxls.end(); ++it )
|
||||
circle( res, *it, radius, LIGHTBLUE, thickness );
|
||||
for( it = prFgdPxls.begin(); it != prFgdPxls.end(); ++it )
|
||||
circle( res, *it, radius, PINK, thickness );
|
||||
|
||||
if( rectState == IN_PROCESS || rectState == SET )
|
||||
rectangle( res, Point( rect.x, rect.y ), Point(rect.x + rect.width, rect.y + rect.height ), GREEN, 2);
|
||||
|
||||
imshow( *winName, res );
|
||||
}
|
||||
|
||||
void GCApplication::setRectInMask()
|
||||
{
|
||||
assert( !mask.empty() );
|
||||
mask.setTo( GC_BGD );
|
||||
rect.x = max(0, rect.x);
|
||||
rect.y = max(0, rect.y);
|
||||
rect.width = min(rect.width, image->cols-rect.x);
|
||||
rect.height = min(rect.height, image->rows-rect.y);
|
||||
(mask(rect)).setTo( Scalar(GC_PR_FGD) );
|
||||
}
|
||||
|
||||
void GCApplication::setLblsInMask( int flags, Point p, bool isPr )
|
||||
{
|
||||
vector<Point> *bpxls, *fpxls;
|
||||
uchar bvalue, fvalue;
|
||||
if( !isPr )
|
||||
{
|
||||
bpxls = &bgdPxls;
|
||||
fpxls = &fgdPxls;
|
||||
bvalue = GC_BGD;
|
||||
fvalue = GC_FGD;
|
||||
}
|
||||
else
|
||||
{
|
||||
bpxls = &prBgdPxls;
|
||||
fpxls = &prFgdPxls;
|
||||
bvalue = GC_PR_BGD;
|
||||
fvalue = GC_PR_FGD;
|
||||
}
|
||||
if( flags & BGD_KEY )
|
||||
{
|
||||
bpxls->push_back(p);
|
||||
circle( mask, p, radius, bvalue, thickness );
|
||||
}
|
||||
if( flags & FGD_KEY )
|
||||
{
|
||||
fpxls->push_back(p);
|
||||
circle( mask, p, radius, fvalue, thickness );
|
||||
}
|
||||
}
|
||||
|
||||
void GCApplication::mouseClick( int event, int x, int y, int flags, void* )
|
||||
{
|
||||
// TODO add bad args check
|
||||
switch( event )
|
||||
{
|
||||
case CV_EVENT_LBUTTONDOWN: // set rect or GC_BGD(GC_FGD) labels
|
||||
{
|
||||
bool isb = (flags & BGD_KEY) != 0,
|
||||
isf = (flags & FGD_KEY) != 0;
|
||||
if( rectState == NOT_SET && !isb && !isf )
|
||||
{
|
||||
rectState = IN_PROCESS;
|
||||
rect = Rect( x, y, 1, 1 );
|
||||
}
|
||||
if ( (isb || isf) && rectState == SET )
|
||||
lblsState = IN_PROCESS;
|
||||
}
|
||||
break;
|
||||
case CV_EVENT_RBUTTONDOWN: // set GC_PR_BGD(GC_PR_FGD) labels
|
||||
{
|
||||
bool isb = (flags & BGD_KEY) != 0,
|
||||
isf = (flags & FGD_KEY) != 0;
|
||||
if ( (isb || isf) && rectState == SET )
|
||||
prLblsState = IN_PROCESS;
|
||||
}
|
||||
break;
|
||||
case CV_EVENT_LBUTTONUP:
|
||||
if( rectState == IN_PROCESS )
|
||||
{
|
||||
rect = Rect( Point(rect.x, rect.y), Point(x,y) );
|
||||
rectState = SET;
|
||||
setRectInMask();
|
||||
assert( bgdPxls.empty() && fgdPxls.empty() && prBgdPxls.empty() && prFgdPxls.empty() );
|
||||
showImage();
|
||||
}
|
||||
if( lblsState == IN_PROCESS )
|
||||
{
|
||||
setLblsInMask(flags, Point(x,y), false);
|
||||
lblsState = SET;
|
||||
showImage();
|
||||
}
|
||||
break;
|
||||
case CV_EVENT_RBUTTONUP:
|
||||
if( prLblsState == IN_PROCESS )
|
||||
{
|
||||
setLblsInMask(flags, Point(x,y), true);
|
||||
prLblsState = SET;
|
||||
showImage();
|
||||
}
|
||||
break;
|
||||
case CV_EVENT_MOUSEMOVE:
|
||||
if( rectState == IN_PROCESS )
|
||||
{
|
||||
rect = Rect( Point(rect.x, rect.y), Point(x,y) );
|
||||
assert( bgdPxls.empty() && fgdPxls.empty() && prBgdPxls.empty() && prFgdPxls.empty() );
|
||||
showImage();
|
||||
}
|
||||
else if( lblsState == IN_PROCESS )
|
||||
{
|
||||
setLblsInMask(flags, Point(x,y), false);
|
||||
showImage();
|
||||
}
|
||||
else if( prLblsState == IN_PROCESS )
|
||||
{
|
||||
setLblsInMask(flags, Point(x,y), true);
|
||||
showImage();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int GCApplication::nextIter()
|
||||
{
|
||||
if( isInitialized )
|
||||
grabCut( *image, mask, rect, bgdModel, fgdModel, 1 );
|
||||
else
|
||||
{
|
||||
if( rectState != SET )
|
||||
return iterCount;
|
||||
|
||||
if( lblsState == SET || prLblsState == SET )
|
||||
grabCut( *image, mask, rect, bgdModel, fgdModel, 1, GC_INIT_WITH_MASK );
|
||||
else
|
||||
grabCut( *image, mask, rect, bgdModel, fgdModel, 1, GC_INIT_WITH_RECT );
|
||||
|
||||
isInitialized = true;
|
||||
}
|
||||
iterCount++;
|
||||
|
||||
bgdPxls.clear(); fgdPxls.clear();
|
||||
prBgdPxls.clear(); prFgdPxls.clear();
|
||||
|
||||
return iterCount;
|
||||
}
|
||||
|
||||
GCApplication gcapp;
|
||||
|
||||
void on_mouse( int event, int x, int y, int flags, void* param )
|
||||
{
|
||||
gcapp.mouseClick( event, x, y, flags, param );
|
||||
}
|
||||
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
if( argc==1 )
|
||||
return 1;
|
||||
string filename = argv[1];
|
||||
if( filename.empty() )
|
||||
return 1;
|
||||
Mat image = imread( filename, 1 );
|
||||
if( image.empty() )
|
||||
return 1;
|
||||
|
||||
cout << "First, select the rectangular area\n" <<
|
||||
"Hot keys: \n"
|
||||
"\tESC - quit the program\n"
|
||||
"\tr - restore the original image\n"
|
||||
"\tn - next iteration\n"
|
||||
"\n"
|
||||
"\tleft mouse button - set rectangle\n"
|
||||
"\n"
|
||||
"\tCTRL+left mouse button - set GC_BGD pixels\n"
|
||||
"\tSHIFT+left mouse button - set CG_FGD pixels\n"
|
||||
"\n"
|
||||
"\tCTRL+right mouse button - set GC_PR_BGD pixels\n"
|
||||
"\tSHIFT+right mouse button - set CG_PR_FGD pixels\n";
|
||||
|
||||
const string winName = "image";
|
||||
cvNamedWindow( winName.c_str(), CV_WINDOW_AUTOSIZE );
|
||||
cvSetMouseCallback( winName.c_str(), on_mouse, 0 );
|
||||
|
||||
gcapp.setImageAndWinName( image, winName );
|
||||
gcapp.showImage();
|
||||
|
||||
for(;;)
|
||||
{
|
||||
int c = cvWaitKey(0);
|
||||
switch( (char) c )
|
||||
{
|
||||
case '\x1b':
|
||||
cout << "Exiting ..." << endl;
|
||||
goto exit_main;
|
||||
case 'r':
|
||||
cout << endl;
|
||||
gcapp.reset();
|
||||
gcapp.showImage();
|
||||
break;
|
||||
case 'n':
|
||||
int iterCount = gcapp.getIterCount();
|
||||
cout << "<" << iterCount << "... ";
|
||||
int newIterCount = gcapp.nextIter();
|
||||
if( newIterCount > iterCount )
|
||||
{
|
||||
gcapp.showImage();
|
||||
cout << iterCount << ">" << endl;
|
||||
}
|
||||
else
|
||||
cout << "rect must be determined>" << endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
exit_main:
|
||||
cvDestroyWindow( winName.c_str() );
|
||||
return 0;
|
||||
}
|
23
samples/c/intrinsics.yml
Normal file
23
samples/c/intrinsics.yml
Normal file
@@ -0,0 +1,23 @@
|
||||
%YAML:1.0
|
||||
M1: !!opencv-matrix
|
||||
rows: 3
|
||||
cols: 3
|
||||
dt: d
|
||||
data: [ 5.3480326845051309e+02, 0., 3.3568643204394891e+02, 0.,
|
||||
5.3480326845051309e+02, 2.4066183054066337e+02, 0., 0., 1. ]
|
||||
D1: !!opencv-matrix
|
||||
rows: 1
|
||||
cols: 5
|
||||
dt: d
|
||||
data: [ 2.9589439552724328e-01, -1.0354662043042675e+00, 0., 0., 0. ]
|
||||
M2: !!opencv-matrix
|
||||
rows: 3
|
||||
cols: 3
|
||||
dt: d
|
||||
data: [ 5.3480326845051309e+02, 0., 3.3455744527912015e+02, 0.,
|
||||
5.3480326845051309e+02, 2.4205324573376600e+02, 0., 0., 1. ]
|
||||
D2: !!opencv-matrix
|
||||
rows: 1
|
||||
cols: 5
|
||||
dt: d
|
||||
data: [ -1.6916358306948096e-01, -1.1214173641213163e-01, 0., 0., 0. ]
|
@@ -1,84 +0,0 @@
|
||||
#include "opencv2/highgui/highgui.hpp"
|
||||
#include "opencv2/core/core.hpp"
|
||||
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
#define MAX_CLUSTERS 5
|
||||
CvScalar color_tab[MAX_CLUSTERS];
|
||||
IplImage* img = cvCreateImage( cvSize( 500, 500 ), 8, 3 );
|
||||
CvRNG rng = cvRNG(-1);
|
||||
CvPoint ipt;
|
||||
|
||||
color_tab[0] = CV_RGB(255,0,0);
|
||||
color_tab[1] = CV_RGB(0,255,0);
|
||||
color_tab[2] = CV_RGB(100,100,255);
|
||||
color_tab[3] = CV_RGB(255,0,255);
|
||||
color_tab[4] = CV_RGB(255,255,0);
|
||||
|
||||
cvNamedWindow( "clusters", 1 );
|
||||
|
||||
for(;;)
|
||||
{
|
||||
char key;
|
||||
int k, cluster_count = cvRandInt(&rng)%MAX_CLUSTERS + 1;
|
||||
int i, sample_count = cvRandInt(&rng)%1000 + 1;
|
||||
CvMat* points = cvCreateMat( sample_count, 1, CV_32FC2 );
|
||||
CvMat* clusters = cvCreateMat( sample_count, 1, CV_32SC1 );
|
||||
cluster_count = MIN(cluster_count, sample_count);
|
||||
|
||||
/* generate random sample from multigaussian distribution */
|
||||
for( k = 0; k < cluster_count; k++ )
|
||||
{
|
||||
CvPoint center;
|
||||
CvMat point_chunk;
|
||||
center.x = cvRandInt(&rng)%img->width;
|
||||
center.y = cvRandInt(&rng)%img->height;
|
||||
cvGetRows( points, &point_chunk, k*sample_count/cluster_count,
|
||||
k == cluster_count - 1 ? sample_count :
|
||||
(k+1)*sample_count/cluster_count, 1 );
|
||||
|
||||
cvRandArr( &rng, &point_chunk, CV_RAND_NORMAL,
|
||||
cvScalar(center.x,center.y,0,0),
|
||||
cvScalar(img->width*0.1,img->height*0.1,0,0));
|
||||
}
|
||||
|
||||
/* shuffle samples */
|
||||
for( i = 0; i < sample_count/2; i++ )
|
||||
{
|
||||
CvPoint2D32f* pt1 = (CvPoint2D32f*)points->data.fl + cvRandInt(&rng)%sample_count;
|
||||
CvPoint2D32f* pt2 = (CvPoint2D32f*)points->data.fl + cvRandInt(&rng)%sample_count;
|
||||
CvPoint2D32f temp;
|
||||
CV_SWAP( *pt1, *pt2, temp );
|
||||
}
|
||||
|
||||
printf( "iterations=%d\n", cvKMeans2( points, cluster_count, clusters,
|
||||
cvTermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, 1.0 ),
|
||||
5, 0, 0, 0, 0 ));
|
||||
|
||||
cvZero( img );
|
||||
|
||||
for( i = 0; i < sample_count; i++ )
|
||||
{
|
||||
int cluster_idx = clusters->data.i[i];
|
||||
ipt.x = (int)points->data.fl[i*2];
|
||||
ipt.y = (int)points->data.fl[i*2+1];
|
||||
cvCircle( img, ipt, 2, color_tab[cluster_idx], CV_FILLED, CV_AA, 0 );
|
||||
}
|
||||
|
||||
cvReleaseMat( &points );
|
||||
cvReleaseMat( &clusters );
|
||||
|
||||
cvShowImage( "clusters", img );
|
||||
|
||||
key = (char) cvWaitKey(0);
|
||||
if( key == 27 || key == 'q' || key == 'Q' ) // 'ESC'
|
||||
break;
|
||||
}
|
||||
|
||||
cvDestroyWindow( "clusters" );
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef _EiC
|
||||
main(1,"kmeans.c");
|
||||
#endif
|
@@ -1,76 +0,0 @@
|
||||
#include "opencv2/highgui/highgui.hpp"
|
||||
#include "opencv2/imgproc/imgproc.hpp"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int sigma = 3;
|
||||
int smoothType = CV_GAUSSIAN;
|
||||
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
IplImage* laplace = 0;
|
||||
IplImage* colorlaplace = 0;
|
||||
IplImage* planes[3] = { 0, 0, 0 };
|
||||
CvCapture* capture = 0;
|
||||
|
||||
if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
|
||||
capture = cvCaptureFromCAM( argc == 2 ? argv[1][0] - '0' : 0 );
|
||||
else if( argc == 2 )
|
||||
capture = cvCaptureFromAVI( argv[1] );
|
||||
|
||||
if( !capture )
|
||||
{
|
||||
fprintf(stderr,"Could not initialize capturing...\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
cvNamedWindow( "Laplacian", 0 );
|
||||
cvCreateTrackbar( "Sigma", "Laplacian", &sigma, 15, 0 );
|
||||
|
||||
for(;;)
|
||||
{
|
||||
IplImage* frame = 0;
|
||||
int i, c, ksize;
|
||||
|
||||
frame = cvQueryFrame( capture );
|
||||
if( !frame )
|
||||
break;
|
||||
|
||||
if( !laplace )
|
||||
{
|
||||
for( i = 0; i < 3; i++ )
|
||||
planes[i] = cvCreateImage( cvGetSize(frame), 8, 1 );
|
||||
laplace = cvCreateImage( cvGetSize(frame), IPL_DEPTH_16S, 1 );
|
||||
colorlaplace = cvCreateImage( cvGetSize(frame), 8, 3 );
|
||||
}
|
||||
|
||||
ksize = (sigma*5)|1;
|
||||
cvSmooth( frame, colorlaplace, smoothType, ksize, ksize, sigma, sigma );
|
||||
cvSplit( colorlaplace, planes[0], planes[1], planes[2], 0 );
|
||||
for( i = 0; i < 3; i++ )
|
||||
{
|
||||
cvLaplace( planes[i], laplace, 5 );
|
||||
cvConvertScaleAbs( laplace, planes[i], (sigma+1)*0.25, 0 );
|
||||
}
|
||||
cvMerge( planes[0], planes[1], planes[2], 0, colorlaplace );
|
||||
colorlaplace->origin = frame->origin;
|
||||
|
||||
cvShowImage("Laplacian", colorlaplace );
|
||||
|
||||
c = cvWaitKey(30);
|
||||
if( c == ' ' )
|
||||
smoothType = smoothType == CV_GAUSSIAN ? CV_BLUR : smoothType == CV_BLUR ? CV_MEDIAN : CV_GAUSSIAN;
|
||||
if( c == 'q' || c == 'Q' || (c & 255) == 27 )
|
||||
break;
|
||||
}
|
||||
|
||||
cvReleaseCapture( &capture );
|
||||
cvDestroyWindow("Laplacian");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef _EiC
|
||||
main(1,"laplace.c");
|
||||
#endif
|
@@ -1,187 +0,0 @@
|
||||
#include "opencv2/video/tracking.hpp"
|
||||
#include "opencv2/highgui/highgui.hpp"
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
|
||||
IplImage *image = 0, *grey = 0, *prev_grey = 0, *pyramid = 0, *prev_pyramid = 0, *swap_temp;
|
||||
|
||||
int win_size = 10;
|
||||
const int MAX_COUNT = 500;
|
||||
CvPoint2D32f* points[2] = {0,0}, *swap_points;
|
||||
char* status = 0;
|
||||
int count = 0;
|
||||
int need_to_init = 0;
|
||||
int night_mode = 0;
|
||||
int flags = 0;
|
||||
int add_remove_pt = 0;
|
||||
CvPoint pt;
|
||||
|
||||
|
||||
void on_mouse( int event, int x, int y, int flags, void* param )
|
||||
{
|
||||
if( !image )
|
||||
return;
|
||||
|
||||
if( image->origin )
|
||||
y = image->height - y;
|
||||
|
||||
if( event == CV_EVENT_LBUTTONDOWN )
|
||||
{
|
||||
pt = cvPoint(x,y);
|
||||
add_remove_pt = 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
CvCapture* capture = 0;
|
||||
|
||||
if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
|
||||
capture = cvCaptureFromCAM( argc == 2 ? argv[1][0] - '0' : 0 );
|
||||
else if( argc == 2 )
|
||||
capture = cvCaptureFromAVI( argv[1] );
|
||||
|
||||
if( !capture )
|
||||
{
|
||||
fprintf(stderr,"Could not initialize capturing...\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* print a welcome message, and the OpenCV version */
|
||||
printf ("Welcome to lkdemo, using OpenCV version %s (%d.%d.%d)\n",
|
||||
CV_VERSION,
|
||||
CV_MAJOR_VERSION, CV_MINOR_VERSION, CV_SUBMINOR_VERSION);
|
||||
|
||||
printf( "Hot keys: \n"
|
||||
"\tESC - quit the program\n"
|
||||
"\tr - auto-initialize tracking\n"
|
||||
"\tc - delete all the points\n"
|
||||
"\tn - switch the \"night\" mode on/off\n"
|
||||
"To add/remove a feature point click it\n" );
|
||||
|
||||
cvNamedWindow( "LkDemo", 0 );
|
||||
cvSetMouseCallback( "LkDemo", on_mouse, 0 );
|
||||
|
||||
for(;;)
|
||||
{
|
||||
IplImage* frame = 0;
|
||||
int i, k, c;
|
||||
|
||||
frame = cvQueryFrame( capture );
|
||||
if( !frame )
|
||||
break;
|
||||
|
||||
if( !image )
|
||||
{
|
||||
/* allocate all the buffers */
|
||||
image = cvCreateImage( cvGetSize(frame), 8, 3 );
|
||||
image->origin = frame->origin;
|
||||
grey = cvCreateImage( cvGetSize(frame), 8, 1 );
|
||||
prev_grey = cvCreateImage( cvGetSize(frame), 8, 1 );
|
||||
pyramid = cvCreateImage( cvGetSize(frame), 8, 1 );
|
||||
prev_pyramid = cvCreateImage( cvGetSize(frame), 8, 1 );
|
||||
points[0] = (CvPoint2D32f*)cvAlloc(MAX_COUNT*sizeof(points[0][0]));
|
||||
points[1] = (CvPoint2D32f*)cvAlloc(MAX_COUNT*sizeof(points[0][0]));
|
||||
status = (char*)cvAlloc(MAX_COUNT);
|
||||
flags = 0;
|
||||
}
|
||||
|
||||
cvCopy( frame, image, 0 );
|
||||
cvCvtColor( image, grey, CV_BGR2GRAY );
|
||||
|
||||
if( night_mode )
|
||||
cvZero( image );
|
||||
|
||||
if( need_to_init )
|
||||
{
|
||||
/* automatic initialization */
|
||||
IplImage* eig = cvCreateImage( cvGetSize(grey), 32, 1 );
|
||||
IplImage* temp = cvCreateImage( cvGetSize(grey), 32, 1 );
|
||||
double quality = 0.01;
|
||||
double min_distance = 10;
|
||||
|
||||
count = MAX_COUNT;
|
||||
cvGoodFeaturesToTrack( grey, eig, temp, points[1], &count,
|
||||
quality, min_distance, 0, 3, 0, 0.04 );
|
||||
cvFindCornerSubPix( grey, points[1], count,
|
||||
cvSize(win_size,win_size), cvSize(-1,-1),
|
||||
cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,20,0.03));
|
||||
cvReleaseImage( &eig );
|
||||
cvReleaseImage( &temp );
|
||||
|
||||
add_remove_pt = 0;
|
||||
}
|
||||
else if( count > 0 )
|
||||
{
|
||||
cvCalcOpticalFlowPyrLK( prev_grey, grey, prev_pyramid, pyramid,
|
||||
points[0], points[1], count, cvSize(win_size,win_size), 3, status, 0,
|
||||
cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,20,0.03), flags );
|
||||
flags |= CV_LKFLOW_PYR_A_READY;
|
||||
for( i = k = 0; i < count; i++ )
|
||||
{
|
||||
if( add_remove_pt )
|
||||
{
|
||||
double dx = pt.x - points[1][i].x;
|
||||
double dy = pt.y - points[1][i].y;
|
||||
|
||||
if( dx*dx + dy*dy <= 25 )
|
||||
{
|
||||
add_remove_pt = 0;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if( !status[i] )
|
||||
continue;
|
||||
|
||||
points[1][k++] = points[1][i];
|
||||
cvCircle( image, cvPointFrom32f(points[1][i]), 3, CV_RGB(0,255,0), -1, 8,0);
|
||||
}
|
||||
count = k;
|
||||
}
|
||||
|
||||
if( add_remove_pt && count < MAX_COUNT )
|
||||
{
|
||||
points[1][count++] = cvPointTo32f(pt);
|
||||
cvFindCornerSubPix( grey, points[1] + count - 1, 1,
|
||||
cvSize(win_size,win_size), cvSize(-1,-1),
|
||||
cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,20,0.03));
|
||||
add_remove_pt = 0;
|
||||
}
|
||||
|
||||
CV_SWAP( prev_grey, grey, swap_temp );
|
||||
CV_SWAP( prev_pyramid, pyramid, swap_temp );
|
||||
CV_SWAP( points[0], points[1], swap_points );
|
||||
need_to_init = 0;
|
||||
cvShowImage( "LkDemo", image );
|
||||
|
||||
c = cvWaitKey(10);
|
||||
if( (char)c == 27 )
|
||||
break;
|
||||
switch( (char) c )
|
||||
{
|
||||
case 'r':
|
||||
need_to_init = 1;
|
||||
break;
|
||||
case 'c':
|
||||
count = 0;
|
||||
break;
|
||||
case 'n':
|
||||
night_mode ^= 1;
|
||||
break;
|
||||
default:
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
cvReleaseCapture( &capture );
|
||||
cvDestroyWindow("LkDemo");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef _EiC
|
||||
main(1,"lkdemo.c");
|
||||
#endif
|
@@ -1,100 +0,0 @@
|
||||
#include "opencv2/highgui/highgui.hpp"
|
||||
#include "opencv2/imgproc/imgproc_c.h"
|
||||
|
||||
#define ARRAY 1
|
||||
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
IplImage* img = cvCreateImage( cvSize( 500, 500 ), 8, 3 );
|
||||
#if !ARRAY
|
||||
CvMemStorage* storage = cvCreateMemStorage(0);
|
||||
#endif
|
||||
|
||||
cvNamedWindow( "rect & circle", 1 );
|
||||
|
||||
for(;;)
|
||||
{
|
||||
char key;
|
||||
int i, count = rand()%100 + 1;
|
||||
CvPoint pt0, pt;
|
||||
CvBox2D box;
|
||||
CvPoint2D32f box_vtx[4];
|
||||
CvPoint2D32f center;
|
||||
CvPoint icenter;
|
||||
float radius;
|
||||
#if !ARRAY
|
||||
CvSeq* ptseq = cvCreateSeq( CV_SEQ_KIND_GENERIC|CV_32SC2, sizeof(CvContour),
|
||||
sizeof(CvPoint), storage );
|
||||
for( i = 0; i < count; i++ )
|
||||
{
|
||||
pt0.x = rand() % (img->width/2) + img->width/4;
|
||||
pt0.y = rand() % (img->height/2) + img->height/4;
|
||||
cvSeqPush( ptseq, &pt0 );
|
||||
}
|
||||
#ifndef _EiC /* unfortunately, here EiC crashes */
|
||||
box = cvMinAreaRect2( ptseq, 0 );
|
||||
#endif
|
||||
cvMinEnclosingCircle( ptseq, ¢er, &radius );
|
||||
#else
|
||||
CvPoint* points = (CvPoint*)malloc( count * sizeof(points[0]));
|
||||
CvMat pointMat = cvMat( 1, count, CV_32SC2, points );
|
||||
|
||||
for( i = 0; i < count; i++ )
|
||||
{
|
||||
pt0.x = rand() % (img->width/2) + img->width/4;
|
||||
pt0.y = rand() % (img->height/2) + img->height/4;
|
||||
points[i] = pt0;
|
||||
}
|
||||
#ifndef _EiC
|
||||
box = cvMinAreaRect2( &pointMat, 0 );
|
||||
#endif
|
||||
cvMinEnclosingCircle( &pointMat, ¢er, &radius );
|
||||
#endif
|
||||
cvBoxPoints( box, box_vtx );
|
||||
cvZero( img );
|
||||
for( i = 0; i < count; i++ )
|
||||
{
|
||||
#if !ARRAY
|
||||
pt0 = *CV_GET_SEQ_ELEM( CvPoint, ptseq, i );
|
||||
#else
|
||||
pt0 = points[i];
|
||||
#endif
|
||||
cvCircle( img, pt0, 2, CV_RGB( 255, 0, 0 ), CV_FILLED, CV_AA, 0 );
|
||||
}
|
||||
|
||||
#ifndef _EiC
|
||||
pt0.x = cvRound(box_vtx[3].x);
|
||||
pt0.y = cvRound(box_vtx[3].y);
|
||||
for( i = 0; i < 4; i++ )
|
||||
{
|
||||
pt.x = cvRound(box_vtx[i].x);
|
||||
pt.y = cvRound(box_vtx[i].y);
|
||||
cvLine(img, pt0, pt, CV_RGB(0, 255, 0), 1, CV_AA, 0);
|
||||
pt0 = pt;
|
||||
}
|
||||
#endif
|
||||
icenter.x = cvRound(center.x);
|
||||
icenter.y = cvRound(center.y);
|
||||
cvCircle( img, icenter, cvRound(radius), CV_RGB(255, 255, 0), 1, CV_AA, 0 );
|
||||
|
||||
cvShowImage( "rect & circle", img );
|
||||
|
||||
key = (char) cvWaitKey(0);
|
||||
if( key == 27 || key == 'q' || key == 'Q' ) // 'ESC'
|
||||
break;
|
||||
|
||||
#if !ARRAY
|
||||
cvClearMemStorage( storage );
|
||||
#else
|
||||
free( points );
|
||||
#endif
|
||||
}
|
||||
|
||||
cvDestroyWindow( "rect & circle" );
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef _EiC
|
||||
main(1,"convexhull.c");
|
||||
#endif
|
||||
|
@@ -1,102 +0,0 @@
|
||||
#include "opencv2/imgproc/imgproc.hpp"
|
||||
#include "opencv2/objdetect/objdetect.hpp"
|
||||
#include "opencv2/highgui/highgui.hpp"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
using namespace cv;
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
Mat img;
|
||||
FILE* f = 0;
|
||||
char _filename[1024];
|
||||
|
||||
if( argc == 1 )
|
||||
{
|
||||
printf("Usage: peopledetect (<image_filename> | <image_list>.txt)\n");
|
||||
return 0;
|
||||
}
|
||||
img = imread(argv[1]);
|
||||
|
||||
if( img.data )
|
||||
{
|
||||
strcpy(_filename, argv[1]);
|
||||
}
|
||||
else
|
||||
{
|
||||
f = fopen(argv[1], "rt");
|
||||
if(!f)
|
||||
{
|
||||
fprintf( stderr, "ERROR: the specified file could not be loaded\n");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
HOGDescriptor hog;
|
||||
hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());
|
||||
namedWindow("people detector", 1);
|
||||
|
||||
for(;;)
|
||||
{
|
||||
char* filename = _filename;
|
||||
if(f)
|
||||
{
|
||||
if(!fgets(filename, (int)sizeof(_filename)-2, f))
|
||||
break;
|
||||
//while(*filename && isspace(*filename))
|
||||
// ++filename;
|
||||
if(filename[0] == '#')
|
||||
continue;
|
||||
int l = strlen(filename);
|
||||
while(l > 0 && isspace(filename[l-1]))
|
||||
--l;
|
||||
filename[l] = '\0';
|
||||
img = imread(filename);
|
||||
}
|
||||
printf("%s:\n", filename);
|
||||
if(!img.data)
|
||||
continue;
|
||||
|
||||
fflush(stdout);
|
||||
vector<Rect> found, found_filtered;
|
||||
double t = (double)getTickCount();
|
||||
// run the detector with default parameters. to get a higher hit-rate
|
||||
// (and more false alarms, respectively), decrease the hitThreshold and
|
||||
// groupThreshold (set groupThreshold to 0 to turn off the grouping completely).
|
||||
hog.detectMultiScale(img, found, 0, Size(8,8), Size(32,32), 1.05, 2);
|
||||
t = (double)getTickCount() - t;
|
||||
printf("tdetection time = %gms\n", t*1000./cv::getTickFrequency());
|
||||
size_t i, j;
|
||||
for( i = 0; i < found.size(); i++ )
|
||||
{
|
||||
Rect r = found[i];
|
||||
for( j = 0; j < found.size(); j++ )
|
||||
if( j != i && (r & found[j]) == r)
|
||||
break;
|
||||
if( j == found.size() )
|
||||
found_filtered.push_back(r);
|
||||
}
|
||||
for( i = 0; i < found_filtered.size(); i++ )
|
||||
{
|
||||
Rect r = found_filtered[i];
|
||||
// the HOG detector returns slightly larger rectangles than the real objects.
|
||||
// so we slightly shrink the rectangles to get a nicer output.
|
||||
r.x += cvRound(r.width*0.1);
|
||||
r.width = cvRound(r.width*0.8);
|
||||
r.y += cvRound(r.height*0.07);
|
||||
r.height = cvRound(r.height*0.8);
|
||||
rectangle(img, r.tl(), r.br(), cv::Scalar(0,255,0), 3);
|
||||
}
|
||||
imshow("people detector", img);
|
||||
int c = waitKey(0) & 255;
|
||||
if( c == 'q' || c == 'Q' || !f)
|
||||
break;
|
||||
}
|
||||
if(f)
|
||||
fclose(f);
|
||||
return 0;
|
||||
}
|
@@ -1,146 +0,0 @@
|
||||
#include "opencv2/imgproc/imgproc_c.h"
|
||||
#include "opencv2/highgui/highgui.hpp"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
IplImage* marker_mask = 0;
|
||||
IplImage* markers = 0;
|
||||
IplImage* img0 = 0, *img = 0, *img_gray = 0, *wshed = 0;
|
||||
CvPoint prev_pt = {-1,-1};
|
||||
|
||||
void on_mouse( int event, int x, int y, int flags, void* )
|
||||
{
|
||||
if( !img )
|
||||
return;
|
||||
|
||||
if( event == CV_EVENT_LBUTTONUP || !(flags & CV_EVENT_FLAG_LBUTTON) )
|
||||
prev_pt = cvPoint(-1,-1);
|
||||
else if( event == CV_EVENT_LBUTTONDOWN )
|
||||
prev_pt = cvPoint(x,y);
|
||||
else if( event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON) )
|
||||
{
|
||||
CvPoint pt = cvPoint(x,y);
|
||||
if( prev_pt.x < 0 )
|
||||
prev_pt = pt;
|
||||
cvLine( marker_mask, prev_pt, pt, cvScalarAll(255), 5, 8, 0 );
|
||||
cvLine( img, prev_pt, pt, cvScalarAll(255), 5, 8, 0 );
|
||||
prev_pt = pt;
|
||||
cvShowImage( "image", img );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
char* filename = argc >= 2 ? argv[1] : (char*)"fruits.jpg";
|
||||
CvMemStorage* storage = cvCreateMemStorage(0);
|
||||
CvRNG rng = cvRNG(-1);
|
||||
|
||||
if( (img0 = cvLoadImage(filename,1)) == 0 )
|
||||
return 0;
|
||||
|
||||
printf( "Hot keys: \n"
|
||||
"\tESC - quit the program\n"
|
||||
"\tr - restore the original image\n"
|
||||
"\tw or SPACE - run watershed algorithm\n"
|
||||
"\t\t(before running it, roughly mark the areas on the image)\n"
|
||||
"\t (before that, roughly outline several markers on the image)\n" );
|
||||
|
||||
cvNamedWindow( "image", 1 );
|
||||
cvNamedWindow( "watershed transform", 1 );
|
||||
|
||||
img = cvCloneImage( img0 );
|
||||
img_gray = cvCloneImage( img0 );
|
||||
wshed = cvCloneImage( img0 );
|
||||
marker_mask = cvCreateImage( cvGetSize(img), 8, 1 );
|
||||
markers = cvCreateImage( cvGetSize(img), IPL_DEPTH_32S, 1 );
|
||||
cvCvtColor( img, marker_mask, CV_BGR2GRAY );
|
||||
cvCvtColor( marker_mask, img_gray, CV_GRAY2BGR );
|
||||
|
||||
cvZero( marker_mask );
|
||||
cvZero( wshed );
|
||||
cvShowImage( "image", img );
|
||||
cvShowImage( "watershed transform", wshed );
|
||||
cvSetMouseCallback( "image", on_mouse, 0 );
|
||||
|
||||
for(;;)
|
||||
{
|
||||
int c = cvWaitKey(0);
|
||||
|
||||
if( (char)c == 27 )
|
||||
break;
|
||||
|
||||
if( (char)c == 'r' )
|
||||
{
|
||||
cvZero( marker_mask );
|
||||
cvCopy( img0, img );
|
||||
cvShowImage( "image", img );
|
||||
}
|
||||
|
||||
if( (char)c == 'w' || (char)c == ' ' )
|
||||
{
|
||||
CvSeq* contours = 0;
|
||||
CvMat* color_tab = 0;
|
||||
int i, j, comp_count = 0;
|
||||
|
||||
cvClearMemStorage(storage);
|
||||
|
||||
//cvSaveImage( "wshed_mask.png", marker_mask );
|
||||
//marker_mask = cvLoadImage( "wshed_mask.png", 0 );
|
||||
cvFindContours( marker_mask, storage, &contours, sizeof(CvContour),
|
||||
CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
|
||||
cvZero( markers );
|
||||
for( ; contours != 0; contours = contours->h_next, comp_count++ )
|
||||
{
|
||||
cvDrawContours( markers, contours, cvScalarAll(comp_count+1),
|
||||
cvScalarAll(comp_count+1), -1, -1, 8, cvPoint(0,0) );
|
||||
}
|
||||
|
||||
if( comp_count == 0 )
|
||||
continue;
|
||||
|
||||
color_tab = cvCreateMat( 1, comp_count, CV_8UC3 );
|
||||
for( i = 0; i < comp_count; i++ )
|
||||
{
|
||||
uchar* ptr = color_tab->data.ptr + i*3;
|
||||
ptr[0] = (uchar)(cvRandInt(&rng)%180 + 50);
|
||||
ptr[1] = (uchar)(cvRandInt(&rng)%180 + 50);
|
||||
ptr[2] = (uchar)(cvRandInt(&rng)%180 + 50);
|
||||
}
|
||||
|
||||
{
|
||||
double t = (double)cvGetTickCount();
|
||||
cvWatershed( img0, markers );
|
||||
t = (double)cvGetTickCount() - t;
|
||||
printf( "exec time = %gms\n", t/(cvGetTickFrequency()*1000.) );
|
||||
}
|
||||
|
||||
// paint the watershed image
|
||||
for( i = 0; i < markers->height; i++ )
|
||||
for( j = 0; j < markers->width; j++ )
|
||||
{
|
||||
int idx = CV_IMAGE_ELEM( markers, int, i, j );
|
||||
uchar* dst = &CV_IMAGE_ELEM( wshed, uchar, i, j*3 );
|
||||
if( idx == -1 )
|
||||
dst[0] = dst[1] = dst[2] = (uchar)255;
|
||||
else if( idx <= 0 || idx > comp_count )
|
||||
dst[0] = dst[1] = dst[2] = (uchar)0; // should not get here
|
||||
else
|
||||
{
|
||||
uchar* ptr = color_tab->data.ptr + (idx-1)*3;
|
||||
dst[0] = ptr[0]; dst[1] = ptr[1]; dst[2] = ptr[2];
|
||||
}
|
||||
}
|
||||
|
||||
cvAddWeighted( wshed, 0.5, img_gray, 0.5, 0, wshed );
|
||||
cvShowImage( "watershed transform", wshed );
|
||||
cvReleaseMat( &color_tab );
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
#ifdef _EiC
|
||||
main(1,"watershed.cpp");
|
||||
#endif
|
Reference in New Issue
Block a user