segmentation.cpp
1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <opencv2/imgproc/imgproc.hpp>
#include "openbr_internal.h"
#include "openbr/core/opencvutils.h"
using namespace cv;
namespace br
{
/*!
* \ingroup transforms
* \brief Applies watershed segmentation.
* \author Austin Blanton \cite imaus10
*/
class WatershedSegmentationTransform : public UntrainableTransform
{
Q_OBJECT
void project(const Template &src, Template &dst) const
{
dst = src;
Mat mod;
// adaptiveThreshold(src.m(), src.m(), 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, 33, 5);
threshold(src.m(), mod, 0, 255, THRESH_BINARY+THRESH_OTSU);
// findContours requires an 8-bit 1-channel image
// and modifies its source image
if (mod.depth() != CV_8U) OpenCVUtils::cvtUChar(mod, mod);
if (mod.channels() != 1) OpenCVUtils::cvtGray(mod, mod);
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours(mod, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
// draw the contour delineations as 1,2,3... for input to watershed
Mat markers = Mat::zeros(mod.size(), CV_32S);
int compCount=0;
for (int idx=0; idx>=0; idx=hierarchy[idx][0], compCount++) {
drawContours(markers, contours, idx, Scalar::all(compCount+1), -1, 8, hierarchy, INT_MAX);
}
Mat orig = src.m();
// watershed requires a 3-channel 8-bit image
if (orig.channels() == 1) cvtColor(orig, orig, CV_GRAY2BGR);
watershed(orig, markers);
dst.file.set("SegmentsMask", QVariant::fromValue(markers));
dst.file.set("NumSegments", compCount);
}
};
BR_REGISTER(Transform, WatershedSegmentationTransform)
} // namespace br
#include "segmentation.moc"