资源简介
基于OpenCV库,实现分水岭算法图像分割。通过鼠标左键点选或涂抹选择分水岭起点,对图像各区域进行有效分割。已测试可以正常运行,如无法正常运行请联系本人。
代码片段和文件信息
#include “opencv2/imgproc/imgproc.hpp“
#include “opencv2/highgui/highgui.hpp“
#include
#include
using namespace cv;
using namespace std;
static void help()
{
cout << “\nThis program demonstrates the famous watershed segmentation algorithm in OpenCV: watershed()\n“
“Usage:\n“
“./watershed [image_name -- default is fruits.jpg]\n“ << endl;
cout << “Hot keys: \n“
“\tESC - quit the program\n“
“\tr - restore the original image\n“
“\tw or SPACE - run watershed segmentation algorithm\n“
“\t\t(before running it *roughly* mark the areas to segment on the image)\n“
“\t (before that roughly outline several markers on the image)\n“;
}
Mat markerMask img;
Point prevPt(-1 -1);
static void onMouse( int event int x int y int flags void* )
{
if( x < 0 || x >= img.cols || y < 0 || y >= img.rows )
return;
if( event == CV_EVENT_LBUTTONUP || !(flags & CV_EVENT_FLAG_LBUTTON) )
prevPt = Point(-1-1);
else if( event == CV_EVENT_LBUTTONDOWN )
prevPt = Point(xy);
else if( event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON) )
{
Point pt(x y);
if( prevPt.x < 0 )
prevPt = pt;
line( markerMask prevPt pt Scalar::all(255) 5 8 0 );
line( img prevPt pt Scalar::all(255) 5 8 0 );
prevPt = pt;
imshow(“image“ img);
}
}
int main( int argc char** argv )
{
//char* filename = argc >= 2 ? argv[1] : (char*)“fruits.jpg“;
char* filename = (char*)“fruits.jpg“;
Mat img0 = imread(filename 1) imgGray;
if( img0.empty() )
{
cout << “Couldn‘g open image “ << filename << “. Usage: watershed \n“;
return 0;
}
help();
namedWindow( “image“ 1 );
img0.copyTo(img);
cvtColor(img markerMask COLOR_BGR2GRAY);
cvtColor(markerMask imgGray COLOR_GRAY2BGR);
markerMask = Scalar::all(0);
imshow( “image“ img );
setMouseCallback( “image“ onMouse 0 );
for(;;)
{
int c = waitKey(0);
if( (char)c == 27 )
break;
if( (char)c == ‘r‘ )
{
markerMask = Scalar::all(0);
img0.copyTo(img);
imshow( “image“ img );
}
if( (char)c == ‘w‘ || (char)c == ‘ ‘ )
{
int i j compCount = 0;
vector > contours;
vector hierarchy;
findContours(markerMask contours hierarchy CV_RETR_CCOMP CV_CHAIN_APPROX_SIMPLE);
if( contours.empty() )
continue;
Mat markers(markerMask.size() CV_32S);
markers = Scalar::all(0);
int idx = 0;
for( ; idx >= 0; idx = hierarchy[idx][0] compCount++ )
drawContours(markers contours idx Scalar::all(compCount+1) -1 8 hierarchy INT_MAX);
if( compCount == 0 )
continue;
vect
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 4260 2016-04-28 14:37 watershed\watershed\watershed.cpp
文件 4832 2016-04-28 14:37 watershed\watershed\watershed.vcxproj
文件 947 2016-04-28 14:37 watershed\watershed\watershed.vcxproj.filters
文件 143 2016-04-28 14:35 watershed\watershed\watershed.vcxproj.user
文件 894 2016-04-28 14:35 watershed\watershed.sln
目录 0 2016-04-28 16:32 watershed\watershed
目录 0 2016-04-28 14:40 watershed
----------- --------- ---------- ----- ----
11076 7
相关资源
- QT+opencv+OCR 身份证号码,银行卡号识别
- opencv视频特定颜色区域识别
- 把RGB转换为HSV和HSI然后根据黄色和蓝
- opencv视觉测距
- 基于Qt和opencv的身份证号码识别系统
- opencv_ffmpeg249.dll
- SfM稀疏三维点云重建--完整工程文件
- 基于opencv的数人头程序源代码
- 利用OpenCV中的Stitcher类实现全景图像拼
- opencv实现的sift算法源码,包含了图像
- openCV 上的小波变换
- 基于OPENCV的车牌识别系统设计
- 617张国内车牌60-17bmp图片用于OpenCV正样
- hog特征提取,c版本代码
- 基于Qt5.8+OpenCV3.2的Basler多相机触发开
- 基于Opencv实现的图像纠偏
- ImageWatch2019.vsix
- SIFT特征提取+匹配
- 基于SIFT算法的图像拼接.rar
- opencv4.1.1+contrib完整版.zip
- OpenCV3.3+contrib-master,VS2013编译后的l
- OpenCV2.4.10官方完整版
- 基于OpenCV3.0的手势识别.rar
- 基于opencv的车牌识别源码
- Firmware_F103 V1.3.rar
- VS2017+opencv写的火焰检测带检测视频哦
- opencv图像处理方法总结.pdf
- bumblebee双目标定/伪彩色深度图/鼠标测
- OpenCV中对图片进行灰度处理
- 车牌识别系统原创+详细注释版+少函数
评论
共有 条评论