Showing posts with label OpenCV. Show all posts
Showing posts with label OpenCV. Show all posts

Monday, May 13, 2013

new OpenCV book: OpenCV Computer Vision with Python

http://link.packtpub.com/wdleKL 

 There are several OpenCV C++ books in the market, this one is Python version. I am really glad that someone wrote a book describing how to implement computer vision algorithms using Python. One of the most difficult part in OpenCV is how to install the library. This book starts with highly detailed installation instructions for Linux, Windows and Mac OS. The author shows readers different I/O operations, such as reading/writing image, video files and accessing cameras. The book also discusses the much needed GUI utilities and APIs. One of the very useful section in the book is how to train haar cascade features in xml files.

Thursday, December 27, 2012

New OpenCV book: Mastering OpenCV with Practical Computer Vision Projects

Packt has recently published its first book on OpenCV titled “Mastering OpenCV with Practical Computer Vision Projects”. Written by Daniel LĂ©lis Baggio. This book contains examples with source codes

This is a good addition to OpenCV cook book. The book covers different ways you could use you OpenCV skills in developing real computer vision applications. Most projects in the book are very useful and traditional computer vision problems. The most interesting chapters for me is chapter 1 and 5. Chapter 1 shows how to use OpenCV on android platform. Chapter 5 shows the car license plate recognition application with SVM.

Mastering OpenCV with Practical Computer Vision Projects

Friday, July 08, 2011

New OpenCV book

Packt has recently published its first book on OpenCV titled “OpenCV 2 Computer Vision Application Programming Cookbook”. Written by Robert Laganiere this book contains examples with source codes which teaches you how to program computer vision applications in C++ using the different features of the OpenCV library. http://www.packtpub.com/opencv-2-computer-vision-application-programming-cookbook/book

All the codes in this book are using OpenCV C++ instead of C. It covers many topic of image processing.

Check the sample chapter

Monday, June 13, 2011

Setup OpenCV2 in Visual Studio 2010

in project "Property pages":
C/C++ – General – Additional Include Directories:
C:\OpenCV2.2\include;

Liner – General – Additional Library Directories:
C:\OpenCV2.2\lib;

Liner – General – Additional Dependencies:
opencv_core220d.lib;opencv_highgui220d.lib;
opencv_imgproc220d.lib;opencv_features2d220d.lib;
opencv_calib3d220d.lib;

In source code:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>



Wednesday, October 29, 2008

[OpenCV]: match template (cvMatchTemplate)

#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <stdio.h>
int main( int argc, char** argv )
{
IplImage *src, *templ,*ftmp[6]; //ftmp is what to display on
int i;

//Read in the template to be used for matching:
if((templ=cvLoadImage("template.jpg", 1))== 0) {
printf("Error on reading template %s\n","temp");
return(-1);
}

//Read in the source image to be searched:
if((src=cvLoadImage("source.jpg", 1))== 0) {
printf("Error on reading src image %s\n","source");
return(-1);
}

int patchx = templ->width;
int patchy = templ->height;
int iwidth = src->width - patchx + 1;
int iheight = src->height - patchy + 1;
for(i=0; i<6; ++i){
ftmp[i] = cvCreateImage( cvSize(iwidth,iheight),32,1);
}

//DO THE MATCHING OF THE TEMPLATE WITH THE IMAGE
for(i=0; i<6; ++i){
cvMatchTemplate( src, templ, ftmp[i], i);
cvNormalize(ftmp[i],ftmp[i],1,0,CV_MINMAX);
}
//DISPLAY
cvNamedWindow( "Template", 0 );
cvShowImage( "Template", templ );
cvNamedWindow( "Image", 0 );
cvShowImage( "Image", src );

cvNamedWindow( "SQDIFF", 0 );
cvShowImage( "SQDIFF", ftmp[0] );
cvNamedWindow( "SQDIFF_NORMED", 0 );
cvShowImage( "SQDIFF_NORMED", ftmp[1] );
cvNamedWindow( "CCORR", 0 );
cvShowImage( "CCORR", ftmp[2] );

cvNamedWindow( "CCORR_NORMED", 0 );
cvShowImage( "CCORR_NORMED", ftmp[3] );

cvNamedWindow( "CCOEFF", 0 );
cvShowImage( "CCOEFF", ftmp[4] );

cvNamedWindow( "CCOEFF_NORMED", 0 );
cvShowImage( "CCOEFF_NORMED", ftmp[5] );
cvWaitKey(0);
}

