OpenCV 鼠标手动绘制掩码图像
OpenCV 鼠标手动绘制掩码图像
完整的代码:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
using namespace cv;
cv::Mat marker_mask;
cv::Mat markers;
cv::Mat img0, img, img_gray, wshed;
cv::Point prev_pt(-1,-1);
void on_mouse( int event, int x, int y, int flags, void* param )
{
if( !img.data )
return;
if( event == CV_EVENT_LBUTTONUP || !(flags & CV_EVENT_FLAG_LBUTTON) )
prev_pt = cv::Point(-1,-1);
else if( event == CV_EVENT_LBUTTONDOWN )
prev_pt = cv::Point(x,y);
else if( event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON) )
{
cv::Point pt(x,y);
if( prev_pt.x < 0 )
prev_pt = pt;
cv::line( marker_mask, prev_pt, pt, cv::Scalar::all(255), 1, 8, 0 );
cv::line( img, prev_pt, pt, cv::Scalar::all(255), 1, 8, 0 );
prev_pt = pt;
cv::imshow( "image", img );
}
}
int main(){
img0 = cv::imread("phase_map_org.bmp", 1);
if(!img0.data)
return 0;
cv::namedWindow( "image" );
cv::namedWindow( "watershed transform" );
img = img0.clone();
img_gray = img0.clone();
wshed = img0.clone();
marker_mask = cv::Mat( img.size(), CV_8U, cv::Scalar::all(0));
cv::imshow( "image", img );
cv::imshow( "watershed transform", wshed );
cvSetMouseCallback( "image", on_mouse, 0 );
for(;;){
int c = cv::waitKey(0);
if( (char)c == 27 )
break;
if( (char)c == 'r' ) {
marker_mask.setTo(cv::Scalar::all(0));
img0.copyTo(img);
cv::imshow( "image", img );
}
if( (char)c == 's' ) {
cv::imwrite("marker_mask.bmp", marker_mask);
std::cout << "save marker_mask.bmp finals " << std::endl;
}
if( (char)c == 'p' ) {
std::cout << "processing ... " << std::endl;
std::vector<std::vector<cv::Point> > contours;
vector<Vec4i> hierarchy;
cv::findContours( marker_mask, contours,
CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE );
marker_mask.setTo(cv::Scalar::all(0));
cv::drawContours( marker_mask, contours, -1,
cv::Scalar(255), CV_FILLED );
//cv::Mat color_tab;
std::cout << contours.size() << std::endl;
cv::imshow( "image", marker_mask );
}
}
cv::waitKey(0);
return 0;
}
得到掩码图像
将闭合环线外的部分像素值全设为0,内部全设为255.
就是在for(;;)循环里面的:
if( (char)c == 'p' ) {
std::cout << "processing ... " << std::endl;
std::vector<std::vector<cv::Point> > contours;
vector<Vec4i> hierarchy;
cv::findContours( marker_mask, contours,
CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE );
marker_mask.setTo(cv::Scalar::all(0));
cv::drawContours( marker_mask, contours, -1,
cv::Scalar(255), CV_FILLED );
//cv::Mat color_tab;
std::cout << contours.size() << std::endl;
cv::imshow( "image", marker_mask );
}
运行程序:鼠标手动绘制一个闭合环线,按P生成掩码图像
知识点
清零函数
OpenCV1的函数cvZero( marker_mask );等同于OpenCV2的函数:
marker_mask.setTo(cv::Scalar::all(0));复制函数和克隆函数
OpenCV1的函数cvCopy( 原图, 副本);等同于OpenCV2的函数:
原图.copyTo(副本);或着
副本 = 原图.clone();
参考网站:
http://blog.csdn.net/wangyaninglm/article/details/41864021
http://answers.opencv.org/question/26415/cvzero-fucntion-for-c-api/
OpenCV 鼠标手动绘制掩码图像的更多相关文章
- Python 图像处理 OpenCV (16):图像直方图
前文传送门: 「Python 图像处理 OpenCV (1):入门」 「Python 图像处理 OpenCV (2):像素处理与 Numpy 操作以及 Matplotlib 显示图像」 「Python ...
- 【OpenCV入门教程之三】 图像的载入,显示和输出 一站式完全解析(转)
本系列文章由@浅墨_毛星云 出品,转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/20537737 作者:毛星云(浅墨) ...
- Python 图像处理 OpenCV (15):图像轮廓
前文传送门: 「Python 图像处理 OpenCV (1):入门」 「Python 图像处理 OpenCV (2):像素处理与 Numpy 操作以及 Matplotlib 显示图像」 「Python ...
- -_-#【Canvas】导出在<canvas>元素上绘制的图像
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- (原)使用opencv的warpAffine函数对图像进行旋转
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5070576.html 参考网址: http://stackoverflow.com/questions ...
- OpenCV成长之路:图像直方图的应用
OpenCV成长之路:图像直方图的应用 2014-04-11 13:57:03 标签:opencv 图像 直方图 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否 ...
- OpenCV成长之路:图像滤波
http://ronny.blog.51cto.com/8801997/1394138 OpenCV成长之路:图像滤波 2014-04-11 14:28:44 标签:opencv 边缘检测 sobel ...
- Matlab绘图基础——利用axes(坐标系图形对象)绘制重叠图像 及 一图多轴(一幅图绘制多个坐标轴)
描述 axes在当前窗口中创建一个包含默认属性坐标系 axes('PropertyName',propertyvalue,...)创建坐标系时,同时指定它的一些属性,没有指定的使用DefaultAxe ...
- Delphi实例之绘制正弦函数图像
Delphi实例之绘制正弦函数图像 unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphic ...
随机推荐
- Jmeter使用文档(windows)
1. 安装jdk并配置环境变量 以1.8为例: (1)安装jdk1.8; (2)在系统变量里点击新建,变量名填写JAVA_HOME,变量值填写JDK的安装路径“C:\Program Files\Jav ...
- 第三篇 css属性
一.颜色属性 颜色属性有下面四种方式 <div style="color:blueviolet">ppppp</div> <div style=&qu ...
- hd acm1061
Problem Description Given a positive integer N, you should output the most right digit of N^N. Inp ...
- EntityFramework 学习 一 Eager Loading
贪婪加载是指查询一个类型实体的时候同时查询与实体关联的类型 通过Include()方法实现 using (var context = new SchoolDBEntities()) { var stu ...
- EntityFramework 学习 一 Local Data
DBSet的Local属性提供简单的从context上下文获取当前已经被跟踪的实体(实体不能被标记为Deleted状态) using System.Data.Entity; class Program ...
- Codeforces 358D Dima and Hares:dp【只考虑相邻元素】
题目链接:http://codeforces.com/problemset/problem/358/D 题意: 有n个物品A[i]摆成一排,你要按照某一个顺序将它们全部取走. 其中,取走A[i]的收益 ...
- C#中在内容页获取其模板页中的变量,或者值
在CSDN的博文中看到了 muziduoxi 的文章:http://blog.csdn.net/muziduoxi/article/details/5386543 虽然里面提到的方法没有解决我的难题, ...
- C++STL 常用 函数 用法(转)
http://www.cnblogs.com/duoduo369/archive/2012/04/12/2439118.html 迭代器(iterator) 个人理解就是把所有和迭代有关的东西给抽象出 ...
- Hibernate学习---第十四节:hibernate之session线程安全
1.hibernate.cfg.xml 文件中添加如下代码开启线程安全: <property name="hibernate.current_session_context_class ...
- CNN检测模型统计检出率
X, y = get_feature_charseq() #max_document_length=64 volcab_file = "volcab.pkl" assert os. ...