当前位置: 首页 > news >正文

高端汽车网站建设公司网站模版

高端汽车网站建设,公司网站模版,乐东黎族自治县住房建设局网站,网站建设太金手指六六二八原理介绍 前面写了OTSU算法、最大熵算法、自适应阈值法、基于区域生长算法。他们都有各自的优缺点,而分水岭算法具有它们都具有的优势,所以通常能够产生更加稳健的分割效果。 分水岭算法(watershed)是一种比较基本的数学形态学分割算法,其基…

原理介绍

前面写了OTSU算法、最大熵算法、自适应阈值法、基于区域生长算法。他们都有各自的优缺点,而分水岭算法具有它们都具有的优势,所以通常能够产生更加稳健的分割效果。

分水岭算法(watershed)是一种比较基本的数学形态学分割算法,其基本思想是将灰度图像转换为梯度图像,将梯度值看作高低起伏的山岭,将局部极小值及其邻域看作一个“集水盆”。设想一个个“集水盆”中存在积水,且水位不断升高,淹没梯度较低的地方,当水漫过程停止后,就找出了分割线,图像也就可以被分割成几块连通区域。这里用动图演示一下:

                                   lpe1 (1)  ima3 (1)

所以寻找局部极小值是算法的关键,但在真实图像中,由于噪声点或者其它干扰因素的存在,使用分水岭算法常常存在过度分割的现象,这是因为很多很小的局部极值点的存在,比如下面的图像,这样的分割效果是毫无用处的:

                                       

为了解决过度分割的问题,可以使用基于标记(mark)图像的分水岭算法,就是通过先验知识,来指导分水岭算法,以便获得更好的图像分段效果。通常的mark图像,都是在某个区域定义了一些灰度层级,在这个区域的洪水淹没过程中,水平面都是从定义的高度开始的,这样可以避免一些很小的噪声极值区域的分割。

标记(mark):可以通过人工交互标记,也可以通过计算。下面展示了采用基于mark的分水岭算法过程:第一幅图红色区域就是标记的灰度级。

           ima4  lpe2  ima5 

通过标记,我们可以得到很好得分割效果:

                                           

OpenCV接口: 

void watershed( InputArray image, InputOutputArray markers ); 

输入图像必须是3通道的RGB图像,最为关键的是第二个参数markers,这个参数包含不同区域的轮廓,每个轮廓有一个自己唯一的编号,轮廓的定位可以通过Opencv中findContours方法实现。然后分水岭算法会根据markers传入的标记作为种子(也就是所谓的注水点、标记点),对图像上其他的像素点根据分水岭算法规则进行判断,并对每个像素点的区域归属进行划定,直到处理完图像上所有像素点。而区域与区域之间的分界处的值被置为“-1”,以做区分。

OpenCV的官方示例:https://docs.opencv.org/3.4.0/d8/da9/watershed_8cpp-example.html

 

示例代码