[OpenCV]: find circle with Hough transform

#include <cv.h>
#include <highgui.h>
#include <math.h>

int main(int argc, char** argv) {
IplImage* image = cvLoadImage(
"circle.jpg",
CV_LOAD_IMAGE_GRAYSCALE
);
IplImage* src = cvLoadImage("circle.jpg");
CvMemStorage* storage = cvCreateMemStorage(0);
cvSmooth(image, image, CV_GAUSSIAN, 5, 5 );
CvSeq* results = cvHoughCircles(
image,
storage,
CV_HOUGH_GRADIENT,
4,
image->width/10
);

for( int i = 0; i < results->total; i++ )
{
float* p = (float*) cvGetSeqElem( results, i );
CvPoint pt = cvPoint( cvRound( p[0] ), cvRound( p[1] ) );
cvCircle(
src,
pt,
cvRound( p[2] ),
CV_RGB(0xff,0,0)
);
}
cvNamedWindow( "HoughCircles", 1 );
cvShowImage( "HoughCircles", src);
cvWaitKey(0);
}


codes from 'learning opencv' book examples

[OpenCV]: use cvPyrSegmentation

#include <stdio.h>
#include <cv.h>
#include <highgui.h>
#include <stdio.h>

void f(
IplImage* src,
IplImage* dst
) {
CvMemStorage* storage = cvCreateMemStorage(0);
CvSeq* comp = NULL;

cvPyrSegmentation( src, dst, storage, &comp, 4, 200, 50 );
int n_comp = comp->total;

for( int i=0; i<n_comp; i++ ) {
CvConnectedComp* cc = (CvConnectedComp*) cvGetSeqElem( comp, i );
// do_something_with( cc );
}
cvReleaseMemStorage( &storage );
}

int main(int argc, char** argv)
{

// Create a named window with a the name of the file.
cvNamedWindow( argv[1], 1 );
// Load the image from the given file name.
IplImage* src = cvLoadImage( argv[1] );
if(!src) { printf("Couldn't seem to Open %s, sorry\n",argv[1]); return -1;}
IplImage* dst = cvCreateImage( cvGetSize(src), src->depth, src->nChannels);
f( src, dst);

// Show the image in the named window
cvShowImage( argv[1], dst );

// Idle until the user hits the "Esc" key.
while( 1 ) { if( cvWaitKey( 100 ) == 27 ) break; }

// Clean up and don’t be piggies
cvDestroyWindow( argv[1] );
cvReleaseImage( &src );
cvReleaseImage( &dst );
}


code from 'learning opencv' book

Thursday, December 27, 2007

OpenCV: capture the frame

To open the video from camera or from the avi file, you may use the following two functions:

CvCapture *cap = cvCaptureFromCAM(0);
CvCapture *cap = cvCaptureFromAVI("test.avi");

Use the following functions to capture one frame:

IplImage *img = 0;
if (!cvGrabFrame(cap))
{
printf("cannot grab\n");
exit(0);
}
img = cvRetrieveFrame(cap);

Remember: do NOT use cvReleaseImage to release img.
You may also use the followings to capture one frame:

IplImage *img = 0;
img = cvQueryFrame(cap);
if (!img)
{
printf("cannot grab\n");
exit(0);
}
Use 'cvReleaseCapture(&cap);' in the end.

Friday, December 21, 2007

OpenCV: use CamShift for tracking

Use the OpenCV function cvCamShift. The following source codes are 'copy/paste' from other website/newsgroup:

#include "cv.h"
#include "highgui.h"
#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 = 48;
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 )
{
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;
#ifdef _DEBUG
printf("\n # mouse region:");
printf("\n X = %d, Y = %d, Width = %d, Height = %d",
selection.x, selection.y, selection.width, selection.height);
#endif
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;

#ifdef _DEBUG
printf("\n # Convert HSV to RGB:");
printf("\n HUE = %f", hue);
printf("\n R = %d, G = %d, B = %d", rgb[0],rgb[1],rgb[2]);
#endif

return cvScalar(rgb[2], rgb[1], rgb[0],0);
}

