Ключевые точки StarDescriptor
Материал из CompVision
Детектор особых точек изображения. Функция: cvGetStarKeypoints.
Результат работы:
//--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop # define _STLP_NO_CSTD_FUNCTION_IMPORTS //#define SKIP_INCLUDES #define _FM_NO_REMAP #include "cv.h" #include "highgui.h" #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; CvCapture* capture = 0; // Описатель шрифта (см. дальше) CvFont font; IplImage *frame=0, *frame_copy = 0; IplImage* gray=0; void ProcessFrame( IplImage* image ); #define WIDTHBYTES(bits) ((((bits) + 31) / 32) * 4) //--------------------------------------------------------------------------- // Создание API шного битмапа из интеловского RGB изображения //--------------------------------------------------------------------------- HBITMAP CreateRGBBitmap(IplImage* _Grab) { char *App; LPBITMAPINFO lpbi = new BITMAPINFO; lpbi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); lpbi->bmiHeader.biWidth = _Grab->width; lpbi->bmiHeader.biHeight =_Grab->height; lpbi->bmiHeader.biPlanes = 1; lpbi->bmiHeader.biBitCount = 24; lpbi->bmiHeader.biCompression = BI_RGB; lpbi->bmiHeader.biSizeImage = WIDTHBYTES((DWORD)_Grab->width * 8) * _Grab->height; lpbi->bmiHeader.biXPelsPerMeter = 0; lpbi->bmiHeader.biYPelsPerMeter = 0; lpbi->bmiHeader.biClrUsed = 0; lpbi->bmiHeader.biClrImportant = 0; void* pBits; HBITMAP hBitmap = CreateDIBSection( NULL, lpbi, DIB_RGB_COLORS, (void **)&pBits, NULL, 0 ); delete lpbi; if ( hBitmap ) App=(char*)pBits; long int length=0; if(_Grab->nChannels==1) // Серое или бинарное { length = _Grab->width*(_Grab->height); for (int i=0;i<_Grab->height;i++) { for (int j=0;j<_Grab->width;j++) { App[_Grab->width*3*(_Grab->height-i-1)+j*3]=_Grab->imageData[_Grab->width*(i)+j]; App[_Grab->width*3*(_Grab->height-i-1)+j*3+1]=_Grab->imageData[_Grab->width*(i)+j]; App[_Grab->width*3*(_Grab->height-i-1)+j*3+2]=_Grab->imageData[_Grab->width*(i)+j]; } } } if(_Grab->nChannels==3) // Цветное { for (int i=0;i<_Grab->height;i++) { memcpy(App+_Grab->width*3*(_Grab->height-i-1),_Grab->imageData+_Grab->width*3*i,_Grab->width*3); // Копируем память } } return hBitmap; } //--------------------------------------------------------------------------- //--------------------------------------------------------------------------- // Функция вывода изображения на HANDLE оконного компонента //--------------------------------------------------------------------------- void APIDrawIpl(int x,int y,IplImage* _Grab,void *HANDLE) { HDC hMemDC,hDC; hDC=GetDC(HANDLE); hMemDC = CreateCompatibleDC(hDC); HBITMAP Bitmap=CreateRGBBitmap(_Grab); SelectObject(hMemDC,Bitmap); BitBlt(hDC,x,y,_Grab->width,_Grab->height,hMemDC,0,0,SRCCOPY); DeleteObject(Bitmap); DeleteDC(hMemDC); DeleteDC(hDC); } //--------------------------------------------------------------------------- //--------------------------------------------------------------------------- //--------------------------------------------------------------------------- //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { capture = cvCaptureFromCAM(0); Application->OnIdle = IdleLoop; // Поток обработки простоя } //--------------------------------------------------------------------------- void __fastcall TForm1::IdleLoop(TObject*, bool& done) { done = false;// Поток обработки простоя if( capture ) { if( !cvGrabFrame( capture )) goto skip; frame = cvRetrieveFrame( capture ); if( !frame ) goto skip; if( !frame_copy ) frame_copy = cvCreateImage( cvSize(frame->width,frame->height), IPL_DEPTH_8U, frame->nChannels ); if( frame->origin == IPL_ORIGIN_TL ) cvCopy( frame, frame_copy, 0 ); else cvFlip( frame, frame_copy, 0 ); if( !gray ) gray = cvCreateImage( cvSize(frame_copy->width,frame_copy->height), IPL_DEPTH_8U, 1); ProcessFrame( frame_copy ); } skip:; } //--------------------------------------------------------------------------- void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action) { cvReleaseImage( &frame_copy ); cvReleaseImage( &gray ); cvReleaseCapture( &capture ); } //--------------------------------------------------------------------------- void ProcessFrame( IplImage* Grab ) { CvSeq* keypoints = 0; CvMemStorage* storage = cvCreateMemStorage(0); cvCvtColor(Grab, gray, CV_BGR2GRAY); // Получаем серый цвет // Необходимо удалить белый бордюр cvRectangle(gray, cvPoint(0,0), cvPoint(gray->width-1,gray->height-1),CV_RGB(0,0,0)); keypoints = cvGetStarKeypoints( gray, storage, cvStarDetectorParams(45) ); for(int i = 0; i < (keypoints ? keypoints->total : 0); i++ ) { CvStarKeypoint kpt = *(CvStarKeypoint*)cvGetSeqElem(keypoints, i); int r = kpt.size/2; cvCircle( Grab, kpt.pt, r, CV_RGB(0,255,0)); cvLine( Grab, cvPoint(kpt.pt.x + r, kpt.pt.y + r), cvPoint(kpt.pt.x - r, kpt.pt.y - r), CV_RGB(0,255,0)); cvLine( Grab, cvPoint(kpt.pt.x - r, kpt.pt.y + r), cvPoint(kpt.pt.x + r, kpt.pt.y - r), CV_RGB(0,255,0)); } cvReleaseMemStorage(&storage); APIDrawIpl(10,10,Grab,Form1->Handle); // Рисуем результат }
