opencv+arduino实现物体点追踪效果-创新互联
本文所要实现的结果是:通过在摄像头中选择一个追踪点,通过pc控制摄像头的舵机,使这一点始终在图像的中心。
在扶余等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供网站制作、网站设计 网站设计制作定制设计,公司网站建设,企业网站建设,成都品牌网站建设,全网整合营销推广,成都外贸网站建设公司,扶余网站建设费用合理。要点:使用光流法在舵机旋转的同时进行追踪,若该点运动,则摄像头跟踪联动。
#include#include #include #include #include #include using namespace std; using namespace cv; #define WINDOW_NAME "【程序窗口】" void on_MouseHandle(int event, int x, int y, int flags, void* param); void DrawRectangle( cv::Mat& img, cv::Rect box ); void tracking(Mat &frame,vector temp); HANDLE hComm; LPCWSTR pStr=L"COM4"; char lpOutbuffer[100]; DWORD dwbyte=100; Mat srcImage,grayImage,tempImage1,tempImage,imageROI,grayprev; int g_maxCornerNumber = 1; double qualityLevel = 0.01; double minDistance = 10; int blockSize = 3; double k = 0.04; vector corners; vector pre_corners; vector counts; vector status; vector err; Rect g_rectangle; Rect g_temprectangle; bool g_bDrawingBox = false; int main( int argc, char** argv ) { Mat frame; Mat result; COMSTAT Comstat; DWORD dwError; BOOL bWritestat; hComm=CreateFile(pStr,GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING, 0,NULL); if (hComm == INVALID_HANDLE_VALUE) { cout<<"FLASE"; return -1; } else { cout<<"TURE"; } DCB dcb; GetCommState(hComm,&dcb); dcb.BaudRate=9600; dcb.ByteSize=8; dcb.Parity=NOPARITY; dcb.StopBits=TWOSTOPBITS; bool set=SetCommState(hComm,&dcb); bool sup=SetupComm(hComm,1024,1024); VideoCapture capture(0); namedWindow( WINDOW_NAME ); setMouseCallback(WINDOW_NAME,on_MouseHandle,(void*)&frame); while(1) { capture >> frame; if(!frame.empty()) { cvtColor(frame,grayImage,CV_RGB2GRAY); if( g_bDrawingBox ) rectangle(frame,g_rectangle.tl(),g_rectangle.br(),Scalar(255,255,255)); if (corners.size()!=0) { bool can=PurgeComm(hComm,PURGE_TXCLEAR); if (corners[0].x>(frame.cols/2+100)) { lpOutbuffer[0]='a'; bool ne=WriteFile(hComm,lpOutbuffer,dwbyte,&dwbyte,NULL); } else if (corners[0].x<(frame.cols/2-100)) { lpOutbuffer[0]='b'; bool ne=WriteFile(hComm,lpOutbuffer,dwbyte,&dwbyte,NULL); } tracking(frame,corners); rectangle(frame,Point(corners[0].x-10,corners[0].y-10),Point(corners[0].x+10,corners[0].y+10),Scalar(255,255,255)); } imshow( WINDOW_NAME, frame ); } else { printf(" --(!) No captured frame -- Break!"); break; } int c = waitKey(50); if( (char)c == 27 ) { break; } } return 0; } void on_MouseHandle(int event, int x, int y, int flags, void* param) { Mat& image = *(cv::Mat*) param; switch( event) { case EVENT_MOUSEMOVE: { if( g_bDrawingBox ) { g_rectangle.width = x-g_rectangle.x; g_rectangle.height = y-g_rectangle.y; } } break; case EVENT_LBUTTONDOWN: { g_bDrawingBox = true; g_rectangle =Rect( x, y, 0, 0 ); } break; case EVENT_LBUTTONUP: { g_bDrawingBox = false; if( g_rectangle.width < 0 ) { g_rectangle.x += g_rectangle.width; g_rectangle.width *= -1; } if( g_rectangle.height < 0 ) { g_rectangle.y += g_rectangle.height; g_rectangle.height *= -1; } imageROI=grayImage(g_rectangle); goodFeaturesToTrack( imageROI,corners,g_maxCornerNumber,qualityLevel,minDistance,Mat(),blockSize,false,k ); for (int i = 0; i < corners.size(); i++) { corners[i].x=corners[i].x+g_rectangle.x; corners[i].y=corners[i].y+g_rectangle.y; } } break; } } void tracking(Mat &frame,vector temp) { cvtColor(frame, tempImage1, COLOR_BGR2GRAY); if (grayprev.empty()) { tempImage1.copyTo(grayprev); } calcOpticalFlowPyrLK(grayprev, tempImage1, temp, pre_corners, status, err); for (size_t i=0; i 另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
分享标题:opencv+arduino实现物体点追踪效果-创新互联
本文地址:http://abwzjs.com/article/jdphs.html