资源简介
grabcut图像分割代码,可以连续分割(手动切换图片进行半自动连续分割),非自动连续分割,需要配置opencv,将要处理的图片放入data目录下,要求数字命名的jpg格式(如1.jpg),结果在result目录下
代码片段和文件信息
// grabcut.cpp : 定义控制台应用程序的入口点。
//
#include “stdafx.h“
#include “opencv2/highgui/highgui.hpp“
#include “opencv2/imgproc/imgproc.hpp“
#include
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 \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“
“\tw - next image and save result\n“
“\tb - choose image\n“
“\n“
“\tleft mouse button - set rectangle\n“
“\n“
“\tCTRL+left mouse button - set GC_BGD pixels\n“
“\tSHIFT+left mouse button - set GC_FGD pixels\n“
“\n“
“\tCTRL+right mouse button - set GC_PR_BGD pixels\n“
“\tSHIFT+right mouse button - set GC_PR_FGD pixels\n“ << endl;
}
const Scalar RED = Scalar(0 0 255);
const Scalar PINK = Scalar(230 130 255);
const Scalar BLUE = Scalar(255 0 0);
const Scalar LIGHTBLUE = Scalar(255 255 160);
const Scalar GREEN = Scalar(0 255 0);
Mat Result;
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 & 1; //得到mask的最低位实际上是只保留确定的或者有可能的前景点当做mask
}
class GCApplication
{
public:
enum{ NOT_SET = 0 IN_PROCESS = 1 SET = 2 };
static const int radius = 2;
static const int thickness = -1;
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; }
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 fgdPxls bgdPxls prFgdPxls prBgdPxls;
int iterCount;
};
/*给类的变量赋值*/
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 = 0;
}
/*给类的成员变量赋值而已*/
void GCApplication::setImageAndWinName(const Mat& _image const string& _winName)
{
if (_image.empty() || _winName.empty())
return;
image = &_image;
winName = &_winName;
mask.create(image->size() CV_8UC
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 565 2018-10-09 14:58 grabcut -连续处理\grabcut\grabcut\Debug\grabcut.Build.CppClean.log
文件 210 2018-10-09 14:58 grabcut -连续处理\grabcut\grabcut\Debug\grabcut.log
文件 1638400 2018-10-09 14:58 grabcut -连续处理\grabcut\grabcut\Debug\grabcut.pch
文件 1550 2018-10-09 14:58 grabcut -连续处理\grabcut\grabcut\Debug\grabcut.tlog\cl.command.1.tlog
文件 2904 2018-10-09 14:58 grabcut -连续处理\grabcut\grabcut\Debug\grabcut.tlog\CL.read.1.tlog
文件 1146 2018-10-09 14:58 grabcut -连续处理\grabcut\grabcut\Debug\grabcut.tlog\CL.write.1.tlog
文件 180 2018-10-09 14:58 grabcut -连续处理\grabcut\grabcut\Debug\grabcut.tlog\grabcut.lastbuildstate
文件 0 2018-10-09 14:58 grabcut -连续处理\grabcut\grabcut\Debug\grabcut.tlog\unsuccessfulbuild
文件 11660 2018-10-09 14:58 grabcut -连续处理\grabcut\grabcut\Debug\stdafx.obj
文件 52224 2018-10-09 14:58 grabcut -连续处理\grabcut\grabcut\Debug\vc120.idb
文件 143360 2018-10-09 14:58 grabcut -连续处理\grabcut\grabcut\Debug\vc120.pdb
文件 9707 2018-12-07 16:00 grabcut -连续处理\grabcut\grabcut\grabcut.cpp
文件 4905 2018-10-09 15:01 grabcut -连续处理\grabcut\grabcut\grabcut.vcxproj
文件 1314 2018-10-09 14:54 grabcut -连续处理\grabcut\grabcut\grabcut.vcxproj.filters
文件 165 2018-10-09 14:54 grabcut -连续处理\grabcut\grabcut\grabcut.vcxproj.user
文件 1096 2018-10-09 15:04 grabcut -连续处理\grabcut\grabcut\PropertySheet.props
文件 1510 2018-10-09 14:54 grabcut -连续处理\grabcut\grabcut\ReadMe.txt
文件 1039 2018-11-29 21:45 grabcut -连续处理\grabcut\grabcut\Release\grabcut.Build.CppClean.log
文件 174 2018-11-29 21:45 grabcut -连续处理\grabcut\grabcut\Release\grabcut.log
文件 1255017 2018-11-29 21:45 grabcut -连续处理\grabcut\grabcut\Release\grabcut.obj
文件 1638400 2018-11-29 21:45 grabcut -连续处理\grabcut\grabcut\Release\grabcut.pch
文件 1622 2018-11-29 21:45 grabcut -连续处理\grabcut\grabcut\Release\grabcut.tlog\cl.command.1.tlog
文件 16328 2018-11-29 21:45 grabcut -连续处理\grabcut\grabcut\Release\grabcut.tlog\CL.read.1.tlog
文件 1068 2018-11-29 21:45 grabcut -连续处理\grabcut\grabcut\Release\grabcut.tlog\CL.write.1.tlog
文件 185 2018-11-29 21:45 grabcut -连续处理\grabcut\grabcut\Release\grabcut.tlog\grabcut.lastbuildstate
文件 2390 2018-11-29 21:45 grabcut -连续处理\grabcut\grabcut\Release\grabcut.tlog\li
文件 5794 2018-11-29 21:45 grabcut -连续处理\grabcut\grabcut\Release\grabcut.tlog\li
文件 600 2018-11-29 21:45 grabcut -连续处理\grabcut\grabcut\Release\grabcut.tlog\li
文件 30907 2018-11-29 21:45 grabcut -连续处理\grabcut\grabcut\Release\stdafx.obj
文件 684032 2018-11-29 21:45 grabcut -连续处理\grabcut\grabcut\Release\vc120.pdb
............此处省略36个文件信息
评论
共有 条评论