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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
第一次来,支持一个
[***.219.124.196]2024年04月24日 02时46分04秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
HikariCP、MySQL Configuration 性能优化
2019-04-27
系统配置自动装载机制 - 分布式开发
2019-04-27
SpringCloud实战 - Hystrix
2019-04-27
Kafka实战(七) - 优雅地部署 Kafka 集群
2019-04-27
Java支付系统(三) - SpringBoot 应用程序搭建
2019-04-27
详解Java业务领域分层模型中的vo/po/dto/pojo/bo
2019-04-27
Java持久层框架MyBatis全注解详解
2019-04-27
Java线程组ThreadGroup
2019-04-27
Java同步器之AbstractOwnableSynchronizer详解
2019-04-27
为什么需要学习并发编程?
2019-04-27
Java计算机IT编程文档常见单词翻译
2019-04-27
Java协作中断机制
2019-04-27
MySQL8.0数据库基础教程(二)-理解"关系"
2019-04-27
2020年最新阿里Java面试题,看看你都会了吗?
2019-04-27
大厂业务开发面试必问的UML你都会了吗?
2019-04-27
MySQL8.0关系数据库基础教程(三)-select语句详解
2019-04-27
JVM参数调优基础-参数的类型详解
2019-04-27
大厂都这么使用MySQL8进行条件查询
2019-04-27