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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import processing.video.*;
 
int stat;
int btn = 0;
int btn2 = 0;
int btn3 = 0;
float nbsum = 0;
float pbsum = 0;
float nbsum2 = 0;
float pbsum2 = 0;
float nbsum3 = 0;
float pbsum3 = 0;
 
ParticleSystem ps;
Capture video;
PImage img, dback;
double sum4;
 
void setup() {
  size(600480);
  video = new Capture(this, 64048030);
  video.start();
 
  img = createImage(640480, RGB);
  dback = createImage(640480, GRAY);
  background(0);
  
  ps = new ParticleSystem(new PVector(width/2-1500));
}
 
 
void draw() {
  if(video.available()){
    video.read();
  }
 
  video.loadPixels();
  img.loadPixels();
  for(int x = 50; x < 630; x++){
    for(int y = 0; y < 480; y++){
      img.pixels[y*img.width +(640-x)] = video.pixels[y*img.width + x];
    }
  }
  img.filter(GRAY);
  checkInterection2();
  dithering();
  checkInterection();
  if(btn == 1){
    ps.addParticle();
    ps.run();
  }
  
  image(img, 0480);
  //rect(300, 0, 20, 20);
}
 
void checkInterection(){
  img.loadPixels();
  nbsum = 0;
  for(int dx = 0; dx < 20; dx++){
    for(int dy = 0; dy < 20; dy++){
      nbsum = nbsum + brightness(img.pixels[(dy)*img.width + (500+dx)]);
    }
  }
 
  if(abs(nbsum - pbsum) > 6000){
    if(btn == 0){btn = 1;}
    else if(btn == 1){btn = 0;}
  }
  //text("brightness difference : " + abs(nbsum - pbsum), 10, 480);
  
  pbsum = nbsum;
}
 
void checkInterection2(){
  img.loadPixels();
  nbsum2 = 0;
  for(int dx = 0; dx < 20; dx++){
    for(int dy = 0; dy < 20; dy++){
      nbsum2 = nbsum2 + brightness(img.pixels[(dy)*img.width + (100+dx)]);
    }
  }
  
  if(abs(nbsum2 - pbsum2) > 10000){
    if(btn2 == 0){if(btn3==1){btn2 = 2;}else{btn2 = 1;}}
    else if(btn2 == 1){btn2 = 2;}
    else if(btn2 == 2){btn2 = 3;}
    else if(btn2 == 3){btn2 = 4;}
    else if(btn2 == 4){btn2 = 0;}
  }
  //text("brightness difference : " + abs(nbsum2 - pbsum2), 10, 480);
  
  pbsum2 = nbsum2;
}
 