#include <opencv2/core/utility.hpp>
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include <cstdio>
#include <iostream>
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 ../data/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 == EVENT_LBUTTONUP || !(flags & EVENT_FLAG_LBUTTON))prevPt = Point(-1, -1);else if (event == EVENT_LBUTTONDOWN)prevPt = Point(x, y);else if (event == EVENT_MOUSEMOVE && (flags & EVENT_FLAG_LBUTTON)){Point pt(x, y);if (prevPt.x < 0)prevPt = pt;line(markerMask, prevPt, pt, Scalar::all(255), 1, 8, 0);line(img, prevPt, pt, Scalar::all(255), 1, 8, 0);prevPt = pt;imshow("image", img);}
}
int main(int argc, char** argv)
{cv::CommandLineParser parser(argc, argv, "{help h | | }{ @input | I:/Learning-and-Practice/2019Change/Image process algorithm/Img/lung2.jpeg | }");if (parser.has("help")){help();return 0;}string filename = parser.get<string>("@input");Mat img0 = imread(filename, 1), imgGray;if (img0.empty()){cout << "Couldn'g open image " << filename << ". Usage: watershed <image_name>\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 (;;){char c = (char)waitKey(0);if (c == 27)break;if (c == 'r'){markerMask = Scalar::all(0);img0.copyTo(img);imshow("image", img);}if (c == 'w' || c == ' '){int i, j, compCount = 0;vector<vector<Point> > contours;vector<Vec4i> hierarchy;findContours(markerMask, contours, hierarchy, RETR_CCOMP, 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;vector<Vec3b> colorTab;for (i = 0; i < compCount; i++){int b = theRNG().uniform(0, 255);int g = theRNG().uniform(0, 255);int r = theRNG().uniform(0, 255);colorTab.push_back(Vec3b((uchar)b, (uchar)g, (uchar)r));}double t = (double)getTickCount();Mat tmp = Mat::zeros(markers.size(), CV_8U);markers.convertTo(tmp, CV_8U, 255);imshow("markers",tmp);watershed(img0, markers);t = (double)getTickCount() - t;printf("execution time = %gms\n", t*1000. / getTickFrequency());Mat wshed(markers.size(), CV_8UC3);// paint the watershed imagefor (i = 0; i < markers.rows; i++)for (j = 0; j < markers.cols; j++){int index = markers.at<int>(i, j);if (index == -1)wshed.at<Vec3b>(i, j) = Vec3b(255, 255, 255); //边界else if (index <= 0 || index > compCount)wshed.at<Vec3b>(i, j) = Vec3b(0, 0, 0);elsewshed.at<Vec3b>(i, j) = colorTab[index - 1];}wshed = wshed*0.5 + imgGray*0.5;imshow("watershed transform", wshed);}}return 0;
}

效果

   

                                  标记                                               原图+标记                                                         分割效果

       

 

参考: 

http://www.cnblogs.com/mikewolf2002/p/3304118.html

https://www.cnblogs.com/zyly/p/9392881.html#_label2

http://m.itboth.com/d/6Zv2ue/opencv-c++

https://blog.csdn.net/just_sort/article/details/87376355

https://docs.opencv.org/3.4.0/d8/da9/watershed_8cpp-example.html

http://www.tj-hxxt.cn/news/49353.html

相关文章:

  • 各大搜索引擎提交网站入口大全网络营销策略有哪五种
  • 多个网站域名 是新增接入他达拉非
  • 唐山网站从哪里找口碑营销
  • 四川聚锋建设工程有限公司官方网站四川seo整站优化
  • 羊肉口报关做网站百度推广登录入口登录
  • 吉林企业建站系统费用seo关键词怎么优化
  • 外贸福步论坛官网佛山seo优化外包
  • 南阳网站推广价格手机网站关键词seo
  • 网上书城网站开发意义手机关键词seo排名优化
  • 郑州哪里做网站最好工作手机
  • 男同志做爰网站参考网是合法网站吗?
  • 上海猎头公司招聘信息网络营销优化推广
  • 许昌做网站优化如何免费推广自己的网站
  • 学做淘宝店的网站吗电商seo是什么意思啊
  • 上海网站建设哪家公司好最近社会热点新闻事件
  • 网站导航为什么用ul列表做宁波seo整体优化公司
  • 网站域名会赠送几个邮箱在线教育
  • 网站建设模块北京网站优化指导
  • 那里可以做旅游网站的吗什么是网站seo
  • 建设网站和app百度公司介绍
  • html5 mysql 网站开发免费自学电商教程
  • 搭建网站代码青岛网站排名推广
  • 如何做网站编辑 沒技术移动优化课主讲:夫唯老师
  • 珠海医疗网站建设公司排名关键词推广
  • 宁夏免费建个人网站网站是怎么做的
  • wordpress 发布文章插件seo 重庆
  • 手机如何搭建网站站长域名查询工具
  • 天元建设集团有限公司是国企吗优化设计全部答案
  • 怎么在网站做浮动图标上海seo网站推广公司
  • 营销型网站方案ppt模板做网络推广的公司