引言

Grab Cut是在Graph Cut上进行改进的,相比后者,前者的新颖之处在于:

  1. 迭代预测估计(iterative estimation)
  2. 不完全标记(incomplete labelling)

用户可以通过较少的交互性标记,就可以完成图像分割。具体的原理参考以下文章:

  • GraphCut入门学习
  • 图像分割之(一~四)GraphCut,GrabCut函数使用和源码解读
  • 图割小结

文中提到的经典论文:

  1. Interactive Graph Cutsfor Optimal Boundary & Region Segmentation of Objects in N-D Images  (Yuri Y. Boykov Marie-Pierre Jolly)
  2. “GrabCut” — Interactive Foreground Extraction using Iterated Graph Cuts  (Carsten Rother     Vladimir Kolmogorov
    Andrew Blake)

代码

最近在看图像分割方面的论文,发现了这种分割方法,这里复现了《“GrabCut” — Interactive Foreground Extraction using Iterated Graph Cuts》中的图像分割。只需要用鼠标将目标框出,可以将目标图像从复杂背景中“扣”出来。

// ConvertColor.cpp : 定义控制台应用程序的入口点。
//#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>using namespace std;
using namespace cv;
// Global variables
// Flags updated according to left mouse button activity
bool ldown = false, lup = false;
// Original image
Mat img;
// Starting and ending points of the user's selection
Point corner1, corner2;
// ROI
Rect box;
Mat bgModel, fgModel, mask;
// Callback function for mouse events
static void mouse_callback(int event, int x, int y, int, void *)
{// When the left mouse button is pressed, record its position and save it in corner1if (event == EVENT_LBUTTONDOWN){ldown = true;corner1.x = x;corner1.y = y;cout << "Corner 1 recorded at " << corner1 << endl;}// When the left mouse button is released, record its position and save it in corner2if (event == EVENT_LBUTTONUP){// Also check if user selection is bigger than 20 pixels (jut for fun!)if (abs(x - corner1.x) > 10 && abs(y - corner1.y) > 10){lup = true;ldown = false;corner2.x = x;corner2.y = y;cout << "Corner 2 recorded at " << corner2 << endl << endl;}else{cout << "Please select a bigger region" << endl;ldown = false;}}// Update the box showing the selected region as the user drags the mouseif (ldown == true && lup == false){Point pt;pt.x = x;pt.y = y;Mat local_img = img.clone();rectangle(local_img, corner1, pt, Scalar(0, 0, 255),3);imshow("Cropping app", local_img);}// Define ROI and crop it out when both corners have been selectedif (ldown == false && lup == true){box.width = abs(corner1.x - corner2.x);box.height = abs(corner1.y - corner2.y);box.x = min(corner1.x, corner2.x);box.y = min(corner1.y, corner2.y);// Make an image out of just the selected ROI and display it in a new window/*    Mat crop(img, box);*/grabCut(img, mask, box, bgModel, fgModel, 5, cv::GC_INIT_WITH_RECT);compare(mask, GC_PR_FGD, mask, cv::CMP_EQ);namedWindow("Crop");Mat imageROI;imageROI.create(img.size(), img.type());/*imageROI = img(Rect(box));*/imageROI.setTo(Scalar(255, 255, 255));img.copyTo(imageROI, mask);Mat crop(imageROI, box);imshow("Crop", crop);//Mat test = img - imageROI;//imshow("Crop", test);///*imwrite("Crop1.jpg", imageROI);*/ldown = false;lup = false;}
}
int main()
{img = imread("flower.jpg");/*img = imread("Crop.jpg");*/namedWindow("Cropping app");imshow("Cropping app", img);// Set the mouse event callback functionsetMouseCallback("Cropping app", mouse_callback);// Exit by pressing 'q'while (char(waitKey(1)) != 'q') {}return 0;
}

结果

从下图看,效果还算可以!

感谢阅读,请多指教!

