vs2015+opencv3.3.1+ maxflow-v3.01 c++实现Yuri Boykov 的Interactive Graph Cuts
发布日期:2022-04-04 06:36:26 浏览次数:18 分类:博客文章

本文共 5157 字,大约阅读时间需要 17 分钟。

出的结果不理想。 感觉是tlink的权重的计算有问题,以及参数的设置。三个可设置参数是后面的 i j k  。如果你找到了一组好参数请告诉我。

下载地址 http://download.csdn.net/download/li_haoren/10232283

 

 

 

 

 Interactive Graph Cuts  for Optimal Boundary & Region Segmentation of Objects in N-D Images  code

#include "opencv2\imgproc\imgproc.hpp"  // Gaussian Blur#include "opencv2\core\core.hpp"        // Basic OpenCV structures (cv::Mat, Scalar)#include 
// OpenCV window I/O#include
// for standard I/O#include
// for strings#include
// for controlling float print precision#include
// string to number conversion#include
#include
#include "graph.h"  //maxflow-v3.01   下载地址 http://vision.csd.uwo.ca/code/maxflow-v3.01.zip typedef Graph
GraphType;using namespace cv;using namespace std; int ofexp = 5;//Lambdadouble BSigma = 2;//the Sigma of B{p,q} 小边缘越清double RSigma = 3;//the Sigma of Rp 小号const string filename = "1.jpg";//照片bool rButtonDown = false;bool lButtonDown = false;int numUsedBins = 0;float varianceSquared = 0;int scribbleRadius = 10;Mat inputImg = imread(filename, 3), showImg = imread(filename, 3);Mat fgScribbleMask;Mat bgScribbleMask;static void onMouse(int event, int x, int y, int, void*){ //cout << "On Mouse: (" << x << "," << y << ")" <
(y,x)=(char)255; // set variables using mask //showImg.setTo(redColorElement,fgScribbleMask); //showImg.at
(y,x)[0] = 0; //showImg.at
(y,x)[1] = 0; //showImg.at
(y,x)[2] = 255; } } imshow("draw", showImg); imshow("fg mask", fgScribbleMask); imshow("bg mask", bgScribbleMask);}vector
slink(inputImg.cols*inputImg.rows),tlink(inputImg.cols*inputImg.rows);double k=0;GraphType *g = new GraphType(inputImg.cols*inputImg.rows, inputImg.cols*inputImg.rows*4);inline int node1n(int i, int j) { return i*inputImg.cols + j; }void computnlink() { double weight0=0; double weight1=0; double weight2=0; double weight3=0; for (int i = 0; i < inputImg.rows; ++i) for (int j = 0; j < inputImg.cols; ++j) { g->add_node(); } for (int i = 0; i < inputImg.rows; ++i) for (int j = 0; j < inputImg.cols; ++j) { if (i == 0); else { weight0 = ofexp*exp(-(pow(inputImg.at
(i, j)[0] - inputImg.at
(i - 1, j)[0], 2) + pow(inputImg.at
(i, j)[1] - inputImg.at
(i - 1, j)[1], 2) + pow(inputImg.at
(i, j)[2] - inputImg.at
(i - 1, j)[2], 2)) / 6.0 / BSigma / BSigma); g->add_edge(node1n(i, j), node1n(i - 1, j), weight0, weight0); } if (i + 1 ==inputImg.rows); else { weight2 = ofexp*exp(-(pow(inputImg.at
(i, j)[0] - inputImg.at
(i + 1, j)[0], 2) + pow(inputImg.at
(i, j)[1] - inputImg.at
(i + 1, j)[1], 2) + pow(inputImg.at
(i, j)[2] - inputImg.at
(i + 1, j)[2], 2)) / 6.0 / BSigma / BSigma); g->add_edge(node1n(i, j), node1n(i + 1, j), weight2, weight2); } if (j==0); else { weight3 = ofexp*exp(-(pow(inputImg.at
(i, j)[0] - inputImg.at
(i , j-1)[0], 2) + pow(inputImg.at
(i, j)[1] - inputImg.at
(i , j-1)[1], 2) + pow(inputImg.at
(i, j)[2] - inputImg.at
(i , j-1)[2], 2)) / 6.0 / BSigma / BSigma); g->add_edge(node1n(i, j), node1n(i , j-1), weight3, weight3); } if (j + 1 == inputImg.cols); else { weight1 = ofexp*exp(-(pow(inputImg.at
(i, j)[0] - inputImg.at
(i, j +1)[0], 2) + pow(inputImg.at
(i, j)[1] - inputImg.at
(i, j + 1)[1], 2) + pow(inputImg.at
(i, j)[2] - inputImg.at
(i, j + 1)[2], 2)) / 6.0 / BSigma / BSigma); g->add_edge(node1n(i, j), node1n(i, j + 1), weight1, weight1); } if (k < weight1+ weight0+ weight3+ weight2) k = weight1 + weight0 + weight3 + weight2; } k += 1;}Mat greyimg = imread(filename, 0); double gray[256]; double Bgray[256]; double Fgray[256]; double weight1 = 0; double weight2 = 0;/*weight有问题改动*/void computtlink() { for (int i = 0; i < greyimg.rows*greyimg.cols; i++) { ++gray[greyimg.at
(i)]; if(bgScribbleMask.at
(i)) ++Bgray[greyimg.at
(i)]; if (fgScribbleMask.at
(i)) ++Fgray[greyimg.at
(i)]; } cout << greyimg.rows*greyimg.cols<
(i) - j, 2))/2 / RSigma)); weight2 += (!gray[j]) ? 0 : ((Bgray[j] / gray[j])*exp(-1 * (pow(greyimg.at
(i) - j, 2))/2/ RSigma)); } if (bgScribbleMask.at
(i)) {g->add_tweights(i, /* capacities */ k, 0); continue;} if (fgScribbleMask.at
(i)) {g->add_tweights(i, /* capacities */ 0, k); continue;} g->add_tweights(i, /* capacities */weight2 ,weight1 ); }}int main() { cout << "在draw上画前景背景,左键红前景,右键蓝背景。然后在input输入s 开始运算" << endl; bgScribbleMask.create(2, inputImg.size, CV_8UC1); bgScribbleMask = 0; fgScribbleMask.create(2, inputImg.size, CV_8UC1); Mat result; result.create(2, inputImg.size, CV_8UC1); result = 0; fgScribbleMask = 0; namedWindow("draw"); imshow("draw", showImg); namedWindow("input"); imshow("input", inputImg); string ii[250]; for (int i = 1; i < 250; i++) ii[i] = i; setMouseCallback("draw", onMouse, 0); while (1) { char key = waitKey(0); if (key == 's') { double j = 5; int k = 13;for (int i = 1; i < 10; ++++i) { ofexp = i;//Lambda 三个可变动参数 BSigma = j;//the Sigma of B{p,q} 小边缘越清 RSigma = k; computnlink(); computtlink(); int flow = g->maxflow(); printf("Flow = %d\n", flow); cout << k; for (int i = 0; i < inputImg.rows*inputImg.cols; i++) { if (g->what_segment(i)) { result.at
(i) = 255; result.at
(i) = 255; result.at
(i) = 255; } } namedWindow(ii[i+k*6]); imshow(ii[i+k * 6], result); } } } return 0;}

  

转载地址:https://www.cnblogs.com/l2017/p/8386583.html 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:c++迭代递归实现汉诺塔(5种迭代方法满足你)
下一篇:在构造函数和析构函数中调用虚函数------新标准c++程序设计

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2024年04月24日 02时46分04秒