int main( int argc, char** argv )
{
CvCapture* capture = 0;
IplImage* frame = 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( "CamShiftDemo", 1 );
cvSetMouseCallback( "CamShiftDemo", on_mouse, NULL );
cvCreateTrackbar( "Vmin", "CamShiftDemo", &vmin, 256, 0 );
cvCreateTrackbar( "Vmax", "CamShiftDemo", &vmax, 256, 0 );
cvCreateTrackbar( "Smin", "CamShiftDemo", &smin, 256, 0 );

for(;;)
{
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 ); // BGR to HSV

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 ); // binary 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 ); // remove ROI
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 );

// calling CAMSHIFT
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( c == 27 )
break; // exit from for-loop
switch( 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;
}

OpenCV: Build background with cvRunningAvg

Use running average to build background image and detect motions. This method is OK to show the demo but it's far away to be good. In the following codes, you may check the input avi file to make sure the codec is working with the opencv function

#include <stdio.h>
#include <cv.h>
#include <highgui.h>

int main( int argc, char** argv )
{
IplImage* pFrame = 0;
IplImage* pFrImg = 0;
IplImage* pFrImg2 = 0;
IplImage* pBkImg = 0;
IplImage* pBkImg2 = 0;

CvMat* pFrameMat = NULL;
CvMat* pFrMat = NULL;
CvMat* pBkMat = NULL;

CvCapture* pCapture = NULL;
int nFrmNum = 0;

cvNamedWindow("video", 1);
cvNamedWindow("background",1);
cvNamedWindow("foreground",1);
cvMoveWindow("video", 30, 0);
cvMoveWindow("background", 360, 0);
cvMoveWindow("foreground", 690, 0);

if( !(pCapture = cvCaptureFromFile("smith.avi")))
{
fprintf(stderr, "Can not open file %s\n", argv[1]);
return -2;
}

while(pFrame = cvQueryFrame( pCapture ))
{
nFrmNum++;
if(nFrmNum == 1)
{
pBkImg = cvCreateImage(cvSize(pFrame->width,
pFrame->height), IPL_DEPTH_8U,1);
pFrImg = cvCreateImage(cvSize(pFrame->width,
pFrame->height), IPL_DEPTH_8U,1);
pBkImg2 = cvClone(pBkImg);
pFrImg2 = cvClone(pFrImg);
pBkMat = cvCreateMat(pFrame->height, pFrame->width, CV_32FC1);
pFrMat = cvCreateMat(pFrame->height, pFrame->width, CV_32FC1);
pFrameMat = cvCreateMat(pFrame->height, pFrame->width, CV_32FC1);

cvCvtColor(pFrame, pBkImg, CV_BGR2GRAY);
cvCvtColor(pFrame, pFrImg, CV_BGR2GRAY);

cvConvert(pFrImg, pFrameMat);
cvConvert(pFrImg, pFrMat);
cvConvert(pFrImg, pBkMat);
}
else
{
cvCvtColor(pFrame, pFrImg, CV_BGR2GRAY);
cvConvert(pFrImg, pFrameMat);
cvAbsDiff(pFrameMat, pBkMat, pFrMat);
cvThreshold(pFrMat, pFrImg, 60, 255.0, CV_THRESH_BINARY);
cvRunningAvg(pFrameMat, pBkMat, 0.003, 0);
cvConvert(pBkMat, pBkImg);
cvShowImage("video", pFrame);
cvFlip(pBkImg, pBkImg2, 0);
cvShowImage("background", pBkImg2);
cvFlip(pFrImg, pFrImg2, 0);
cvShowImage("foreground", pFrImg2);
if( cvWaitKey(1) >= 0 )
break;
}
}

cvDestroyWindow("video");
cvDestroyWindow("background");
cvDestroyWindow("foreground");
cvReleaseImage(&pFrImg);
cvReleaseImage(&pFrImg2);
cvReleaseImage(&pBkImg);
cvReleaseImage(&pBkImg2);
cvReleaseMat(&pFrameMat);
cvReleaseMat(&pFrMat);
cvReleaseMat(&pBkMat);

return 0;
}

OpenCV: detect circles with Hough transform

#include <cv.h>
#include <highgui.h>
#include <math.h>

