-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaskoperators.cpp
85 lines (68 loc) · 2.13 KB
/
maskoperators.cpp
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
* Author: Artur Dobrogowski
* 2020-01-04
*/
#include "maskoperators.h"
#include "segmentfiller.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>
int separate_segments(const cv::Mat &src, std::vector<Segment> &container, int tresh) {
int nRows = src.rows;
int nCols = src.cols;
int discarded_tresh = 0;
unsigned char *pvis;
cv::Mat visited = src.clone();
pvis = visited.ptr<unsigned char>(0);
assert( src.isContinuous() );
assert( visited.isContinuous() );
for(int i = 0; i < nRows; ++i) {
for (int j = 0; j < nCols; ++j) {
if(*(pvis + i*nCols + j) > 0) {
Segment s = Segment(src, j, i);
SegmentFiller sfm = SegmentFiller(visited, s);
sfm(j, i);
if (s.getArea() >= tresh) {
container.push_back(s);
}
else
discarded_tresh++;
}
}
}
return discarded_tresh;
}
cv::Mat ups_segmentate(cv::Mat src) {
//void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0 )
cv::Mat hsvsrc(src.rows, src.cols, CV_8UC3);
// Conversion to HSV
cv::cvtColor(src, hsvsrc, cv::COLOR_BGR2HSV);
cv::Mat res(src.rows, src.cols, CV_8UC1);
CV_Assert(src.depth() == CV_8U);
int channels = hsvsrc.channels();
int nRows = hsvsrc.rows;
int nCols = hsvsrc.cols;
if (hsvsrc.isContinuous()) {
nCols *= nRows;
nRows = 1;
}
uchar* p;
unsigned char* t;
for(int i = 0; i < nRows; ++i) {
p = hsvsrc.ptr<uchar>(i);
t = res.ptr<unsigned char>(i);
for (int j = 0; j < nCols; ++j) {
//H +0
//S +1
//V +2
// Segmentation color condition
if (p[j*3] >= 9 && p[j*3] <= 28 // Yellow-Orange Hue
&& p[j*3+1] > 40 // Quite saturated
&& p[j*3+2] > 130) // Bright color
t[j] = 255;
else
t[j] = 0;
}
}
return res;
}