資源簡介
基于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“?<
????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?“?<\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
評論
共有 條評論