OpenCV2.4.9 + VS2012 + win10 配置
Step1
下载opencv 2.4.9 pack
Step2
解压到本地 我解压路径是: C:\OPENCV
Step3
添加环境变量:
这里虽然把X64下的VC11(VC11对应VS2012的C++编译器VC11)也添加了,但是我实际上只用到了x86;
Step4
VS新建空白控制台工程 。 项目上右键属性,给 Debug配置属性 (Release如果需要也要配置一遍)平台就选 Win32吧。Win32是编译器类型x86与本机的x64无关。
VC++目录下我们先配上 包含目录
然后在配上库目录:
C:\OPENCV\opencv\build\x86\vc11\lib
链接器 -> 输入 -> 附加依赖项 搞一下
opencv_core249d.lib
opencv_imgproc249d.lib
opencv_highgui249d.lib
opencv_ml249d.lib
opencv_video249d.lib
后面的d代表 debug用的lib 如果配置release那去掉‘d’ 奥~
完成!随便搞个例子试一下:使用 opencv实现的graphcut算法,就是抠图啦:
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp" #include <iostream> using namespace std;
using namespace cv; static void help()
{
cout << "\nThis program demonstrates GrabCut segmentation -- select an object in a region\n"
"and then grabcut will attempt to segment it out.\n"
"Call:\n"
"./grabcut <image_name>\n"
"\nSelect a rectangular area around the object you want to segment\n" <<
"\nHot keys: \n"
"\tESC - quit the program\n"
"\tr - restore the original image\n"
"\tn - next iteration\n"
"\ts - save image\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" << endl;
} const Scalar RED = Scalar(,,);
const Scalar PINK = Scalar(,,);
const Scalar BLUE = Scalar(,,);
const Scalar LIGHTBLUE = Scalar(,,);
const Scalar GREEN = Scalar(,,); const int BGD_KEY = CV_EVENT_FLAG_CTRLKEY; //Ctrl键
const int FGD_KEY = CV_EVENT_FLAG_SHIFTKEY; //Shift键 static 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 & ; //得到mask的最低位,实际上是只保留确定的或者有可能的前景点当做mask
} class GCApplication
{
public:
enum{ NOT_SET = , IN_PROCESS = , SET = }; static const int radius = ; static const int thickness = -; 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; } Mat getImage(); void saveImage(string path); 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; int fileId;
}; /*给类的变量赋值*/
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; //NOT_SET == 0
lblsState = NOT_SET;
prLblsState = NOT_SET;
iterCount = ;
fileId = ;
} /*给类的成员变量赋值而已*/
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();
} /*显示4个点,一个矩形和图像内容,因为后面的步骤很多地方都要用到这个函数,所以单独拿出来*/
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 ); //按照最低位是0还是1来复制,只保留跟前景有关的图像,比如说可能的前景,可能的背景
} vector<Point>::const_iterator it;
/*下面4句代码是将选中的4个点用不同的颜色显示出来*/
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, );
imshow( *winName, res );
} /*该步骤完成后,mask图像中rect内部是3,外面全是0*/
void GCApplication::setRectInMask()
{
assert( !mask.empty() );
mask.setTo( GC_BGD ); //GC_BGD == 0
rect.x = max(, rect.x);
rect.y = max(, 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) ); //GC_PR_FGD == 3,矩形内部,为可能的前景点
} 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 ); //该点处为2
}
if( flags & FGD_KEY )
{
fpxls->push_back(p);
circle( mask, p, radius, fvalue, thickness ); //该点处为3
}
} /*鼠标响应函数,参数flags为CV_EVENT_FLAG的组合*/
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) != ,
isf = (flags & FGD_KEY) != ;
if( rectState == NOT_SET && !isb && !isf )//只有左键按下时
{
rectState = IN_PROCESS; //表示正在画矩形
rect = Rect( x, y, , );
}
if ( (isb || isf) && rectState == SET ) //按下了alt键或者shift键,且画好了矩形,表示正在画前景背景点
lblsState = IN_PROCESS;
}
break;
case CV_EVENT_RBUTTONDOWN: // set GC_PR_BGD(GC_PR_FGD) labels
{
bool isb = (flags & BGD_KEY) != ,
isf = (flags & FGD_KEY) != ;
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;
}
} /*该函数进行grabcut算法,并且返回算法运行迭代的次数*/
int GCApplication::nextIter()
{
if( isInitialized )
//使用grab算法进行一次迭代,参数2为mask,里面存的mask位是:矩形内部除掉那些可能是背景或者已经确定是背景后的所有的点,且mask同时也为输出
//保存的是分割后的前景图像
grabCut( *image, mask, rect, bgdModel, fgdModel, );
else
{
if( rectState != SET )
return iterCount; if( lblsState == SET || prLblsState == SET )
grabCut( *image, mask, rect, bgdModel, fgdModel, , GC_INIT_WITH_MASK );
else
grabCut( *image, mask, rect, bgdModel, fgdModel, , GC_INIT_WITH_RECT ); isInitialized = true;
}
iterCount++; bgdPxls.clear(); fgdPxls.clear();
prBgdPxls.clear(); prFgdPxls.clear(); return iterCount;
} Mat GCApplication::getImage(){
Mat res, binMask;
if( !isInitialized )
image->copyTo( res );
else
{
getBinMask( mask, binMask );
image->copyTo( res, binMask ); //按照最低位是0还是1来复制,只保留跟前景有关的图像,比如说可能的前景,可能的背景
}
return res;
} void GCApplication::saveImage(string path = "D:\\"){
string fileName = path;
if(fileName == "D:\\"){
fileName = path + "out_img\\" + to_string(fileId) + ".jpg";
}
if(imwrite(fileName, getImage())){
cout<<"Save "<<fileName<<" , success!"<<endl;
}else{
cout<<"Failed in saving "<<fileName<<endl;
}
} GCApplication gcapp; static 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 )
{ string filename = "C:\\Users\\501-PC\\Desktop\\week-1\\pipos.jpg";
Mat image = imread( filename, ); if( image.empty() )
{
cout << "\n Durn, couldn't read image filename " << filename << endl;
return ;
} help(); const string winName = "image";
cvNamedWindow( winName.c_str(), CV_WINDOW_AUTOSIZE );
cvSetMouseCallback( winName.c_str(), on_mouse, ); gcapp.setImageAndWinName( image, winName );
gcapp.showImage();
cout<<"Show image"<<endl;
for(;;)
{
int c = cvWaitKey();
switch( (char) c )
{
case '\x1b':
cout << "Exiting ..." << endl;
goto exit_main;
case 's':
gcapp.saveImage();
break;
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 ;
}
OpenCV2.4.9 + VS2012 + win10 配置的更多相关文章
- opencv2.4.9+vs2012安装配置
需要下载并安装vs2012 http://pan.baidu.com/s/1qXP76CO 第一次启动会提示要求输入激活序列号,请输入:YKCW6-BPFPF-BT8C9-7DCTH-QXG ...
- Opencv + vs2012环境配置
首先获得最新的Opencv 2.4.10源码:opencv源码下载 一.Opencv环境变量配置 1.将源码安装到制定目录: 2.为Opencv 添加环境变量:计算机-->属性 点击高级系统设置 ...
- opencv2.4.10+VS2012配置问题
opencv2.4.10+VS2012配置 作为opencv的初学者,第一个难题想必都一样,如何配置opencv+VS的环境呢?在网上的教程,铺天盖地,但我仍然是尝试了十几次才找到属于自己的那套配置方 ...
- 学习Opencv 2.4.9 (一)---Opencv + vs2012环境配置
作者:咕唧咕唧liukun321 来自:http://blog.csdn.net/liukun321 首先获得最新的Opencv 2.4.9源代码:opencv源代码下载 一.Opencv环境变量配置 ...
- VS2012下配置MPI
并行处理结课实验,要用到MPI编程,我的电脑和VS2012都是64位的,以为MPICH也得是64位才行,结果饶了很大的弯——配置正确,添加引用之后,仍然无法识别MPI函数. 后来换了个32位的MPIC ...
- win10配置Memcached及MVC5测试分布式缓存入门
win10配置Memcached: 1.安装包下载 2.解压后有: 3.以管理员省份运行cmd: 4.安装:输入cmd命令: E:/memcached-amd64/memcached.exe -d ...
- 专治编译器编辑器vscode中文乱码输出 win10 配置系统默认utf-8编码
VS Code输出会出现乱码,很多人都遇到过.这是因为VS Code内部用的是utf-8编码,cmd/Powershell是gbk编码.直接编译,会把“你好”输出成“浣犲ソ”.如果把cmd的活动代码页 ...
- win10配置java环境变量,解决javac不是内部或外部命令等问题
win10配置java环境变量,解决javac不是内部或外部命令等问题 https://www.cnblogs.com/qianji/p/6402690.html
- Win10配置ADB工具教程
1.在该网站下载adb工具 http://pcedu.pconline.com.cn/748/7481463.html 2. Win10怎么配置ADB环境?Win10怎么安装ADB工具?这想必是很多安 ...
随机推荐
- Thinkphp5.0 的使用模型Model删除数据
Thinkphp5.0 的使用模型Model删除数据 一.使用destory()删除数据 //删除id为3的记录 $res = User::destroy(3); //返回影响的行数 dump($re ...
- [bzoj2738]矩阵乘法_整体二分_树状数组
矩阵乘法 bzoj-2738 题目大意:给定一个$n*n$的矩阵.每次给定一个矩阵求矩阵$k$小值. 注释:$1\le n\le 500$,$1\le q\le 6\cdot 10^4$. 想法: 新 ...
- [bzoj5101][POI2018]Powódź_并查集
Powódź bzoj-5101 POI-2018 题目大意:在地面上有一个水箱,它的俯视图被划分成了$n$行$m$列个方格,相邻两个方格之间有一堵厚度可以忽略不计的墙,水箱与外界之间有一堵高度无穷大 ...
- BZOJ——2190: [SDOI2008]仪仗队
思路: 我们将其所在的位置设为(0,0),那么如果存在一个点(x,y),且有gcd(x,y)=k(k!=1),那么点(x/k,y/k)一定会将(x,y)挡住.而如果k=1,那么点(x,y)就一定会被看 ...
- Binary Tree Postorder Traversal(各种非递归实现,完美利用栈结构模拟)
1.后序遍历的非递归实现.(左右根) 难点:后序遍历的非递归实现是三种遍历方式中最难的一种.因为在后序遍历中,要保证左孩子和右孩子都已被访问并且左孩子在右孩子前访问才能访问根结点,这就为流程的控制带来 ...
- IE11 文档模式空白
环境描述: win7 64位系统 安装了 更新 IE11-Windows6.1-KB2929437-x64.IE11-Windows6.1-KB3008923-x64 解决方案: 卸载 IE11-Wi ...
- Eclipse-Java代码规范和质量检查插件-阿里编码规约
此工具配套阿里巴巴Java开发手册:http://www.cnblogs.com/EasonJim/p/6436387.html Eclipse安装和使用方法:https://github.com/a ...
- CentOS 7防火墙服务FirewallD指南
CentOS 7防火墙服务FirewallD指南 作者:chszs,未经博主同意不得转载.经许可的转载需注明作者和博客主页:http://blog.csdn.net/chszs 防火墙是一种位于内部网 ...
- 自己构建的Lumbda表达式
自己构建的Lumbda表达式 定义接口: package com.stono.lambda; public interface Add { public void add(int x, int y); ...
- HDU 5067 Harry And Dig Machine(状压dp)
HDU 5067 Harry And Dig Machine 思路:因为点才10个,在加上一个起点,处理出每一个点之间的曼哈顿距离,然后用状压dp搞,状态表示为: dp[i][s],表示在i位置.走过 ...