int main(int argc, char** argv)
{
IplImage* img = cvLoadImage("circle.jpg", 1);;
IplImage* gray = cvCreateImage(cvGetSize(img), 8, 1);
CvMemStorage* storage = cvCreateMemStorage(0);
cvCvtColor(img, gray, CV_BGR2GRAY);
cvSmooth(gray, gray, CV_GAUSSIAN, 9, 9);
CvSeq* circles = cvHoughCircles(gray, storage,
CV_HOUGH_GRADIENT, 2, gray->height/4, 200, 100);
int i;

for (i = 0; i < circles->total; i++)
{
float* p = (float*)cvGetSeqElem( circles, i );
cvCircle( img, cvPoint(cvRound(p[0]),cvRound(p[1])),
3, CV_RGB(0,255,0), -1, 8, 0 );
cvCircle( img, cvPoint(cvRound(p[0]),cvRound(p[1])),
cvRound(p[2]), CV_RGB(255,0,0), 3, 8, 0 );
}
cvNamedWindow( "circles", 1 );
cvShowImage( "circles", img );
cvWaitKey(0);

return 0;
}

OpenCV: image rotate and zoom (rotation, zoom in and zoom out)

#include "cv.h"
#include "highgui.h"
#include "math.h"
int main( int argc, char** argv )
{
IplImage* src = cvLoadImage("lena.jpg", 1);
IplImage* dst = cvCloneImage( src );

int delta = 1;
int angle = 0;
int opt = 1; // 1: rotate & zoom
// 0: rotate only
double factor;
cvNamedWindow("src", 1);
cvShowImage("src", src);

for(;;)
{
float m[6];
CvMat M = cvMat(2, 3, CV_32F, m);
int w = src->width;
int h = src->height;

if(opt)
factor = (cos(angle*CV_PI/180.) + 1.05) * 2;
else
factor = 1;
m[0] = (float)(factor*cos(-angle*2*CV_PI/180.));
m[1] = (float)(factor*sin(-angle*2*CV_PI/180.));
m[3] = -m[1];
m[4] = m[0];
m[2] = w*0.5f;
m[5] = h*0.5f;

cvGetQuadrangleSubPix( src, dst, &M);
cvNamedWindow("dst", 1);
cvShowImage("dst", dst);
if( cvWaitKey(1) == 27 )
break;
angle =(int)(angle + delta) % 360;
}
return 0;
}

Wednesday, December 19, 2007

OpenCV: access pixel values

We may use ((uchar *)(img->imageData + i*img->widthStep))[j*img->nChannels + 1] = 255; to access the pixel value. Using the following template may make it easier:
template <class T> class Image
{
private:
IplImage *imgp;
public:
Image(IplImage *img = 0) {imgp = img;}
~Image() {imgp = 0;}
void operator = (IplImage *img){imgp = img;}
inline T* operator[](const int rowIndx){
return ((T*)(imgp->imageData + rowIndx *imgp ->widthStep));}
};
typedef struct{
unsigned char b, g, r;
}RgbPixel;

typedef struct{
float b, g, r;
}RgbPixelFloat;

typedef Image<RgbPixel> RgbImage;
typedef Image<RgbPixelFloat> RgbImageFloat;
typedef Image<unsigned char> BwImage;
typedef Image<float> BwImageFloat;

To access a single channel image:
BwImage img(pImg);img[i][j] = 255;

The following codes show an example to access multi-channel image:

IplImage* pImg = cvLoadImage ("lena.jpg", 1); 
cvNamedWindow( "Image", 1 );

RgbImage img(pImg);
for (i = 0; i < 20; i++)
{
for (j = 20; j < 40; j++)
{
img[i][j].r = 10;
img[i][j].g = 10;
img[i][j].b = 10;
}
}
cvShowImage( "Image", pImg );
-online source.

Tuesday, November 06, 2007

OpenCV: print matrix

void PrintMat(CvMat *A)
{
int i, j;
for (i = 0; i < A->rows; i++)
{
printf("\n");
switch (CV_MAT_DEPTH(A->type))
{
case CV_32F:
case CV_64F:
for (j = 0; j < A->cols; j++)
printf ("%8.3f ", (float)cvGetReal2D(A, i, j));
break;
case CV_8U:
case CV_16U:
for(j = 0; j < A->cols; j++)
printf ("%6d",(int)cvGetReal2D(A, i, j));
break;
default:
break;
}
}
printf("\n");
}

Some matrix operations need to be updated.

Sunday, November 04, 2007