基于Grab Cut交互性图像分割相关推荐

  1. 图像分割之(三)从Graph Cut到Grab Cut

    上一文对GraphCut做了一个了解,而现在我们聊到的GrabCut是对其的改进版,是迭代的Graph Cut.OpenCV中的GrabCut算法是依据<"GrabCut" ...

  2. 图像分割(三)--Grab Cut

     原文: http://blog.csdn.net/zouxy09/article/details/8534954 上一文对GraphCut做了一个了解,而现在我们聊到的GrabCut是对其的改进 ...

  3. Grab Cut算法

    转载自:http://blog.csdn.net/zouxy09/article/details/8534954 上一文对GraphCut做了一个了解,而现在我们聊到的GrabCut是对其的改进版,是 ...

  4. 根据大小分割大文本_基于深度学习的图像分割在高德地图的实践

    一.前言 图像分割(Image Segmentation)是计算机视觉领域中的一项重要基础技术,是图像理解中的重要一环.图像分割是将数字图像细分为多个图像子区域的过程,通过简化或改变图像的表示形式,让 ...

  5. 基于深度学习的图像分割在高德的实践

    一.前言 图像分割(Image Segmentation)是计算机视觉领域中的一项重要基础技术,是图像理解中的重要一环.图像分割是将数字图像细分为多个图像子区域的过程,通过简化或改变图像的表示形式,让 ...

  6. matlab实现谱聚类法图像分割代码,一种基于谱聚类的图像分割方法与系统与流程...

    本发明是一种基于谱聚类的图像分割方法与系统,涉及聚类.机器学习和人工智能领域.特别涉及通过相关性将已经学习到的知识运用到图像分割中,并在此基础上构造性地改造谱聚类方法,从而达到快速精确地分割彩色图像的 ...

  7. Python+OpenCV:基于分水岭算法的图像分割(Image Segmentation with Watershed Algorithm)

    Python+OpenCV:基于分水岭算法的图像分割(Image Segmentation with Watershed Algorithm) ############################ ...

  8. 基于聚类算法的图像分割技术

    本科毕业设计,在此总结. 1. 聚类概念 如今,学术界并未对聚类作出一个具体定义.目前,认可度最高的观点是:聚类是一种无监督的分类手段.无标签的数据集可通过聚类分析中设定的相似性度量进行分类,形成多个 ...

  9. 基于GMM模型的图像分割与颜色迁移算法

    在之前的博客中,我们已经介绍过高斯混合模型(GMM)的一些基础知识,链接: Gaussian Mixture Model:混合高斯模型介绍_程序猿老甘的博客-CSDN博客 在该博客中,我们提到了贾佳亚 ...

最新文章

  1. Leetcode 50
  2. V-rep学习笔记:vrep中的实用工具
  3. PHP中的SESSION机制应用
  4. 深入理解Java中的位操作
  5. scala 方法调用_Scala中的方法调用
  6. 00后大学生调研四五线城镇商户:91%用上移动支付
  7. UI实用素材|APP动效设计
  8. 随想录(关于论文投稿)
  9. bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: 的解决方法
  10. 无法注册这篇文档........
  11. homework7_ZhankunLuo
  12. UVa 11991 - Easy Problem from Rujia Liu?
  13. (附源码)Springboot酒店会员点餐系统 毕业设计 072005
  14. RocketMQ消息存储学习
  15. ctf比赛/学习资源整理,记得收藏!
  16. 【CP2K教程(二)】WO3的投影态密度和能带结构
  17. Java回炉之File
  18. oracle substr clob,sql – 在CLOB上的SUBSTR的性能
  19. vue移动端表格吸顶、行自适应高度、列左侧固定悬浮
  20. Android中如何获取IMEI号码

热门文章

  1. 大学物理(上)-期末知识点结合习题复习(3)——质点运动学-惯性系 非惯性系 惯性力 动量定理 动量守恒定律
  2. Unity 中的声音系统
  3. 2021夏实验室内部系列讲座总结
  4. Mysql 命令行模式访问操作mysql数据库
  5. “逝者如斯夫,不舍昼夜”
  6. 5分钟读懂一致性哈希算法原理
  7. 严重: maxIdle is deprecated,严重: testWhileIdle is true, validationQuery not set,Druid连接池连接MSQL报错处理
  8. SQL全方位攻略:3.SQL标准
  9. 深度学习笔记 6 卷积神经网络
  10. 临时关闭selinux模式 setenforce 0