void backbtn(){
  img.loadPixels();
  nbsum3 = 0;
  for(int dx = 0; dx < 20; dx++){
    for(int dy = 0; dy < 20; dy++){
      nbsum3 = nbsum3 + brightness(img.pixels[(dy)*img.width + (500+dx)]);
    }
  }
  
  if(abs(nbsum3 - pbsum3) > 10000){
    if(btn3 == 0){btn3 = 1;}
    else if(btn3 == 1){btn3 = 2;}
    else if(btn3 == 2){btn3 = 3;}
    else if(btn3 == 3){btn3 = 4;}
    else if(btn3 == 4){btn3 = 0;}
  }
  text("brightness difference : " + abs(nbsum3 - pbsum3), 10480);
  
  pbsum3 = nbsum3;
}
 
 
void dithering(){
  stat = 1;
  
  img.loadPixels();
  if(btn2 == 0){background(0);}
  else if(btn2 == 1){background(#25DBD6);}
  else if(btn2 == 2){background(#0130F7);}
  else if(btn2 == 3){background(#C03A1A);}
  else if(btn2 == 4){background(#8A3C86);}
  for(int x = 5; x < 635; x+=10){
    for(int y = 5; y < 475; y+=10){
      sum4 = 0;
      for(int a = -5; a<5; a++){
        for(int b = -5; b<5; b++){
          sum4 = sum4 + brightness(img.pixels[(y+b)*img.width + (x+a)]);
        }
      }     
 
      if (sum4 > 25000){
        ellipse(x, y, 1010);
        noStroke();
      }else if ((20000<sum4)&&(sum4<25000)){
        ellipse(x, y, 88);
        noStroke();
      }else if ((15000<sum4)&&(sum4<20000)){
        ellipse(x, y, 66);
        noStroke();
      }else if ((10000<sum4)&&(sum4<15000)){
        ellipse(x, y, 44);
        noStroke();
      }else if ((5000<sum4)&&(sum4<10000)){
        ellipse(x, y, 22);
        noStroke();
      }
      if(btn2 == 0){
        int crand = int(random(1,4));
        if (crand == 1){fill(#DA2429);}
        else if (crand == 2){fill(#FECF08);}
        else if (crand == 3){fill(#3FC5E5);}
        else if (crand == 4){fill(#75C379);}
      }
      else if(btn2 == 1){fill(#DA2429);}
      else if(btn2 == 2){fill(#FECF08);}
      else if(btn2 == 3){fill(#3FC5E5);}
      else if(btn2 == 4){fill(#75C379);}
    }
  }
}
 
 
class ParticleSystem {
  ArrayList<Particle> particles;
  PVector origin;
 
  ParticleSystem(PVector position) {
    origin = position.copy();
    particles = new ArrayList<Particle>();
  }
 
  void addParticle() {
    particles.add(new Particle(origin));
  }
 
  void run() {
    for (int i = particles.size()-1; i >= 0; i--) {
      Particle p = particles.get(i);
      p.run();
      if (p.isDead()) {
        particles.remove(i);
      }
    }
  }
}
 
class Particle {
  PVector position;
  PVector velocity;
  PVector acceleration;
  float lifespan;
 
  Particle(PVector l) {
    acceleration = new PVector(00.05);
    velocity = new PVector(random(-11), random(-20));
    position = l.copy();
    lifespan = 255.0;
  }
 
  void run() {
    update();
    display();
  }
 
  void update() {
    velocity.add(acceleration);
    position.add(velocity);
    lifespan -= 0.4;
  }
 
  void display() {
    //stroke(255, lifespan);
    noStroke();
    int crand = int(random(1,4));
    if (crand == 1){fill(#DA2429, lifespan);}
    else if (crand == 2){fill(#FECF08, lifespan);}
    else if (crand == 3){fill(#3FC5E5, lifespan);}
    else if (crand == 4){fill(#75C379, lifespan);}
    //fill(#854845, lifespan); //change color
    int rands = int(random(8,20));
    ellipse(position.x, position.y, rands, rands);
  }
 
  boolean isDead() {
    if (lifespan < 0.0) {
      return true;
    } else {
      return false;
    }
  }
}
cs



<실행 영상>

'Computer > Coding' 카테고리의 다른 글

[Android] 반투명 액티비티  (0) 2017.05.13
[Android] 피파온라인 선수 검색 예제  (0) 2017.04.12
[JSP] 테니스 토너먼트 웹  (0) 2017.03.30
[opencv] Simple lane detection  (0) 2017.03.30
[opencv] Line matching  (0) 2017.03.30
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <opencv\cv.h>
#include <opencv\highgui.h>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
 
using namespace cv;
using namespace std;
 
Mat grad, src;
Mat edges(600800, CV_8UC1);
char name[100];
 
void main()
 
{
    int gg = 0;
    while (1){
        sprintf(name, "Testvideo/testvideo1/Left_%d.bmp", gg);
        Mat src = imread(name, 1);
        if (!src.data) break// src 이미지 데이터가없으면 종료
 
        edges = Scalar(0);
        
        //소벨 필터
        Mat src_gray;
        Mat abs_grad_x, abs_grad_y;
        int scale = 1;
        int delta = 0;
        int ddepth = CV_16S;
 
        //이미지 로딩
        GaussianBlur(src, src, Size(33), 00, BORDER_DEFAULT);
        cvtColor(src, src_gray, CV_RGB2GRAY);
 
        Mat grad_x, grad_y;
 
        /// Gradient X
        Sobel(src_gray, grad_x, ddepth, 103, scale, delta, BORDER_DEFAULT);
        convertScaleAbs(grad_x, abs_grad_x);
 
        /// Gradient Y
        Sobel(src_gray, grad_y, ddepth, 013, scale, delta, BORDER_DEFAULT);
        convertScaleAbs(grad_y, abs_grad_y);
 
        addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.50, grad);
        
        //오른쪽 엣지 찾기
        vector<Point2i> rPoints;
        for (int y = 430; y < grad.rows; y = y + 5)
        {
            bool i = false;
            uchar* data = grad.ptr<uchar>(y);
            for (int x = 300; x < grad.cols; x++) {
                if (data[x] > 100 && i == false)
                {
                    rectangle(edges, Point(x, y), Point(x - 2, y - 2), Scalar(25500), 2);
                    rPoints.push_back(Point(x, y));
                    i = true;
                }
            }
        }
        //왼쪽 엣지 찾기
        vector<Point2i> lPoints;
        for (int y = 380; y < grad.rows; y = y + 5)
        {
            bool i = false;
            uchar* data = grad.ptr<uchar>(y);
            for (int x = 300; x > 0; x--) {
                if (data[x] > 100 && i == false)
                {
                    rectangle(edges, Point(x, y), Point(x - 2, y - 2), Scalar(25500), 2);
                    lPoints.push_back(Point(x, y));
                    i = true;
                }
            }
        }
 
        
        // 라인 찾기
        vector<Vec2f> lines;
        HoughLines(edges, lines, 1, CV_PI / 18015000);
        int r, l;
        l = 0, r = 0;
        // 라인 그리기
        for (size_t i = 0; i < lines.size(); i++)
        {
            float rho = lines[i][0], theta = lines[i][1];
            Point pt1, pt2;
            double a = cos(theta), b = sin(theta);
 
            if ((a / b) < 0)
            {
                r = i;
            }
            else
            {
                l = i;
                cout << l << endl;
            }
        }
        
        
        float rho = lines[r][0], theta = lines[r][1];
        Point pt1, pt2;
        double a = cos(theta), b = sin(theta);
        double x0 = a*rho, y0 = b*rho;
        pt1.x = cvRound(x0 + 1000 * (-b));
        pt1.y = cvRound(y0 + 1000 * (a));
        pt2.x = cvRound(x0 - 1000 * (-b));
        pt2.y = cvRound(y0 - 1000 * (a));
        
        float rho2 = lines[l][0], theta2 = lines[l][1];
        Point pt3, pt4;
        double a2 = cos(theta2), b2 = sin(theta2);
        double x02 = a2*rho2, y02 = b2*rho2;
        pt3.x = cvRound(x02 + 1000 * (-b2));
        pt3.y = cvRound(y02 + 1000 * (a2));
        pt4.x = cvRound(x02 - 1000 * (-b2));
        pt4.y = cvRound(y02 - 1000 * (a2));
        
        double t;
        double s;
 
        double under = (pt4.y - pt3.y)*(pt2.x - pt1.x) - (pt4.x - pt3.x)*(pt2.y - pt1.y);
 
        double _t = (pt4.x - pt3.x)*(pt1.y - pt3.y) - (pt4.y - pt3.y)*(pt1.x - pt3.x);
        double _s = (pt2.x - pt1.x)*(pt1.y - pt3.y) - (pt4.y - pt1.y)*(pt1.x - pt3.x);
 
 
 
        t = _t / under;
        s = _s / under;
 
 
        double cx = pt1.x + t * (double)(pt2.x - pt1.x);
        double cy = pt1.y + t * (double)(pt2.y - pt1.y);
        Point cp;
        cp.x = cx, cp.y = cy;
 
        line(src, pt2, cp, Scalar(00255), 3, CV_AA);
        line(src, pt3, cp, Scalar(00255), 3, CV_AA);
        rectangle(src, Point(cx, cy), Point(cx - 4, cy - 4), Scalar(25500), 4);
        
 
 
        cv::namedWindow("Detected Lines with Hough");
        cv::imshow("Detected Lines with Hough", src);
 
        gg++;
        waitKey(1);
    }
    
}
cs



<구현 결과 영상>


<구현 과정>

'Computer > Coding' 카테고리의 다른 글

[Android] 반투명 액티비티  (0) 2017.05.13
[Android] 피파온라인 선수 검색 예제  (0) 2017.04.12
[JSP] 테니스 토너먼트 웹  (0) 2017.03.30
[Processing] particle/interaction project  (0) 2017.03.30
[opencv] Line matching  (0) 2017.03.30


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
#include <opencv2/line_descriptor.hpp>
 
#include "opencv2/core/utility.hpp"
#include <opencv2/imgproc.hpp>
#include <opencv2/features2d.hpp>
#include <opencv2/highgui.hpp>
 
#include <iostream>
 
using namespace cv;
 
static const char* keys =
"{@image_path1 | | Image path 1 }"
        "{@image_path2 | | Image path 2 }" };
 
static void help()
{
  std::cout << "\nThis example shows the functionalities of lines extraction " << "and descriptors computation furnished by BinaryDescriptor class\n"
                << "Please, run this sample using a command in the form\n" << "./example_line_descriptor_compute_descriptors <path_to_input_image 1>"
                << "<path_to_input_image 2>" << std::endl;
 
}
 
int main( int argc, char** argv )
{
  /* get parameters from comand line */
  CommandLineParser parser( argc, argv, keys );
  String image_path1 = parser.get<String>0 );
  String image_path2 = parser.get<String>1 );
 
  if( image_path1.empty() || image_path2.empty() )
  {
        help();
        return -1;
  }
 
  /* load image */
  cv::Mat imageMat1 = imread( image_path1, 1 );
  cv::Mat imageMat2 = imread( image_path2, 1 );
 
  waitKey();
  if( imageMat1.data == NULL || imageMat2.data == NULL )
  {
        std::cout << "Error, images could not be loaded. Please, check their path" << std::endl;
  }
 
  /* create binary masks */
  cv::Mat mask1 = Mat::ones( imageMat1.size(), CV_8UC1 );
  cv::Mat mask2 = Mat::ones( imageMat2.size(), CV_8UC1 );
 
  /* create a pointer to a BinaryDescriptor object with default parameters */
  Ptr<BinaryDescriptor> bd = BinaryDescriptor::createBinaryDescriptor();
 
  /* compute lines */
  std::vector<KeyLine> keylines1, keylines2;
  bd->detect( imageMat1, keylines1, mask1 );
  bd->detect( imageMat2, keylines2, mask2 );
 
  /* compute descriptors */
  cv::Mat descr1, descr2;
  bd->compute( imageMat1, keylines1, descr1 );
  bd->compute( imageMat2, keylines2, descr2 );
 
  /* create a BinaryDescriptorMatcher object */
  Ptr<BinaryDescriptorMatcher> bdm = BinaryDescriptorMatcher::createBinaryDescriptorMatcher();
 
  /* require match */
  std::vector<DMatch> matches;
  bdm->match( descr1, descr2, matches );
 
  /* plot matches */
  cv::Mat outImg;
  std::vector<char> mask( matches.size(), 1 );
  drawLineMatches( imageMat1, keylines1, imageMat2, keylines2, matches, outImg, Scalar::all( -1 ), Scalar::all( -1 ), mask,
                       DrawLinesMatchesFlags::DEFAULT );
 
  imshow( "Matches", outImg );
  waitKey();
}
cs

<출처 : http://docs.opencv.org/3.0-beta/modules/line_descriptor/doc/tutorial.html>


이미지1에서 세그먼트된 각각의 라인들에 대한 best match를 이미지2에서 한개씩 찾는다.


opencv에서는 LBD descriptor을 binary descriptor로 바꾸어 매칭을 실시한다.



이 외에 radius match나 knn match를 이용하려면 아래와 같이 수정하면 된다.

1
2
3
4
5
6
7
8
9
10
11
/* prepare a structure to host matches */
std::vector<std::vector<DMatch> > matches;
 
/* require knn match */
bdm->knnMatch( descr1, descr2, matches, 6 );
 
/* prepare a structure to host matches */
std::vector<std::vector<DMatch> > matches;
 
/* compute matches */
bdm->radiusMatch( queries, matches, 30 );
cs
<출처 : http://docs.opencv.org/3.0-beta/modules/line_descriptor/doc/tutorial.html>

하지만 'matches' 벡터가 2d 벡터로 바뀌므로 image plot 부분의 코드 또한 수정이 필요하다.


1
2
3
4
5
std::vector<char> mask( vsize, 1 );
  for(int i = 0; i < matches.size(); i++){
    drawLineMatches( imageMat1, keylines1, imageMat2, keylines2, matches[i], outImg, 
    Scalar::all( -1 ), Scalar::all( -1 ), mask, DrawLinesMatchesFlags::DEFAULT );
  }
cs

위의 코드와 같이 match의 사이즈만큼 반복시켜주면 끝!