코드:

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
#include <iostream>
#include <opencv2/opencv.hpp>
 
using namespace std
using namespace cv;
void detectHScolor(const cv::Mat&  image, double minHue,
    double maxHue, double minSat, double  maxSat, Mat&  mask);
 
int main() {
    Mat image = imread("girl.jpg");
    Mat mask;
    detectHScolor(image, 1601025166, mask);
    Mat detected(image.size(), CV_8UC3, Scalar(000));//하얗게 주려면 255,255,255
    image.copyTo(detected, mask);
    imshow("Girl", image);
    imshow("Detected", detected);
    waitKey(0);
}
 
 
 
void detectHScolor(const Mat&  image, double minHue, double maxHue, double minSat, double  maxSat, Mat&  mask) {
    //detect by hue(minHue~maxHue), saturate(minSat~maxSat) 
    Mat hsv;
    cvtColor(image, hsv, COLOR_BGR2HSV); //conver BGR to HSV
 
    vector<Mat>  channels;
    split(hsv, channels);    //split channels
    double minVal = 0, maxVal = 0;
    minMaxLoc(channels[0], &minVal, &maxVal, 00);
    cout << "min= " << minVal << " max= " << maxVal << endl//min=0, max=179
    Mat  mask1;   //maxHue 이하는 255, 이상은 0
    threshold(channels[0], mask1, maxHue, 255, THRESH_BINARY_INV);
    Mat  mask2; //minHue 이하는 0, 이상은 255   
    threshold(channels[0], mask2, minHue, 255, THRESH_BINARY);
    Mat hueMask;
    if (minHue < maxHue)  hueMask = mask1 & mask2;
    else hueMask = mask1 | mask2;
    //색상 범위가 0을 포함할 때 즉 160 ~ 10 사이 색상
 
    //채도 마스크
    threshold(channels[1], mask1, maxSat, 255, THRESH_BINARY_INV);
    threshold(channels[1], mask2, minSat, 255, THRESH_BINARY);
 
    Mat satMask;
    satMask = mask1 & mask2;
    //0와 255로 구성된 mask: 255인 곳이 얼굴 영역: 이영역만 원본 영상에서 copy
    mask = hueMask & satMask;
}
 
cs
실행결과:

 

설명:

 

 

 

 

 

'C,C++ > Opencv, c++' 카테고리의 다른 글

Opencv, c++, cvtColor(), split(), merge()  (0) 2019.10.04
Opencv, c++, imshow, imwrite  (0) 2019.10.04
Opencv, c++, video 출력  (0) 2019.09.22
Opencv, c++, Rectangle 출력  (0) 2019.09.22
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
#include <stdio.h>
 
int number = 9;
int heap[9= { 7,6,5,8,3,5,9,1,6 };
 
int main() {
    //힙을 구성
    for (int i = 1; i < number; i++) {
        int c = i;
        do {
            int root = (c - 1/ 2;
            if (heap[root] < heap[c]) {
                int temp = heap[root];
                heap[root] = heap[c];
                heap[c] = temp;
            }
            c = root;
        } while (c != 0);
    }
    //크기를 줄여가며 반복적으로 힙을 구성
    for (int i = number - 1; i >= 0; i--) {
        int temp = heap[0];
        heap[0= heap[i];
        heap[i] = temp;
        int root = 0;
        int c = 1;
        do {
            c = 2 * root + 1;
            //자식 중에 더 큰 값을 찾기
            if (c < i - 1 && heap[c] < heap[c + 1]) {
                c++;
            }
            //루트보다 자식이 크다면 교환
            if (c < i && heap[root] < heap[c]) {
                temp = heap[root];
                heap[root] = heap[c];
                heap[c] = temp;
            }
            root = c;
        } while (c < i);
    }
    //결과 출력
    for (int i = 0; i < number; i++) {
        printf("%d ", heap[i]);
    }
}
cs

 

'C,C++ > Algorithm' 카테고리의 다른 글

Algorithm, c++, Sort  (0) 2019.09.23
Algorithm, c++, BFS와 DFS 구현  (0) 2019.09.23
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
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
 
using namespace std;
 
bool compare(pair<intpair<stringint>> v1, pair<intpair<stringint>> v2) {
    if (v1.second.second != v2.second.second) {
        return v1.second.second > v2.second.second;
    }
    else {
        return v1.first < v2.first;
    }
}
 
int main() {
    vector<pair<intpair<stringint>>> v;
 
 
    for (int i = 0; i < 5; i++) {
        string name;
        int score;
        cin >> name >> score;
        v.push_back(pair<intpair<stringint>>(i, make_pair(name, score)));
    }
 
    sort(v.begin(), v.end(), compare);
 
    for (int i = 0; i < v.size(); i++) {
        cout << v[i].second.first << endl;
    }
}
cs

 

'C,C++ > Algorithm' 카테고리의 다른 글

Algorithm, c++, Heap sort  (0) 2019.09.23
Algorithm, c++, BFS와 DFS 구현  (0) 2019.09.23
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
#include <iostream>
#include <queue>
#include <stack>
 
using namespace std;
 
int number = 7;
bool c[7];
vector<int> a[8];
 
void bfs(int start) {
    queue<int> q;
    q.push(start);
    c[start] = true;
    while (!q.empty()) {
        int x = q.front();
        q.pop();
        cout << x << ' ';
        for (int i = 0; i < a[x].size(); i++) {
            int y = a[x][i];
            if (!c[y]) {
                q.push(y);
                c[y] = true;
            }
        }
    }
}
 
void dfs(int start) {
    if (c[start]) return;
    c[start] = true;
    cout << start << ' ';
    for (int i = 0; i < a[start].size(); i++) {
        int y = a[start][i];
        dfs(y);
    }
}
 
void linkEdge(int x, int y) {
    a[x].push_back(y);
    a[y].push_back(x);
}
 
int main() {
    linkEdge(12);
    linkEdge(13);
    linkEdge(23);
    linkEdge(24);
    linkEdge(25);
    linkEdge(45);
    linkEdge(36);
    linkEdge(37);
    linkEdge(67);
 
 
    dfs(1);
 
    return 0;
}
cs

bfs 출력 결과 :

1 2 3 4 5 6 7 

 

dfs 출력 결과 :

1 2 3 6 7 4 5

 

'C,C++ > Algorithm' 카테고리의 다른 글

Algorithm, c++, Heap sort  (0) 2019.09.23
Algorithm, c++, Sort  (0) 2019.09.23

+ Recent posts