/
ofApp.cpp
152 lines (101 loc) · 3.48 KB
/
ofApp.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
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
#include "ofApp.h"
bool compare( const ofVec2f& a, const ofVec2f& b) {
return a.y < b.y;
}
//--------------------------------------------------------------
void ofApp::setup(){
initImg.loadImage("25.jpg");
initImg.setImageType(OF_IMAGE_GRAYSCALE);
threshImg.allocate(initImg.getWidth(), initImg.getHeight(), OF_IMAGE_GRAYSCALE);
unsigned char * pixinit = initImg.getPixels();
unsigned char * pixthresh = threshImg.getPixels();
blackCount = 0;
totalCount = 0;
pixpos.set(0,0);
int xTotal = 0;
int yTotal = 0;
xAvg = 0;
yAvg = 0;
// for (int i = 0; i < initImg.getWidth() * initImg.getHeight(); i++){
//
// totalCount++;
//
// if (pixinit[i] > 200){
// pixthresh[i] = 255;
// } else {
// blackCount++;
// pixthresh[i] = 0;
// }
//
// }
for (int i = 0; i < initImg.getWidth(); i++){
for (int j = 0; j < initImg.getHeight(); j++){
pixpos.x = i;
pixpos.y = j;
pos = j * initImg.getWidth() + i;
if (pixinit[pos] > 200){
pixthresh[pos] = 255;
} else {
posvec.push_back(pixpos);
pixthresh[pos] = 0;
cout << pixpos << endl;
}
}
}
int pvSize = posvec.size();
for (int k = 0; k < pvSize; k++) {
xTotal += posvec[k].x;
yTotal += posvec[k].y;
}
yAvg = yTotal/pvSize;
xAvg = xTotal/pvSize;
minX = posvec[0].x;
maxX = posvec[pvSize-1].x;
ofSort(posvec, compare);
minY = posvec[0].y;
maxY = posvec[pvSize-1].y;
cout << "xMin: " << minX << " xMax: " << maxX << " yMin: " << minY << " yMax: " << maxY << endl;
cout << initImg.getWidth() + 20 + minX << " " << minY << " " << maxX - minX << " " << maxY - minY << endl;
threshImg.update();
}
//--------------------------------------------------------------
void ofApp::update(){
}
//--------------------------------------------------------------
void ofApp::draw(){
ofSetColor(255,255,255);
initImg.draw(0,0);
threshImg.draw(initImg.getWidth()+20, 0);
ofSetColor(0,0,255);
ofCircle(xAvg + initImg.getWidth(), yAvg, 14);
ofNoFill();
ofSetColor(255, 0, 0);//stroke color
ofRect(initImg.getWidth() + 20 + minX, minY, maxX - minX, maxY - minY);
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
}
//--------------------------------------------------------------
void ofApp::keyReleased(int key){
}
//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){
}
//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){
}