Guest User

Untitled

a guest
Dec 7th, 2012
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.82 KB | None | 0 0
  1. #include "testApp.h"
  2.  
  3. using namespace ofxCv;
  4. using namespace cv;
  5.  
  6. //-----------------------INITIALISING--------------//
  7. void testApp::setup(){
  8. // ofSetVerticalSync(true);
  9.  
  10. camWidth = 640; // try to grab at this size.
  11. camHeight = 480;
  12.  
  13. Adam = pic = cCirc = false;
  14.  
  15. vidGrabber.setVerbose(true);
  16. vidGrabber.initGrabber(camWidth,camHeight);
  17.  
  18. colorImg = new unsigned char[camWidth*camHeight*3];
  19. videoTexture.allocate(camWidth,camHeight, GL_RGB);
  20.  
  21. origImg.loadImage("display.jpg");
  22. //warpImg.loadImage("display.jpg");
  23. origPoints.push_back(ofVec2f(0,0)); // blue
  24. origPoints.push_back(ofVec2f(origImg.getWidth(),0)); // red
  25. origPoints.push_back(ofVec2f(0,origImg.getHeight())); // yellow
  26. origPoints.push_back(ofVec2f(origImg.getWidth(),origImg.getHeight())); // green
  27.  
  28.  
  29. imitate(warpImg, origImg); // this isn't really doing anything as of right now, I believe.
  30. }
  31. //--------------------UPDATE FUNCTION------------------//
  32. void testApp::update(){
  33.  
  34. vidGrabber.grabFrame();
  35.  
  36.  
  37. if (vidGrabber.isFrameNew()){
  38.  
  39. //int totalPixels = camWidth*camHeight*3;
  40. unsigned char * pixels = vidGrabber.getPixels();
  41.  
  42. for (int y = 0; y < camHeight; y++)
  43. for (int x = 0; x < camWidth; x++)
  44. for (int c = 0; c < 3; c++)
  45. colorImg[(y*camWidth + x)*3+c] = pixels[(y*camWidth + x)*3+c];
  46.  
  47. // this code is enabling our webcam feed on the OF canvas
  48.  
  49.  
  50. videoTexture.loadData(colorImg, camWidth,camHeight, GL_RGB);
  51. }
  52. }
  53. //------------------------Draw function-----------------------------//
  54. void testApp::draw(){
  55. ofSetColor(255,255,255);
  56. videoTexture.draw(0,0,camWidth,camHeight);
  57.  
  58. if (Adam) {
  59. trackRect(); // runs the function that tracks the colors
  60. ofNoFill();
  61. ofSetLineWidth(4);
  62.  
  63. if (cCirc) { //if statement that controls whether or not the circles and the white shape should be drawn
  64. ofSetColor(0, 0, 200);
  65. ofCircle(pos.x, pos.y, 40);
  66.  
  67. ofSetColor(200, 0, 0);
  68. ofCircle(pos2.x, pos2.y, 40);
  69.  
  70. ofSetColor(200,200,0);
  71. ofCircle(pos3.x, pos3.y, 40);
  72.  
  73. ofSetColor(0,200,0);
  74. ofCircle(pos4.x, pos4.y, 40);
  75.  
  76. //
  77. // //_---------------------//
  78. ofSetColor(255);
  79. ofSetPolyMode(OF_POLY_WINDING_NONZERO);
  80. ofBeginShape();
  81. ofVertex(pos.x, pos.y); // blue
  82. ofVertex(pos2.x, pos2.y); // red
  83. ofVertex(pos4.x, pos4.y); // yellow
  84. ofVertex(pos3.x, pos3.y); // green
  85. ofEndShape();
  86. } // end of cCirc
  87. //
  88. // //--------------------//
  89. ofSetColor(255);
  90. if( pos.x>10 && pos.y > 10 && pic)
  91. //ofEnableBlendMode(OF_BLENDMODE_MULTIPLY);
  92. warpImg.draw(pos.x, pos.y, 250, 250);
  93.  
  94.  
  95. //--------------------//
  96.  
  97.  
  98. }
  99. }
  100.  
  101. void testApp::keyPressed(int key) // keypressed function that allows us to show different states, such as the circles, picture and rect.
  102. {
  103. if ( key == 'g' || key == 'G'){
  104. ValleRect = Adam = true;
  105. }
  106. if ( key == 'p' || key == 'P'){
  107. pic = Adam = true;
  108. }
  109. if ( key == 'v' || key == 'V'){
  110. cCirc = Adam = true;
  111. }
  112.  
  113. if ( key == 'c' || key == 'C'){
  114. pic = Adam = cCirc = false;
  115. }
  116.  
  117. }
  118.  
  119.  
  120.  
  121. void testApp::trackRect() { //the function that tracks the colors
  122.  
  123. pos=ofPoint(0,0);
  124. int n=0;
  125. pos2=ofPoint(0,0);
  126. int n2=0;
  127. pos3=ofPoint(0,0);
  128. int n3=0;
  129. pos4=ofPoint(0,0);
  130. int n4=0;
  131. //the above are resetting the points and counters for each color every time it runs through.
  132.  
  133.  
  134. unsigned char * pixels = vidGrabber.getPixels();
  135.  
  136. for (int y= 0; y < camHeight; y++) {
  137. for (int x= 0; x < camWidth; x++) {
  138.  
  139. r = pixels[(y*camWidth+x)*3 + 0];
  140. g = pixels[(y*camWidth+x)*3 + 1];
  141. b = pixels[(y*camWidth+x)*3 + 2];
  142.  
  143. result = (b> g+30 && b> r+50 )? 255: 0; // blue
  144. result2 = (r> g+50 && r> b+30 )? 255: 0; // red
  145. result3 = (r> b+50 && g> b+50 )? 255: 0; // yellow
  146. result4 = (g> r+20 && g> b+40 )? 255: 0; // green
  147.  
  148. if (result==255){
  149. pos.x+=x; // adds the x position to the pos.x
  150. pos.y+=y; // adds the y position to the pos.y
  151. n++; // increases the counter by 1 for each runthrough
  152. }
  153. if (result2==255){
  154. pos2.x+=x;
  155. pos2.y+=y;
  156. n2++;
  157. }
  158. if (result3==255){
  159. pos3.x+=x;
  160. pos3.y+=y;
  161. n3++;
  162. }
  163. if (result4==255){
  164. pos4.x+=x;
  165. pos4.y+=y;
  166. n4++;
  167. }
  168. }
  169. }
  170. if (n>0)
  171. {
  172. pos.x/=n; // divides the pos.x with our counter in order to find the middle of the color field
  173. pos.y/=n; // divides the pos.y with our counter in order to find the middle of the color field
  174. }
  175. if (n2>0)
  176. {
  177. pos2.x/=n2;
  178. pos2.y/=n2;
  179. }
  180. if (n3>0)
  181. {
  182. pos3.x/=n3;
  183. pos3.y/=n3;
  184. }
  185. if (n4>0)
  186. {
  187. pos4.x/=n4;
  188. pos4.y/=n4;
  189. }
  190.  
  191.  
  192. // if(leftPoints.size() >= 4) {
  193. // vector<Point2f> srcPoints, dstPoints;
  194. // for(int i = 0; i < leftPoints.size(); i++) {
  195. // srcPoints.push_back(Point2f(rightPoints[i].x - left.getWidth(), rightPoints[i].y));
  196. // dstPoints.push_back(Point2f(leftPoints[i].x, leftPoints[i].y));
  197. // }
  198. //----------------------------------------------//
  199. if (!(origPoints.size() < 4))
  200. {
  201. warpPoints.push_back(ofVec2f(pos.x,pos.y)); // blue
  202. warpPoints.push_back(ofVec2f(pos2.x,pos2.y)); // red
  203. warpPoints.push_back(ofVec2f(pos3.x,pos3.y)); // yellow
  204. warpPoints.push_back(ofVec2f(pos4.x,pos4.y)); // green
  205. vector<Point2f> srcPoints, dstPoints;
  206.  
  207. for(n = 0; n < 4; n++)
  208. {
  209. srcPoints.push_back(Point2f(origPoints[n].x,origPoints[n].y));
  210. dstPoints.push_back(Point2f(warpPoints[n].x,warpPoints[n].y));
  211. }
  212.  
  213. homography = findHomography(Mat(dstPoints), Mat(srcPoints));
  214. homographyReady = true;
  215.  
  216. }
  217.  
  218. if (homographyReady) {
  219. // this is how you warp one ofImage into another ofImage given the homography matrix
  220. // CV INTER NN is 113 fps, CV_INTER_LINEAR is 93 fps
  221. // warpPerspective(right, warpedColor, homography, CV_INTER_LINEAR);
  222. warpPerspective(origImg, warpImg, homography, CV_INTER_LINEAR);
  223. warpImg.update();
  224. }
  225. videoTexture.loadData(pixels, camWidth,camHeight, GL_RGB); // loads the pixels (original input) in to our texture
  226. }
Advertisement
Add Comment
Please, Sign In to add comment