//Copyright (c) 2014, Lena Gorelick
//All rights reserved.
//
//Redistribution and use in source and binary forms, with or without
//modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of the University of Western Ontarior nor the
// names of its contributors may be used to endorse or promote products
// derived from this software without specific prior written permission.
//
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
//ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
//WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
//DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
//DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
//(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
//LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
//ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
//(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
//SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//
//THIS SOFTWARE IMPLEMENTS THE OneCut ALGORITHM THAT USES SCRIBBLES AS HARD CONSTRAINTS.
//PLEASE USE THE FOLLOWING CITATION:
//
//@inproceedings{iccv2013onecut,
// title = {Grabcut in One Cut},
// author = {Tang, Meng and Gorelick, Lena and Veksler, Olga and Boykov, Yuri},
// booktitle={International Conference on Computer Vision},
// month = {December},
// year = {2013}}
//
//THIS SOFTWARE USES maxflow/min-cut CODE THAT WAS IMPLEMENTED BY VLADIMIR KOLMOGOROV,
//THAT CAN BE DOWNLOADED FROM http://vision.csd.uwo.ca/code/.
//PLEASE USE THE FOLLOWING CITATION:
//
//@ARTICLE{Boykov01anexperimental,
// author = {Yuri Boykov and Vladimir Kolmogorov},
// title = {An Experimental Comparison of Min-Cut/Max-Flow Algorithms for Energy Minimization in Vision},
// journal = {IEEE TRANSACTIONS ON PATTERN ANALYSIS AND MACHINE INTELLIGENCE},
// year = {2001},
// volume = {26},
// pages = {359--374}}
//
//
//
//THIS SOFTWARE USES OpenCV 2.4.3 THAT CAN BE DOWNLOADED FROM http://opencv.org
//
//
//
//
//
//
//
//
//
//##################################################################
//
// USAGE INSTRUCTIONS
//
// In the command line type:
//
// OneCut <imageFileName> [<numBinsPerChannel> <colorSep_slope>]
//
// Default values: numBinsPerChannel=64 colorSep_slope= 0.1
//
// Example: OneCut frida_small.jpg 64 0.1
// or OneCut frida_small.jpg
//
//
// Once the image is opened you can scribble with left and right
// mouse buttons on the object and the background in the
// "Scribble Image" window. Once the scribbles are given you can
// segment the image.You can keep repeatedly adding scribbles and
// segmenting until the result is satisfactory.
//
//
// "Use the following Short Keys:
// 'q' - quit
// 's' - segment
// 'r' - reset (removes all strokes and clears all results)
// 'k' - keep the scribbles and the segmentation
// 'l' - load the scribbles
// '+' - increase brush stroke radius
// '-' - decrease brush stroke radius
// 'right mouse button drag' - draw blue scribble
// 'left mouse button drag' - draw red scribble
//
//
#include <iostream> // for standard I/O
#include <string> // for strings
#include <iomanip> // for controlling float print precision
#include <sstream> // string to number conversion
#include <opencv2/imgproc/imgproc.hpp> // Gaussian Blur
#include <opencv2/core/core.hpp> // Basic OpenCV structures (cv::Mat, Scalar)
#include <opencv2/highgui/highgui.hpp> // OpenCV window I/O
#include "graph.h"
using namespace std;
using namespace cv;
// images
Mat inputImg, showImg, binPerPixelImg, showEdgesImg, segMask, segShowImg;
// mask
Mat fgScribbleMask, bgScribbleMask, fgScribbleMaskAll, bgScribbleMaskAll;
// user clicked mouse buttons flags
bool rButtonDown = false;
bool lButtonDown = false;
int numUsedBins = 0;
float varianceSquared = 0;
int scribbleRadius = 10;
// default arguments
float bha_slope = 0.5f;
int numBinsPerChannel = 16;
float EDGE_STRENGTH_WEIGHT = 0.95f;
const float INT32_CONST = 1000;
const float HARD_CONSTRAINT_CONST = 1000;
#define NEIGHBORHOOD_4_TYPE 1;
const int NEIGHBORHOOD = NEIGHBORHOOD_4_TYPE;
//************************************
// F u n c t i o n d e c l a r a t i o n s
// init all images/vars
int init(char * imgFileName);
// clear everything before closing
void destroyAll();
// mouse listener
static void onMouse(int event, int x, int y, int, void*);
// set bin index for each image pixel, store it in binPerPixelImg
void getBinPerPixel(Mat & binPerPixelImg, Mat & inputImg, int numBinsPerChannel, int & numUsedBins);
// compute the variance of image edges between neighbors
void getEdgeVariance(Mat & inputImg, Mat & showEdgesImg, float & varianceSquared);
// keep the scribbles for later
int keepScribbles(char * imgFileName, Mat & fgScribbleMaskAll, Mat & bgScribbleMaskAll, Mat & segMask, float colorSep, int numBinsPerChannel, float contrastSensitive);
int loadScribbles(char * imgFileName, Mat & fgScribbleMask, Mat & bgScribbleMask);
void getColorSepE(int & colorSep_E, int & hardConstraints_E);
typedef Graph<int, int, int> GraphType;
GraphType *myGraph;
//***********************************
// M a i n
int main(int argc, char *argv[])
{
/*if (argc > 4 || argc < 2)
{
cout << " Usage: OneCut ImageToSegment [numBinsPerChannel colorSep_slope]" << endl;
return -1;
}*/
if (argc >= 3)
{
// get the second arg
String numBinsStr(argv[2]);
// convert to int
numBinsPerChannel = atoi(numBinsStr.c_str());
cout << "Using " << numBinsPerChannel << " bins per channel " << endl;
if (argc >= 4)
{
//get third argument
String bhaSlopeStr(argv[3]);
bha_slope = (float)atof(bhaSlopeStr.c_str());
cout << "Using colorSep_slope = " << bha_slope << endl;
}
else
cout << "Using default colorSep_slope = " << bha_slope << endl;
}
else
{
cout << "Using default " << numBinsPerChannel << " bins per channel " << endl;
cout << "Using default colorSep_slope = " << bha_slope << endl;
}
// get img name parameter
char * imgFileName = "E://data//test//cat15.jpg";
if (init(imgFileName) == -1)
{
cout << "Could not initialize" << endl;
return -1;
}
cout << "if there are scribbles from before, load them" << endl;
if (loadScribbles(imgFileName, fgScribbleMask, bgScribbleMask) == -1)
{
cout << "could not load scribbles" << std::endl;
cout << "running without scribbles" << std::endl;
}
cout << "Use the following Short Keys:" << endl;
cout << " 'q' - quit" << endl;
cout << " 's' - segment" << endl;
cout << " 'r' - reset (removes all strokes and clears all results)" << endl;
cout << " 'k' - keep the scribbles and the segmentation" << endl;
cout << " 'l' - load the scribbles" << endl;
cout << " '+' - increase brush stroke radius" << endl;
cout << " '-' - decrease brush stroke radius" << endl;
cout << " 'right mouse button drag' - draw blue scribble" << endl;
cout << " 'left mouse button drag' - draw red scribble" << endl;
// Wait for a keystroke in the window
for (;;)
{
char key = waitKey(0);
switch (key)
{
case 'q':
cout << "goodbye" << endl;
destroyAll();
return 0;
case '-':
if (scribbleRadius > 2)
scribbleRadius--;
cout
没有合适的资源?快使用搜索试试~ 我知道了~
one-cut-withseed代码实现
共4个文件
cpp:3个
h:1个
1星 需积分: 18 33 下载量 195 浏览量
2018-11-10
16:07:22
上传
评论 1
收藏 15KB 7Z 举报
温馨提示
人工设置种子点的one-cut算法。包括主函数、以及必要库文件代码。放入自己opencv工程,可直接使用。
资源推荐
资源详情
资源评论
收起资源包目录
one-cut-withseed.7z (4个子文件)
main_2013_oneCut_Seed.cpp 27KB
graph.h 17KB
graph.cpp 3KB
maxflow.cpp 15KB
共 4 条
- 1
资源评论
- h2479324492019-08-06touwenjian都不全 不要了
PandasRan
- 粉丝: 30
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功