OpenCV: setup in Visual C++ 2005

In Visual C++ 2005 (without opening a project)
  • Tools -> Options
  • Projects and Solutions -> VC++ Directories
  • Show Directories for -> Library files
  • add C:\Program Files\OpenCV
  • Include files, add:
  • C:\Program Files\OpenCV\cxcore\include
    C:\Program Files\OpenCV\cv\include
    C:\Program Files\OpenCV\cvaux\include
    C:\Program Files\OpenCV\ml\include
    C:\Program Files\OpenCV\otherlibs\highgui
    C:\Program Files\OpenCV\otherlibs\cvcam\include
In an OpenCV project:
  • Project -> Properties -> Configuration Properties -> Linker -> Input -> addition dependencies
  • cxcore.lib cv.lib ml.lib cvaux.lib highgui.lib

OpenCV: use random colors for drawing

Function to create random colors:
CvScalar random_color(CvRNG* rng)
{
int color = cvRandInt(rng);
return CV_RGB(color&255, (color>>8)&255, (color>>16)&255);
}

The applications to use the random colors: line, rectangular, ellipse, polyline, and circle
CvRNG rng;
int line_type = CV_AA;
CvPoint pt1,pt2;
double angle;
CvSize sz;
int arr[2];
arr[0] = 5;
arr[1] =5;

cvLine( image, pt1, pt2, random_color(&rng),
cvRandInt(&rng)%10, line_type, 0 );

cvRectangle( image,pt1, pt2, random_color(&rng),
cvRandInt(&rng)%10-1, line_type, 0);

cvEllipse( image, pt1, sz, angle, angle - 100, angle + 200,
random_color(&rng), cvRandInt(&rng)%10-1, line_type, 0 );

cvPolyLine( image, pt, arr, 2, 1, random_color(&rng),
cvRandInt(&rng)%10, line_type, 0 );

cvFillPoly( image, pt, arr, 2, random_color(&rng), line_type, 0 );

cvCircle( image, pt1, cvRandInt(&rng)%300, random_color(&rng),
cvRandInt(&rng)%10-1, line_type, 0 );

OpenCV: DCT and IDCT

float data[] = { 1, 2, 3, 4, 5, 6, 7, 8 };

CvMat a, b, c;
a = cvMat(2,4,CV_32FC1,data);
b = cvMat(2,4,CV_32FC1,data); // or initialize b,c
c = cvMat(2,4,CV_32FC1,data);

cvDCT(&a, &b, CV_DXT_FORWARD);
cvDCT(&b, &c, CV_DXT_INVERSE);


original matrix:
1 2 3 4
5 6 7 8

after DCT:
12.728 -3.154 0.000 -0.224
-5.657 0.000 0.000 0.000

after IDCT:
1 2 3 4
5 6 7 8

Sunday, October 28, 2007

OpenCV: get a sub image

IplImage* Sub_Image(IplImage *image, CvRect roi)
{
IplImage *result;
// set ROI, you may use following two funs:
//cvSetImageROI( image, cvRect( 0, 0, image->width, image->height ));

cvSetImageROI(image,roi);
// sub-image
result = cvCreateImage( cvSize(roi.width, roi.height), image->depth, image->nChannels );
cvCopy(image,result);
cvResetImageROI(image); // release image ROI
return result;
}

Friday, October 26, 2007

mean shift

Get "Mass Center"
for(int i=0;i< height;i++)
for(int j=0;j width;j++)
M00+=I(i,j);

for(int i=0;i height;i++)
for(int j=0;j width;j++)
{
M10+=i*I(i,j);
M01+=j*I(i,j);
}


Mass Center is:
Xc=M10/M00;
Yc=M01/M00

Four steps for Mean Shift algorithm:
1.initialize the size and position of window.
2.calculate Mass Center of the window.
3.adjust Mass Center of the window.
4.repeat 2 and 3 till the center qualified.

OpenCV function: cvMeanShift
int cvMeanShift(IplImage* imgprob,CvRect windowIn,
CvTermCriteria criteria,CvConnectedComp* out);

Monday, August 14, 2006

Give OpenCV program to others

copy cv.lib, cvaux.lib, cxcore.lib, highgui.lib to the same directory and add them to "input-additional dependencies"

Also copy some head file in direcories: cv\include, cvaux\include cxcore\include, otherlibs\highgui to current directory