1、opencv版本选择1.0。编译环境为vc6.0绿色版。
2、下载OpenCV安装程序(1.0版本)。假如要将opencv安装到C:\Program Files\OpenCV。在安装时选择”将?? \OpenCV\bin加入系统变量”。或安装完成后手动添加环境变量“C:\Program Files\OpenCV\bin”。安装完成后重启才能使得环境变量生效。
3、VC6.0配置
启动VC++6.0,菜单Tools->Options->Directories:先设置lib路径,选择Library files,在下方填入路径:????????? C:\Program Files\OpenCV\lib
然后选择include files,在下方填入路径:
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
4.每创建一个将要使用OpenCV的VC Project,都需要给它指定需要的lib。菜单:Project->Settings,然后将Setting for选为All Configurations,然后选择右边的link标签,在Object/library modules附加上:
cxcore.lib cv.lib ml.lib cvaux.lib highgui.lib cvcam.lib
如果不需要这么多lib,你可以只添加你需要的lib。
// PictureSelectToSave.cpp : Defines the entry point for the console application. // //#include "stdafx.h" //#include <iostream.h> #include <cv.h> #include <highgui.h> #include <math.h> #include <stdio.h> #include <cxcore.h> IplImage* img=0; CvRect selection; CvPoint origin; int select_object = 0; int track_object = 0; //////鼠标事件函数 void on_mouse( int event, int x, int y, int flags, void* param ) { if( !img ) return; if( img->origin ) y = img->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, img->width ); selection.height = MIN( selection.height, img->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; } if( track_object <0 ) { CvPoint point1 = { selection.x,selection.y}; CvPoint point2 = { selection.x+selection.width+1, selection.y+selection.height+1 }; //////画面上画框并显示 if(selection.width>0 && selection.height>0) cvRectangle(img,point1,point2,CV_RGB(0,255,0),1,8,0); cvShowImage( "PictureShow", img); /////获得框中图片区域 ///方法一:可获得与原图像大小一样的图片/// /* IplImage* img2 = cvCreateImage(cvGetSize(img), 8, 1 ); cvSet(img2,CV_RGB(0,0,0),NULL);//背景设为白色 cvRectangle(img2,point1,point2, CV_RGB(255,255,255),-1,8,0);//框区域设为黑色 cvAnd(img,img2,img2);//逻辑与运算 cvShowImage("output", img2); cvSaveImage(".\\output\\SelectedAera.jpg",img2); */ ///方法二:仅仅将获取图片保存为实际区域的大小/// IplImage* img2 = cvCreateImage(cvSize(selection.width+1,selection.height+1), 8,1 ); CvRect rect = cvRect(selection.x, selection.y, selection.width+1,selection.height+1); cvSetImageROI(img, rect); cvCopy(img,img2);//复制对象区域 cvShowImage("output", img2); cvResetImageROI(img); cvSaveImage(".\\a2.bmp",img2); } } int main() { img=cvLoadImage("C:\\a1.bmp",0 ); //cvCanny(img,img, 120, 120, 3); cvNamedWindow( "PictureShow", CV_WINDOW_AUTOSIZE ); cvNamedWindow( "output",1 ); //////鼠标事件回调函数 cvSetMouseCallback( "PictureShow",on_mouse,0); cvShowImage( "PictureShow", img); cvWaitKey(0); cvDestroyWindow( "PictureShow" );//销毁窗口 cvDestroyWindow( "output" ); cvReleaseImage( &img ); //释放图像 // cvReleaseImage( &img2 ); return 0; }