Advertisement
Guest User

Untitled

a guest
Sep 19th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 45.50 KB | None | 0 0
  1. #include "ofApp.h"
  2. #define Nw 320
  3. #define Nh 240
  4.  
  5. unsigned char Imin =0;
  6. unsigned char Imax = 255;
  7.  
  8. // Render Size
  9. const int Rw = 320;
  10. const int Rh = 240;
  11.  
  12. const int Gw = 128;
  13. const int Gh = 128;
  14.  
  15. const int Nob=3000;
  16. const int Ntg=3000;
  17.  
  18. int TargInd[Nob];
  19. ofVec2f TheObjects[Nob];
  20. ofVec2f TheVelocities[Nob];
  21.  
  22. const int HashRow = 12;
  23. const int HashColumn = 16;
  24. const float hashdec = 20.0;
  25.  
  26. // target hash
  27. vector <ofVec2f> TargetHash[HashRow][HashColumn];
  28. // Object hash
  29. list <ofVec2f> ObjectHash[HashRow][HashColumn];
  30. // Index hash
  31. list <int> ObjectIndHash[HashRow][HashColumn];
  32.  
  33. // histogram equalization
  34.  
  35. int EquaHist = 0;
  36.  
  37. // image with dither
  38. cv::Mat TheDither;
  39.  
  40.  
  41.  
  42. // gauss texture
  43. ofTexture GaussTexture;
  44. ofTexture GaussTextureDerivX;
  45. ofTexture GaussTextureDerivY;
  46. float var = 48;//168?
  47. float alpha = 0.45;
  48.  
  49. ofImage fondoFinal;
  50. cv::Mat Transf;
  51. int TheInd = 0;
  52. float timeVar = 0.0;
  53. float refTime = 0.0;
  54. float Tmax = 20.0;
  55. //--------------------------------------------------------------
  56. void ofApp::setup(){
  57. vidGrabber.setVerbose(true);
  58. vidGrabber.initGrabber(Nw,Nh);
  59. colorImg.allocate(Nw, Nh);
  60. grayImage.allocate(Nw, Nh);
  61.  
  62. ThDis =13.4;
  63. Ktar = 19.25;
  64. Kdam = 1.05;
  65.  
  66. for(int i =0;i< Nob;i++){
  67. ofVec2f TheValue(ofRandom(Nw),
  68. ofRandom(Nh));
  69. TheObjects[i] = TheValue;
  70. TargInd[i] = -1;
  71. TheVelocities[i].set(0.0,0.0);
  72. }
  73. G_alpha = 0.215;
  74. metaballs.load("shaders/Ballsshad150.vert", "shaders/Ballsshad150.frag");
  75. postProShader.load("shaders/PostProshad150.vs", "shaders/PostProshad150.fs");
  76. gaussSprite.load("shaders/gaussShader.vs", "shaders/gaussShader.fs");
  77. gaussSpriteDerivative.load("shaders/gaussShader.vs", "shaders/gaussShaderDeriv.fs");
  78. materialShader.load("shaders/material.vert", "shaders/material.frag");
  79.  
  80. ofDisableArbTex();
  81. // ofLoadImage(GaussTexture, "images/gauss1.png");
  82.  
  83. theFrame.allocate(Rw, Rh,GL_RGBA32F);
  84. theFrameAux.allocate(Rw, Rh,GL_RGBA32F);
  85. PorstPro.allocate(Rw, Rh,GL_RGBA32F);
  86. fboMaterial.allocate(Rw, Rh,GL_RGBA32F);
  87. fboMaterialAux.allocate(Rw, Rh,GL_RGBA32F);
  88. fboGaussian.allocate(Gw, Gh,GL_RGBA32F);
  89. fboGaussianDerivativeX.allocate(Gw, Gh,GL_RGBA32F);
  90. fboGaussianDerivativeY.allocate(Gw, Gh,GL_RGBA32F);
  91. preGaussian.allocate(Gw, Gh,GL_RGBA32F);
  92.  
  93. preGaussian.begin();
  94. ofClear(255, 255, 255, 255);
  95. preGaussian.end();
  96.  
  97.  
  98. // gaussian texture
  99.  
  100. fboGaussian.begin();
  101. ofClear(255, 255, 255, 0);
  102. ofTexture atex = preGaussian.getTexture();
  103. gaussSprite.begin();
  104. gaussSprite.setUniformTexture("tex1", atex, 1 );
  105. gaussSprite.setUniform1f("var", var);
  106. ofSetColor(255, 255, 255,255);
  107. preGaussian.draw(0, 0, Gw, Gh);
  108. gaussSprite.end();
  109. fboGaussian.end();
  110.  
  111. // gaussian derivative texture
  112.  
  113. fboGaussianDerivativeX.begin();
  114. ofClear(255, 255, 255, 0);
  115. gaussSpriteDerivative.begin();
  116. gaussSpriteDerivative.setUniformTexture("tex1", atex, 1 );
  117. gaussSpriteDerivative.setUniform1f("var",var);
  118. gaussSpriteDerivative.setUniform1i("dir",0);
  119. ofSetColor(255, 255, 255,255);
  120. preGaussian.draw(0, 0, Gw, Gh);
  121. gaussSpriteDerivative.end();
  122. fboGaussianDerivativeX.end();
  123.  
  124.  
  125.  
  126. GaussTexture = fboGaussian.getTexture();
  127. GaussTextureDerivX = fboGaussianDerivativeX.getTexture();
  128.  
  129. fboGaussianDerivativeY.begin();
  130. ofClear(255, 255,255, 0);
  131. gaussSpriteDerivative.begin();
  132. gaussSpriteDerivative.setUniformTexture("tex1", atex, 1 );
  133. gaussSpriteDerivative.setUniform1f("var",var);
  134. gaussSpriteDerivative.setUniform1i("dir",1);
  135. ofSetColor(255, 255, 255,255);
  136. preGaussian.draw(0, 0, Gw, Gh);
  137. gaussSpriteDerivative.end();
  138. fboGaussianDerivativeY.end();
  139.  
  140. GaussTextureDerivY = fboGaussianDerivativeY.getTexture();
  141.  
  142. PorstPro.begin();
  143. ofClear(255, 255, 255, 0);
  144. PorstPro.end();
  145.  
  146. fboMaterial.begin();
  147. ofClear(255, 255, 255, 0);
  148. fboMaterial.end();
  149. fboMaterialAux.begin();
  150. ofClear(255, 255, 255, 0);
  151. fboMaterialAux.end();
  152.  
  153. fondoFinal.load("images/Balsamic.jpg");
  154.  
  155. // calculating perspective transformation matrix
  156. //
  157. // cv::Point2f InPoints[4] = {
  158. // cv::Point2f(Rw/2.0,0),
  159. // cv::Point2f(Rw,Rh/2.0),
  160. // cv::Point2f(Rw/2.0,Rh),
  161. // cv::Point2f(0,Rh/2.0)
  162. // };
  163.  
  164. // cv::Point2f OutPoints[4] = {
  165. // cv::Point2f(1102,343),
  166. // cv::Point2f(1447,585),
  167. // cv::Point2f(1117,859),
  168. // cv::Point2f(747,591)
  169. // };
  170.  
  171. // Transf = cv::getPerspectiveTransform(InPoints,OutPoints);
  172. // cout<<Transf<<endl;
  173.  
  174. fboScreen1.allocate(Rw, Rh,GL_RGBA);
  175. fboScreen2.allocate(Rw, Rh,GL_RGBA);
  176.  
  177. fboScreen1.begin();
  178. ofClear(255, 255, 255, 0);
  179. fboScreen1.end();
  180.  
  181. fboScreen2.begin();
  182. ofClear(255, 255, 255, 0);
  183. fboScreen2.end();
  184.  
  185.  
  186. TheScreens.push_back("RGB");
  187. TheScreens.push_back("Gray");
  188. TheScreens.push_back("Dither");
  189. TheScreens.push_back("Darkest");
  190. TheScreens.push_back("TimeCoher");
  191. TheScreens.push_back("Gauss");
  192. TheScreens.push_back("Blend");
  193. TheScreens.push_back("Normals");
  194. TheScreens.push_back("Palette");
  195. TheScreens.push_back("ADS");
  196. TheScreens.push_back("Final");
  197.  
  198.  
  199. //Fonts
  200. Fuentes[0].load("helveticaBold.ttf", 34,true,true,false);
  201. Fuentes[1].load("helvetica.ttf", 30,true,true,false);
  202. Fuentes[2].load("helveticaBold.ttf", 20,true,true,false);
  203. Fuentes[3].load("helvetica.ttf", 20,true,true,true);
  204. Fuentes[4].load("helveticaBold.ttf", 40,true,true,false);
  205.  
  206. HDScreen.allocate(1920,1080,GL_RGBA);
  207.  
  208. HDScreen.begin();
  209. ofClear(255, 255, 255, 255);
  210. HDScreen.end();
  211.  
  212. }
  213.  
  214. //--------------------------------------------------------------
  215. void ofApp::update(){
  216. timeVar = (ofGetElapsedTimef() - refTime);
  217. if (timeVar>Tmax){
  218. refTime = ofGetElapsedTimef();
  219. timeVar = 0.0;
  220. TheInd++;
  221. if (TheInd > 10){TheInd=0;}
  222.  
  223. }
  224. ofSetWindowTitle(ofToString(ofGetFrameRate(), 2));
  225. ofBackground(100, 100, 100);
  226. bool bNewFrame = false;
  227.  
  228. vidGrabber.update();
  229. bNewFrame = vidGrabber.isFrameNew();
  230. if (bNewFrame){
  231. // load grayscale depth image from the kinect source
  232. ofPixels ThePixels;
  233. ThePixels = vidGrabber.getPixels();
  234. colorImg.setFromPixels(ThePixels);
  235. grayImage = colorImg;
  236. TheTargets.clear();
  237. TargetHits.clear();
  238. cv::Mat tempCv = grayImage.getCvImage();
  239. // cv::flip(tempCv,tempCv,1);
  240. // histogram equalization
  241. if (EquaHist == 1){
  242. cv::equalizeHist(tempCv,tempCv);
  243. }
  244. else if (EquaHist == 2){
  245. int clim = 4;
  246. cv::Ptr<cv::CLAHE> clahe = cv::createCLAHE();
  247. clahe->setClipLimit(clim);
  248. cv::Mat dst;
  249. clahe->apply(tempCv, tempCv);
  250. }
  251.  
  252.  
  253.  
  254.  
  255. while (!PointQueue.empty()){
  256. PointQueue.pop();
  257. }
  258.  
  259.  
  260.  
  261. TheDither.release();
  262. TheDither = cv::Mat(tempCv.rows,tempCv.cols,CV_8UC1);
  263. NonLinear(tempCv);
  264. Dither(tempCv,TheDither);
  265. MatchObjetTargetsPart1();
  266. MatchObjetTargetsPart2();
  267.  
  268. // set the vertex buffer object
  269.  
  270. vector<ofVec3f> pointsVec;
  271. for (int sisi =0; sisi<Nob; sisi++) {
  272. pointsVec.push_back(ofVec3f(TheObjects[sisi].x*Rw/(float)Nw,
  273. TheObjects[sisi].y*Rh/(float)Nh,0.0));
  274. }
  275.  
  276. // pointsVec.push_back(ofVec3f(Rw/4.0,Rh/2.0,0.0));
  277. // pointsVec.push_back(ofVec3f(Rw/2.0,Rh/4.0,0.0));
  278. // pointsVec.push_back(ofVec3f(3*Rw/4.0,Rh/2.0,0.0));
  279. // pointsVec.push_back(ofVec3f(Rw/2.0,3*Rh/4.0,0.0));
  280.  
  281.  
  282. ballsVbo.setVertexData( &pointsVec[0], Nob, GL_DYNAMIC_DRAW );
  283.  
  284. }
  285. }
  286.  
  287. //--------------------------------------------------------------
  288. void ofApp::draw(){
  289. // ofxCvGrayscaleImage ToShow;
  290.  
  291. // colorImg.draw(0,0,Nw,Nh);
  292. // if (!TheDither.empty()){
  293. // ToShow.allocate(TheDither.cols, TheDither.rows);
  294. // ToShow.setFromPixels(TheDither.data,TheDither.cols, TheDither.rows);
  295. // ToShow.draw(0, Nh,Nw,Nh);
  296. // }
  297. if((TheScreens[TheInd].compare("Gauss")==0)||(TheScreens[TheInd].compare("Blend")==0)){
  298. theFrameAux.begin();
  299. ofEnablePointSprites();
  300.  
  301. glEnable(GL_BLEND);
  302. glBlendEquation(GL_ADD);
  303. // glBlendFunc(GL_ONE, GL_ONE);
  304. // glDisable(GL_BLEND);
  305. ofClear(0, 0, 0, 0);
  306. metaballs.begin();
  307. GaussTexture.bind();
  308. metaballs.setUniform1i("TheChannel", 4);
  309. metaballs.setUniform1f("scale", .9);
  310. ballsVbo.draw(GL_POINTS, 0, Nob);
  311. GaussTexture.unbind();
  312. metaballs.end();
  313. ofDisablePointSprites();
  314. theFrameAux.end();
  315. }
  316.  
  317. else{
  318.  
  319. theFrameAux.begin();
  320.  
  321. ofEnablePointSprites();
  322.  
  323. glEnable(GL_BLEND);
  324. glBlendEquation(GL_ADD);
  325. glBlendFunc(GL_ONE, GL_ONE);
  326.  
  327. ofClear(0, 0, 0, 0);
  328. metaballs.begin();
  329. GaussTexture.bind();
  330. metaballs.setUniform1i("TheChannel", 1);
  331. metaballs.setUniform1f("scale", 0.01);
  332. ballsVbo.draw(GL_POINTS, 0, Nob);
  333. GaussTexture.unbind();
  334. metaballs.end();
  335.  
  336. metaballs.begin();
  337. GaussTextureDerivX.bind();
  338. metaballs.setUniform1i("TheChannel", 2);
  339. metaballs.setUniform1f("scale", 0.01);
  340. ballsVbo.draw(GL_POINTS, 0, Nob);
  341. GaussTextureDerivX.unbind();
  342. metaballs.end();
  343.  
  344. metaballs.begin();
  345. GaussTextureDerivY.bind();
  346. metaballs.setUniform1i("TheChannel", 3);
  347. metaballs.setUniform1f("scale", 0.01);
  348. ballsVbo.draw(GL_POINTS, 0, Nob);
  349. GaussTextureDerivY.unbind();
  350. metaballs.end();
  351.  
  352. glDisable(GL_BLEND);
  353. ofDisablePointSprites();
  354.  
  355. theFrameAux.end();
  356. }
  357.  
  358.  
  359. theFrame.begin();
  360.  
  361. ofEnablePointSprites();
  362.  
  363. glEnable(GL_BLEND);
  364. glBlendEquation(GL_ADD);
  365. glBlendFunc(GL_ONE, GL_ONE);
  366.  
  367. ofClear(0, 0, 0, 0);
  368. metaballs.begin();
  369. GaussTexture.bind();
  370. metaballs.setUniform1i("TheChannel", 1);
  371. metaballs.setUniform1f("scale", 0.01);
  372. ballsVbo.draw(GL_POINTS, 0, Nob);
  373. GaussTexture.unbind();
  374. metaballs.end();
  375.  
  376.  
  377. metaballs.begin();
  378. GaussTexture.bind();
  379. metaballs.setUniform1i("TheChannel", 2);
  380. metaballs.setUniform1f("scale", 0.01);
  381. ballsVbo.draw(GL_POINTS, 0, Nob);
  382. GaussTexture.unbind();
  383. metaballs.end();
  384.  
  385. metaballs.begin();
  386. GaussTexture.bind();
  387. metaballs.setUniform1i("TheChannel", 3);
  388. metaballs.setUniform1f("scale", 0.01);
  389. ballsVbo.draw(GL_POINTS, 0, Nob);
  390. GaussTexture.unbind();
  391. metaballs.end();
  392.  
  393. glDisable(GL_BLEND);
  394. ofDisablePointSprites();
  395.  
  396. theFrame.end();
  397.  
  398.  
  399.  
  400.  
  401. // PorstPro.begin();
  402. // ofClear(0, 0, 0, 0);
  403. // ofTexture tex3 = theFrame.getTexture();
  404. // postProShader.begin();
  405. // postProShader.setUniformTexture("tex1", tex3, 1 );
  406. // postProShader.setUniform1f("alpha", alpha);
  407. // ofSetColor(255, 255, 255,255);
  408. // theFrame.draw(0, 0, Rw, Rh);
  409. // postProShader.end();
  410. // PorstPro.end();
  411.  
  412.  
  413. fboMaterialAux.begin();
  414. ofClear(0, 0, 0, 255);
  415. ofTexture tex2 = theFrameAux.getTexture();
  416. materialShader.begin();
  417. materialShader.setUniformTexture("tex1", tex2, 1 );
  418. materialShader.setUniform1f("alpha", alpha);
  419. if(TheScreens[TheInd].compare("Final")==0){
  420. materialShader.setUniform1i("ADSOn",1);
  421. materialShader.setUniform1i("circleOn",1);
  422. }
  423. else{
  424. materialShader.setUniform1i("ADSOn",0);
  425. materialShader.setUniform1i("circleOn",0);
  426. }
  427. ofSetColor(0, 0, 0,255);
  428. theFrameAux.draw(0, 0, Rw, Rh);
  429. materialShader.end();
  430. fboMaterialAux.end();
  431.  
  432.  
  433. fboMaterial.begin();
  434. ofClear(0, 0, 0, 255);
  435. materialShader.begin();
  436. materialShader.setUniformTexture("tex1", tex2, 1 );
  437. materialShader.setUniform1f("alpha", alpha);
  438. materialShader.setUniform1i("ADSOn",1);
  439. materialShader.setUniform1i("circleOn",0);
  440. ofSetColor(0, 0, 0,255);
  441. theFrameAux.draw(0, 0, Rw, Rh);
  442. materialShader.end();
  443. fboMaterial.end();
  444.  
  445.  
  446. // material render
  447.  
  448.  
  449.  
  450.  
  451. // ofSetColor(0, 0, 0);
  452. // for (int sisi =0; sisi<Nob; sisi++) {
  453. // ofCircle(Nw + TheObjects[sisi].x, TheObjects[sisi].y, 4);
  454. // }
  455.  
  456. // theFrame.draw(Nw,0,2*320,2*240);
  457. // PorstPro.draw(Nw,2.2*Nh,2*320,2*240);
  458. // ofClear(0, 0, 0,0);
  459. // ofEnableAlphaBlending();
  460. //
  461. //
  462. //
  463. // ofSetColor(255, 255, 255,255);
  464. // fondoFinal.draw(0,0,1920,1080);
  465. // fboMaterial.draw(746,348,712,516);
  466. // ofDisableAlphaBlending();
  467. //
  468. // glDisable(GL_BLEND);
  469. // fboGaussianDerivativeX.draw(0,0,256,256);
  470. // fboGaussian.draw(0,256,256,256);
  471.  
  472. // defining what will be on screen
  473.  
  474. HDScreen.begin();
  475. ofBackground(255, 255, 255);
  476. ofSetColor(255, 255, 255,255);
  477. // The title:
  478. ofSetColor(0,0,0,255);
  479. float theWidth = ofGetWidth();
  480. float theHeight = ofGetHeight();
  481. Fuentes[4].drawString("A Vivisection of Analysis/Synthesis", theWidth/14.0, theHeight/14.0);
  482. ofSetColor(255,255,255,255);
  483. onScreen(TheScreens[TheInd]);
  484.  
  485. ofPushMatrix();
  486. ofTranslate(400, 300);
  487. ofPushMatrix();
  488. ofRotate(25, 0, 1, 0);
  489. if (TheInd != 1){
  490. //ofNoFill();
  491. //ofSetLineWidth(28.0);
  492. ofSetColor(0,0,0,255);
  493. ofDrawRectangle(-fboScreen1.getWidth()/2.0-6,
  494. -fboScreen1.getHeight()/2.0-6,320*3+12,240*3+12);
  495. }
  496. ofSetColor(255,255,255,255);
  497. fboScreen1.draw(-fboScreen1.getWidth()/2.0,
  498. -fboScreen1.getHeight()/2.0,320*3,240*3);
  499. ofPopMatrix();
  500. ofPopMatrix();
  501.  
  502. if (TheInd == 3){
  503. ofDisableAlphaBlending();
  504. }
  505. else{ ofEnableAlphaBlending();}
  506.  
  507. ofPushMatrix();
  508. if (timeVar < 3*Tmax/4.0){
  509. ofTranslate(800, 300);
  510. }
  511. else{
  512. float despx = ofMap(timeVar,3*Tmax/4.0,
  513. Tmax,800,400);
  514. ofTranslate(despx, 300);
  515. }
  516. ofPushMatrix();
  517.  
  518. ofRotate(25, 0, 1, 0);
  519. float fadeIn = (timeVar<Tmax/4.0)?
  520. 255*(timeVar*4.0/Tmax):255;
  521. if (TheInd != 0){
  522. //ofNoFill();
  523. //ofSetLineWidth(28.0);
  524. ofSetColor(0,0,0,fadeIn);
  525. ofDrawRectangle(-fboScreen1.getWidth()/2.0-6,
  526. -fboScreen1.getHeight()/2.0-6,320*3+12,240*3+12);
  527. }
  528.  
  529. ofSetColor(255,255,255,fadeIn);
  530. ofEnableAlphaBlending();
  531. fboScreen2.draw(-fboScreen2.getWidth()/2.0,
  532. -fboScreen2.getHeight()/2.0,320*3,240*3);
  533. ofDisableAlphaBlending();
  534. ofPopMatrix();
  535. ofPopMatrix();
  536. HDScreen.end();
  537.  
  538. ofSetColor(255, 255, 255,255);ofEnableAlphaBlending();
  539. HDScreen.draw(0,0,ofGetWidth(),
  540. ofGetHeight());
  541. ofDisableAlphaBlending();
  542.  
  543. }
  544.  
  545. //--------------------------------------------------------------
  546. void ofApp::keyPressed(int key){
  547.  
  548. if (key == OF_KEY_RIGHT){
  549.  
  550. float paso = (var < 10)?0.1:1.0;
  551. var -= paso;
  552. fboGaussian.begin();
  553. ofClear(255, 255, 255, 0);
  554. ofTexture atex = preGaussian.getTexture();
  555. gaussSprite.begin();
  556. gaussSprite.setUniformTexture("tex1", atex, 1 );
  557. gaussSprite.setUniform1f("var", var);
  558. ofSetColor(255, 255, 255,255);
  559. preGaussian.draw(0, 0, Gw, Gh);
  560. gaussSprite.end();
  561. fboGaussian.end();
  562.  
  563. GaussTexture = fboGaussian.getTexture();
  564.  
  565. fboGaussianDerivativeX.begin();
  566. ofClear(255, 255, 255, 0);
  567. gaussSpriteDerivative.begin();
  568. gaussSpriteDerivative.setUniformTexture("tex1", atex, 1 );
  569. gaussSpriteDerivative.setUniform1f("var", var);
  570. gaussSpriteDerivative.setUniform1i("dir",0);
  571. ofSetColor(255, 255, 255,255);
  572. preGaussian.draw(0, 0, Gw, Gh);
  573. gaussSpriteDerivative.end();
  574. fboGaussianDerivativeX.end();
  575.  
  576. GaussTextureDerivX = fboGaussianDerivativeX.getTexture();
  577.  
  578. fboGaussianDerivativeY.begin();
  579. ofClear(255, 255, 255, 0);
  580. gaussSpriteDerivative.begin();
  581. gaussSpriteDerivative.setUniformTexture("tex1", atex, 1 );
  582. gaussSpriteDerivative.setUniform1f("var", var);
  583. gaussSpriteDerivative.setUniform1i("dir",1);
  584. ofSetColor(255, 255, 255,255);
  585. preGaussian.draw(0, 0, Gw, Gh);
  586. gaussSpriteDerivative.end();
  587. fboGaussianDerivativeY.end();
  588.  
  589. GaussTextureDerivY = fboGaussianDerivativeY.getTexture();
  590.  
  591. cout<<var<<endl;
  592.  
  593. }
  594. if (key == OF_KEY_LEFT){
  595.  
  596. float paso = (var < 10)?0.1:1.0;
  597. var += paso;
  598. fboGaussian.begin();
  599. ofClear(255, 255, 255, 0);
  600. ofTexture atex = preGaussian.getTexture();
  601. gaussSprite.begin();
  602. gaussSprite.setUniformTexture("tex1", atex, 1 );
  603. gaussSprite.setUniform1f("var", var);
  604. ofSetColor(255, 255, 255,255);
  605. preGaussian.draw(0, 0, Gw, Gh);
  606. gaussSprite.end();
  607. fboGaussian.end();
  608.  
  609. GaussTexture = fboGaussian.getTexture();
  610.  
  611. fboGaussianDerivativeX.begin();
  612. ofClear(0, 0, 0, 0);
  613. gaussSpriteDerivative.begin();
  614. gaussSpriteDerivative.setUniformTexture("tex1", atex, 1 );
  615. gaussSpriteDerivative.setUniform1f("var",var);
  616. gaussSpriteDerivative.setUniform1i("dir",0);
  617. ofSetColor(255, 255, 255,255);
  618. preGaussian.draw(0, 0, Gw, Gh);
  619. gaussSpriteDerivative.end();
  620. fboGaussianDerivativeX.end();
  621.  
  622. GaussTextureDerivX = fboGaussianDerivativeX.getTexture();
  623.  
  624. fboGaussianDerivativeY.begin();
  625. ofClear(0, 0, 0, 0);
  626. gaussSpriteDerivative.begin();
  627. gaussSpriteDerivative.setUniformTexture("tex1", atex, 1 );
  628. gaussSpriteDerivative.setUniform1f("var",var);
  629. gaussSpriteDerivative.setUniform1i("dir",1);
  630. ofSetColor(255, 255, 255,255);
  631. preGaussian.draw(0, 0, Gw, Gh);
  632. gaussSpriteDerivative.end();
  633. fboGaussianDerivativeY.end();
  634.  
  635. GaussTextureDerivY = fboGaussianDerivativeY.getTexture();
  636.  
  637. cout<<var<<endl;
  638.  
  639. }
  640. if (key == OF_KEY_UP){
  641. float val;
  642. val = (alpha>2.5)?1.0:0.05;
  643. alpha += val;
  644. cout<<"alpha:" <<alpha<<endl;
  645. }
  646. if (key == OF_KEY_DOWN){
  647. float val;
  648. val = (alpha>2.5)?1.0:0.05;
  649. alpha -= val;
  650. cout<<"alpha:" <<alpha<<endl;
  651. }
  652. if (key == 'g'){
  653.  
  654. EquaHist ++;
  655. if (EquaHist>2){EquaHist = 0;}
  656.  
  657. }
  658.  
  659. if (key == 'a'){
  660. TheInd++;
  661. cout<<TheInd<<endl;
  662.  
  663. }
  664. if (key == 'z'){
  665. TheInd--;
  666. cout<<TheInd<<endl;
  667.  
  668. }
  669.  
  670. }
  671.  
  672.  
  673.  
  674. // sets the fbos to play on screen
  675. void ofApp::onScreen(string screenName){
  676. float thew = HDScreen.getWidth();// ofGetWidth();
  677. float theh = HDScreen.getHeight();//ofGetHeight();
  678. if (screenName.compare("RGB")==0){
  679.  
  680. // subtitle
  681. ofSetColor(0, 0, 0, 255);
  682. Fuentes[0].drawString("Analysis", 3.2*thew/4.0, 1.2*theh/3.0);
  683. Fuentes[3].drawString("1. A colored RGB Image is captured", 3*thew/4.0, 1.4*theh/3.0);
  684. Fuentes[3].drawString("from the Camera",3.05*thew/4.0, 1.5*theh/3.0);
  685.  
  686.  
  687.  
  688. ofSetColor(255, 255, 255, 255);
  689.  
  690.  
  691.  
  692.  
  693. fboScreen1.begin();
  694. ofClear(255,255,255,255);
  695. ofSetColor(255, 255, 255, 255);
  696. ofPushMatrix();
  697. ofScale(-1,1);
  698. colorImg.draw(-fboScreen1.getWidth(),0,fboScreen1.getWidth(),
  699. fboScreen1.getHeight());
  700. ofPopMatrix();
  701. fboScreen1.end();
  702.  
  703. fboScreen2.begin();
  704. ofClear(255,255,255,0);
  705. // split to RGB
  706.  
  707. cv::Mat bgr[3]; //destination array
  708. cv::Mat tempCv = colorImg.getCvImage();
  709. cv::flip(tempCv,tempCv, 1);
  710. split(tempCv,bgr);
  711. ofxCvGrayscaleImage ToShow;
  712. if (!bgr[0].empty()){
  713. ToShow.allocate(bgr[0].cols, bgr[0].rows);
  714. ToShow.setFromPixels(bgr[0].data,bgr[0].cols, bgr[0].rows);
  715. ofSetColor(255, 0, 0, 255);
  716. ToShow.draw(0,fboScreen1.getHeight()/2.0,fboScreen1.getWidth()/2.0,
  717. fboScreen1.getHeight()/2.0);
  718. }
  719. if (!bgr[1].empty()){
  720. ToShow.allocate(bgr[1].cols, bgr[1].rows);
  721. ToShow.setFromPixels(bgr[1].data,bgr[1].cols, bgr[1].rows);
  722. ofSetColor(0, 255, 0, 255);
  723. ToShow.draw(fboScreen1.getWidth()/4.0,fboScreen1.getHeight()/4.0,fboScreen1.getWidth()/2.0,
  724. fboScreen1.getHeight()/2.0);
  725. }
  726. if (!bgr[2].empty()){
  727. ToShow.allocate(bgr[2].cols, bgr[2].rows);
  728. ToShow.setFromPixels(bgr[2].data,bgr[2].cols, bgr[2].rows);
  729. ofSetColor(0, 0, 255, 255);
  730. ToShow.draw(fboScreen1.getWidth()/2.0,0,fboScreen1.getWidth()/2.0,
  731. fboScreen1.getHeight()/2.0);
  732. }
  733. fboScreen2.end();
  734. }
  735. if (screenName.compare("Gray")==0)
  736.  
  737. {
  738.  
  739. // subtitle
  740. ofSetColor(0, 0, 0, 255);
  741. Fuentes[0].drawString("Analysis", 3.2*thew/4.0, 1.2*theh/3.0);
  742. Fuentes[3].drawString("2. The RGB Image is converted", 3*thew/4.0, 1.4*theh/3.0);
  743. Fuentes[3].drawString("to gray values",3.05*thew/4.0, 1.5*theh/3.0);
  744.  
  745.  
  746.  
  747. ofSetColor(255, 255, 255, 255);
  748.  
  749. fboScreen1.begin();
  750. ofClear(255,255,255,0);
  751. // split to RGB
  752.  
  753. cv::Mat bgr[3]; //destination array
  754. cv::Mat tempCv = colorImg.getCvImage();
  755. cv::flip(tempCv,tempCv, 1);
  756. split(tempCv,bgr);
  757. ofxCvGrayscaleImage ToShow;
  758. if (!bgr[0].empty()){
  759. ToShow.allocate(bgr[0].cols, bgr[0].rows);
  760. ToShow.setFromPixels(bgr[0].data,bgr[0].cols, bgr[0].rows);
  761. ofSetColor(255, 0, 0, 255);
  762. ToShow.draw(0,fboScreen1.getHeight()/2.0,fboScreen1.getWidth()/2.0,
  763. fboScreen1.getHeight()/2.0);
  764. }
  765. if (!bgr[1].empty()){
  766. ToShow.allocate(bgr[1].cols, bgr[1].rows);
  767. ToShow.setFromPixels(bgr[1].data,bgr[1].cols, bgr[1].rows);
  768. ofSetColor(0, 255, 0, 255);
  769. ToShow.draw(fboScreen1.getWidth()/4.0,fboScreen1.getHeight()/4.0,fboScreen1.getWidth()/2.0,
  770. fboScreen1.getHeight()/2.0);
  771. }
  772. if (!bgr[2].empty()){
  773. ToShow.allocate(bgr[2].cols, bgr[2].rows);
  774. ToShow.setFromPixels(bgr[2].data,bgr[2].cols, bgr[2].rows);
  775. ofSetColor(0, 0, 255, 255);
  776. ToShow.draw(fboScreen1.getWidth()/2.0,0,fboScreen1.getWidth()/2.0,
  777. fboScreen1.getHeight()/2.0);
  778. }
  779. fboScreen1.end();
  780.  
  781.  
  782. fboScreen2.begin();
  783. ofClear(255,255,255,0);
  784. ofSetColor(255, 255, 255, 255);
  785. ofPushMatrix();
  786. ofScale(-1,1);
  787. grayImage.draw(-fboScreen1.getWidth(),0,fboScreen1.getWidth(),
  788. fboScreen1.getHeight());
  789. ofPopMatrix();
  790. fboScreen2.end();
  791. }
  792. if (screenName.compare("Dither")==0)
  793.  
  794. {
  795. // subtitle
  796. ofSetColor(0, 0, 0, 255);
  797. Fuentes[0].drawString("Analysis", 3.2*thew/4.0, 1.2*theh/3.0);
  798. Fuentes[3].drawString("3. A black and white version", 3*thew/4.0, 1.4*theh/3.0);
  799. Fuentes[3].drawString("is created, using density ",3.05*thew/4.0, 1.5*theh/3.0);
  800. Fuentes[3].drawString("to represent darker areas",3.05*thew/4.0, 1.6*theh/3.0);
  801. ofSetColor(255, 255, 255, 255);
  802. fboScreen1.begin();
  803. ofClear(255,255,255,0);
  804. ofSetColor(255, 255, 255, 255);
  805. ofPushMatrix();
  806. ofScale(-1,1);
  807. grayImage.draw(-fboScreen1.getWidth(),0,fboScreen1.getWidth(),
  808. fboScreen1.getHeight());
  809. ofPopMatrix();
  810. fboScreen1.end();
  811.  
  812. fboScreen2.begin();
  813. ofClear(255,255,255,0);
  814. ofxCvGrayscaleImage ToShow;
  815. cv::Mat tempCv;
  816. TheDither.copyTo(tempCv);
  817. cv::flip(tempCv,tempCv, 1);
  818. if (!tempCv.empty()){
  819. ToShow.allocate(tempCv.cols, tempCv.rows);
  820. ToShow.setFromPixels(tempCv.data,tempCv.cols, tempCv.rows);
  821. ToShow.draw(0,0,fboScreen1.getWidth(),
  822. fboScreen1.getHeight());
  823. }
  824.  
  825. fboScreen2.end();
  826. }
  827.  
  828. if (screenName.compare("Darkest")==0)
  829.  
  830. {
  831. // subtitle
  832. ofSetColor(0, 0, 0, 255);
  833. Fuentes[0].drawString("Analysis", 3.2*thew/4.0, 1.2*theh/3.0);
  834. Fuentes[3].drawString("4. Points that represent", 3*thew/4.0, 1.4*theh/3.0);
  835. Fuentes[3].drawString("the darkest regions are",3.05*thew/4.0, 1.5*theh/3.0);
  836. Fuentes[3].drawString("selected",3.05*thew/4.0, 1.6*theh/3.0);
  837. ofSetColor(255, 255, 255, 255);
  838.  
  839. fboScreen1.begin();
  840. ofClear(255,255,255,255);
  841. ofxCvGrayscaleImage ToShow;
  842. cv::Mat tempCv;
  843. TheDither.copyTo(tempCv);
  844. cv::flip(tempCv,tempCv, 1);
  845. if (!tempCv.empty()){
  846. ToShow.allocate(tempCv.cols, tempCv.rows);
  847. ToShow.setFromPixels(tempCv.data,tempCv.cols, tempCv.rows);
  848. ToShow.draw(0,0,fboScreen1.getWidth(),
  849. fboScreen1.getHeight());
  850. }
  851.  
  852. fboScreen1.end();
  853. fboScreen2.begin();
  854. ofEnableAlphaBlending();
  855. ofClear(255,255,255,255);
  856. ofDrawRectangle(0, 0, fboScreen1.getWidth(),
  857. fboScreen1.getHeight());
  858. // if (!tempCv.empty()){
  859. // ToShow.allocate(tempCv.cols, tempCv.rows);
  860. // ToShow.setFromPixels(tempCv.data,tempCv.cols, tempCv.rows);
  861. // ofSetColor(255,255,255,255);
  862. // ToShow.draw(0,0,fboScreen1.getWidth(),
  863. // fboScreen1.getHeight());
  864. // ofNoFill();
  865. //
  866. // ofSetColor(255,255,255,255);
  867. // }
  868. ofSetColor(120,0,0,70);
  869. for (int k = 0; k< TheTargets.size(); k++){
  870.  
  871. ofDrawCircle(TheTargets[k].x*fboScreen1.getWidth()/(float)Nw, TheTargets[k].y*fboScreen1.getHeight()/(float)Nh, 3);
  872. }
  873. ofDisableAlphaBlending();
  874. fboScreen2.end();
  875. }
  876. if(screenName.compare("TimeCoher")==0){
  877.  
  878. // subtitle
  879. ofSetColor(0, 0, 0, 255);
  880. Fuentes[0].drawString("Synthesis", 3.2*thew/4.0, 1.2*theh/3.0);
  881. Fuentes[3].drawString("1. Points in consecutive frames are", 3*thew/4.0, 1.4*theh/3.0);
  882. Fuentes[3].drawString("matched and their trajectory",3.05*thew/4.0, 1.5*theh/3.0);
  883. Fuentes[3].drawString("is smoothed",3.05*thew/4.0, 1.6*theh/3.0);
  884. ofSetColor(255, 255, 255, 255);
  885. fboScreen1.begin();
  886. ofEnableAlphaBlending();
  887. ofClear(255,255,255,255);
  888. ofDrawRectangle(0, 0, fboScreen1.getWidth(),
  889. fboScreen1.getHeight());
  890. // if (!tempCv.empty()){
  891. // ToShow.allocate(tempCv.cols, tempCv.rows);
  892. // ToShow.setFromPixels(tempCv.data,tempCv.cols, tempCv.rows);
  893. // ofSetColor(255,255,255,255);
  894. // ToShow.draw(0,0,fboScreen1.getWidth(),
  895. // fboScreen1.getHeight());
  896. // ofNoFill();
  897. //
  898. // ofSetColor(255,255,255,255);
  899. // }
  900. ofSetColor(120,0,0,70);
  901. for (int k = 0; k< TheTargets.size(); k++){
  902.  
  903. ofDrawCircle(TheTargets[k].x*fboScreen1.getWidth()/(float)Nw, TheTargets[k].y*fboScreen1.getHeight()/(float)Nh, 3);
  904. }
  905. ofDisableAlphaBlending();
  906. fboScreen1.end();
  907. fboScreen2.begin();
  908. ofEnableAlphaBlending();
  909. ofClear(255,255,255,255);
  910. ofDrawRectangle(0, 0, fboScreen1.getWidth(),
  911. fboScreen1.getHeight());
  912. // if (!tempCv.empty()){
  913. // ToShow.allocate(tempCv.cols, tempCv.rows);
  914. // ToShow.setFromPixels(tempCv.data,tempCv.cols, tempCv.rows);
  915. // ofSetColor(255,255,255,255);
  916. // ToShow.draw(0,0,fboScreen1.getWidth(),
  917. // fboScreen1.getHeight());
  918. // ofNoFill();
  919. //
  920. // ofSetColor(255,255,255,255);
  921. // }
  922. ofSetColor(0,0,150,70);
  923. for (int k = 0; k< Nob; k++){
  924.  
  925. ofDrawCircle(TheObjects[k].x*fboScreen1.getWidth()/(float)Nw, TheObjects[k].y*fboScreen1.getHeight()/(float)Nh, 3);
  926. }
  927. ofDisableAlphaBlending();
  928. fboScreen2.end();
  929.  
  930. }
  931.  
  932. if (screenName.compare("Gauss")==0)
  933.  
  934. {
  935.  
  936. // subtitle
  937. ofSetColor(0, 0, 0, 255);
  938. Fuentes[0].drawString("Synthesis", 3.2*thew/4.0, 1.2*theh/3.0);
  939. Fuentes[3].drawString("2. Points are replaced by", 3*thew/4.0, 1.4*theh/3.0);
  940. Fuentes[3].drawString("a 2D Gaussian function",3.05*thew/4.0, 1.5*theh/3.0);
  941. ofSetColor(255, 255, 255, 255);
  942.  
  943. fboScreen1.begin();
  944. ofEnableAlphaBlending();
  945. ofClear(255,255,255,255);
  946. ofDrawRectangle(0, 0, fboScreen1.getWidth(),
  947. fboScreen1.getHeight());
  948. // if (!tempCv.empty()){
  949. // ToShow.allocate(tempCv.cols, tempCv.rows);
  950. // ToShow.setFromPixels(tempCv.data,tempCv.cols, tempCv.rows);
  951. // ofSetColor(255,255,255,255);
  952. // ToShow.draw(0,0,fboScreen1.getWidth(),
  953. // fboScreen1.getHeight());
  954. // ofNoFill();
  955. //
  956. // ofSetColor(255,255,255,255);
  957. // }
  958. ofSetColor(0,0,150,70);
  959. for (int k = 0; k< Nob; k++){
  960.  
  961. ofDrawCircle(TheObjects[k].x*fboScreen1.getWidth()/(float)Nw, TheObjects[k].y*fboScreen1.getHeight()/(float)Nh, 3);
  962. }
  963. ofDisableAlphaBlending();
  964. fboScreen1.end();
  965.  
  966.  
  967. fboScreen2.begin();
  968. ofClear(255,255,255,255);
  969. ofSetColor(255, 255, 255, 255);
  970.  
  971. theFrameAux.draw(0,0,fboScreen1.getWidth(),
  972. fboScreen1.getHeight());;
  973.  
  974. fboScreen2.end();
  975. }
  976. if (screenName.compare("Blend")==0)
  977.  
  978. {
  979. // subtitle
  980. ofSetColor(0, 0, 0, 255);
  981. Fuentes[0].drawString("Synthesis", 3.2*thew/4.0, 1.2*theh/3.0);
  982. Fuentes[3].drawString("3. All the gaussians are blended", 3*thew/4.0, 1.4*theh/3.0);
  983.  
  984. ofSetColor(255, 255, 255, 255);
  985. fboScreen1.begin();
  986. ofClear(255,255,255,255);
  987. ofSetColor(255, 255, 255, 255);
  988.  
  989. theFrameAux.draw(0,0,fboScreen1.getWidth(),
  990. fboScreen1.getHeight());;
  991.  
  992. fboScreen1.end();
  993. fboScreen2.begin();
  994. ofClear(255,255,255,255);
  995. ofSetColor(255, 255, 255, 255);
  996.  
  997. theFrame.draw(0,0,fboScreen1.getWidth(),
  998. fboScreen1.getHeight());;
  999.  
  1000. fboScreen2.end();
  1001.  
  1002.  
  1003. }
  1004.  
  1005. if (screenName.compare("Normals")==0)
  1006.  
  1007. {
  1008. ofSetColor(0, 0, 0, 255);
  1009. Fuentes[0].drawString("Synthesis", 3.2*thew/4.0, 1.2*theh/3.0);
  1010. Fuentes[3].drawString("4. Other surfaces (in green and blue)", 3*thew/4.0, 1.4*theh/3.0);
  1011. Fuentes[3].drawString("are derived from the Gaussian blend",3.05*thew/4.0, 1.5*theh/3.0);
  1012. Fuentes[3].drawString("(in red)",3.05*thew/4.0, 1.6*theh/3.0);
  1013.  
  1014. ofSetColor(255, 255, 255, 255);
  1015.  
  1016. fboScreen1.begin();
  1017. ofClear(255,255,255,255);
  1018. ofSetColor(255, 255, 255, 255);
  1019.  
  1020. theFrame.draw(0,0,fboScreen1.getWidth(),
  1021. fboScreen1.getHeight());;
  1022.  
  1023. fboScreen1.end();
  1024. fboScreen2.begin();
  1025. ofClear(255,255,255,255);
  1026. ofSetColor(255, 255, 255, 255);
  1027.  
  1028. theFrameAux.draw(0,0,fboScreen1.getWidth(),
  1029. fboScreen1.getHeight());;
  1030.  
  1031. fboScreen2.end();
  1032. }
  1033.  
  1034. if (screenName.compare("Palette")==0)
  1035.  
  1036. {
  1037. ofSetColor(0, 0, 0, 255);
  1038. Fuentes[0].drawString("Synthesis", 3.2*thew/4.0, 1.2*theh/3.0);
  1039. Fuentes[3].drawString("5. Each gray value of the", 3*thew/4.0, 1.4*theh/3.0);
  1040. Fuentes[3].drawString("blend, is mapped to an",3.05*thew/4.0, 1.5*theh/3.0);
  1041. Fuentes[3].drawString("RGB value",3.05*thew/4.0, 1.6*theh/3.0);
  1042.  
  1043. ofSetColor(255, 255, 255, 255);
  1044.  
  1045.  
  1046. fboScreen1.begin();
  1047. ofClear(255,255,255,255);
  1048. ofSetColor(255, 255, 255, 255);
  1049.  
  1050. theFrameAux.draw(0,0,fboScreen1.getWidth(),
  1051. fboScreen1.getHeight());;
  1052.  
  1053. fboScreen1.end();
  1054.  
  1055. fboScreen2.begin();
  1056. ofClear(255,255,255,255);
  1057. ofSetColor(255, 255, 255, 255);
  1058.  
  1059. fboMaterialAux.draw(0,0,fboScreen1.getWidth(),
  1060. fboScreen1.getHeight());;
  1061.  
  1062. fboScreen2.end();
  1063.  
  1064. }
  1065. if (screenName.compare("ADS")==0)
  1066.  
  1067. {
  1068. ofSetColor(0, 0, 0, 255);
  1069. Fuentes[0].drawString("Synthesis", 3.2*thew/4.0, 1.2*theh/3.0);
  1070. Fuentes[3].drawString("6. Light reflections are", 3*thew/4.0, 1.4*theh/3.0);
  1071. Fuentes[3].drawString("calculated and added",3.05*thew/4.0, 1.5*theh/3.0);
  1072.  
  1073. ofSetColor(255, 255, 255, 255);
  1074. fboScreen1.begin();
  1075. ofClear(255,255,255,255);
  1076. ofSetColor(255, 255, 255, 255);
  1077.  
  1078. fboMaterialAux.draw(0,0,fboScreen1.getWidth(),
  1079. fboScreen1.getHeight());;
  1080.  
  1081. fboScreen1.end();
  1082. fboScreen2.begin();
  1083. ofClear(255,255,255,255);
  1084. ofSetColor(255, 255, 255, 255);
  1085.  
  1086. fboMaterial.draw(0,0,fboScreen1.getWidth(),
  1087. fboScreen1.getHeight());;
  1088.  
  1089. fboScreen2.end();
  1090.  
  1091. }
  1092. if (screenName.compare("Final")==0)
  1093.  
  1094. {
  1095. ofSetColor(0, 0, 0, 255);
  1096. Fuentes[0].drawString("Synthesis", 3.2*thew/4.0, 1.2*theh/3.0);
  1097. Fuentes[3].drawString("7. Final image is embedded", 3*thew/4.0, 1.4*theh/3.0);
  1098. Fuentes[3].drawString("into a scene",3.05*thew/4.0, 1.5*theh/3.0);
  1099.  
  1100. ofSetColor(255, 255, 255, 255);
  1101. fboScreen1.begin();
  1102. ofClear(255,255,255,255);
  1103. ofSetColor(255, 255, 255, 255);
  1104.  
  1105. fboMaterial.draw(0,0,fboScreen1.getWidth(),
  1106. fboScreen1.getHeight());;
  1107.  
  1108. fboScreen1.end();
  1109. fboScreen2.begin();
  1110. ofClear(0, 0, 0,0);
  1111. ofEnableAlphaBlending();
  1112. ofSetColor(255, 255, 255,255);
  1113. fondoFinal.draw(0,0,fboScreen1.getWidth(),
  1114. fboScreen1.getHeight());
  1115. fboMaterialAux.draw(746/1920.0*fboScreen1.getWidth(),348/1080.0*fboScreen1.getHeight(),712/1920.0*fboScreen1.getWidth(),516/1080.0*fboScreen1.getHeight());
  1116. ofDisableAlphaBlending();
  1117. fboScreen2.end();
  1118.  
  1119. }
  1120.  
  1121.  
  1122.  
  1123.  
  1124. }
  1125. //--------------------------------------------------------------
  1126. void ofApp::keyReleased(int key){
  1127.  
  1128. }
  1129.  
  1130. //--------------------------------------------------------------
  1131. void ofApp::mouseMoved(int x, int y ){
  1132.  
  1133. }
  1134.  
  1135. //--------------------------------------------------------------
  1136. void ofApp::mouseDragged(int x, int y, int button){
  1137.  
  1138. }
  1139.  
  1140. //--------------------------------------------------------------
  1141. void ofApp::mousePressed(int x, int y, int button){
  1142.  
  1143. }
  1144.  
  1145. //--------------------------------------------------------------
  1146. void ofApp::mouseReleased(int x, int y, int button){
  1147.  
  1148. }
  1149.  
  1150. //--------------------------------------------------------------
  1151. void ofApp::mouseEntered(int x, int y){
  1152.  
  1153. }
  1154.  
  1155. //--------------------------------------------------------------
  1156. void ofApp::mouseExited(int x, int y){
  1157.  
  1158. }
  1159.  
  1160. //--------------------------------------------------------------
  1161. void ofApp::windowResized(int w, int h){
  1162.  
  1163. }
  1164.  
  1165. //--------------------------------------------------------------
  1166. void ofApp::gotMessage(ofMessage msg){
  1167.  
  1168. }
  1169.  
  1170. //--------------------------------------------------------------
  1171. void ofApp::dragEvent(ofDragInfo dragInfo){
  1172.  
  1173. }
  1174.  
  1175. void ofApp::NonLinear(cv::Mat &src) {
  1176. int h = src.rows;
  1177. int w = src.cols;
  1178. float radi;
  1179. radi = (TheInd < 10)?1.0:0.25;
  1180. for (int j = 0; j < h; j++) {
  1181. float ncj = (j/(float)h - 0.5);
  1182. for (int i = 0; i < w; i++) {
  1183. float nci = (i/(float)w - 0.5);
  1184. uchar newVal;
  1185. if ((ncj*ncj+nci*nci)<radi){
  1186. newVal = (Imin +255*((Imax -Imin)/255.0*(pow((float)(src.at<uchar>(j,i)/255.0),(float)G_alpha))));
  1187. }
  1188. else{
  1189. newVal = 255;
  1190. }
  1191. src.at<uchar>(j,i) = newVal;
  1192.  
  1193. }
  1194. }
  1195.  
  1196.  
  1197. }
  1198.  
  1199. void ofApp::Dither(const cv::Mat src, cv::Mat &dst) {
  1200.  
  1201. int pxPos;
  1202. int oldPx = 0;
  1203. int newPx = 0;
  1204. int qError = 0;
  1205.  
  1206. int threshold = 130;
  1207. src.copyTo(dst);
  1208. int r1, r2, bl1, bc1, br1, bc2;
  1209. int new1,new2;
  1210.  
  1211. int h = src.rows;
  1212. int w = src.cols;
  1213. for (int j = 0; j < h-2; j++) {
  1214. for (int i = 1; i < w-2; i++) {
  1215. oldPx = dst.at<uchar>(j,i);
  1216. newPx = oldPx < threshold ? 0 : 255;
  1217. dst.at<uchar>(j,i) = newPx;
  1218. qError = oldPx - newPx;
  1219.  
  1220. //if (qError <0)
  1221. {qError*=2*0.5;}
  1222.  
  1223. // super duper ugly 2 part value clipping (to avoid values beyond 0-255)
  1224. r1 = MIN(dst.at<uchar>(j,(i+1)) + (int)(.125f * qError), 255);
  1225. r2 = MIN(dst.at<uchar>(j,(i+2)) + (int)(.125f * qError), 255);
  1226. bl1 = MIN(dst.at<uchar>(j+1,(i-1)) + (int)(.125f * qError), 255);
  1227. bc1 = MIN(dst.at<uchar>(j+1,i) + (int)(.125f * qError), 255);
  1228. br1 = MIN(dst.at<uchar>(j+1,(i+1)) + (int)(.125f * qError), 255);
  1229. bc2 = MIN(dst.at<uchar>(j+2,i) + (int)(.125f * qError), 255);
  1230. // new1 = MIN(dst[(j+1)*w+(i+2)] + (int)(.125f * qError), 255);
  1231. // new2 = MIN(dst[(j+2)*w+(i+1)] + (int)(.125f * qError), 255);
  1232.  
  1233.  
  1234. dst.at<uchar>(j,(i+1)) = MAX(r1 , 0);
  1235. dst.at<uchar>(j+1,(i-1)) = MAX(bl1, 0);
  1236. dst.at<uchar>(j+1,i) = MAX(bc1, 0);
  1237. dst.at<uchar>(j+1,i+1) = MAX(br1, 0);
  1238. dst.at<uchar>(j+2,i) = MAX(bc2, 0);
  1239. // dst[(j+1)*w+(i+2)] = MAX(new1, 0);
  1240. // dst[(j+2)*w+(i+1)] = MAX(new2, 0);
  1241. if (qError>0){
  1242. PointQueue.push(ThePoints(Nw-i,j,qError));
  1243. }
  1244.  
  1245. }
  1246. }
  1247. }
  1248.  
  1249. void ofApp::MatchObjetTargetsPart1() {
  1250.  
  1251. ThePoints tempoPoint;
  1252. int TheLimit = MIN(Ntg, PointQueue.size());
  1253.  
  1254. for (int k = 0; k < TheLimit; k++) {
  1255. tempoPoint = PointQueue.top();
  1256. TheTargets.push_back(ofVec2f(tempoPoint.Pos.x, tempoPoint.Pos.y));
  1257. PointQueue.pop();
  1258. // TargetHits.push_back(tempoPoint.Error);
  1259. TargetHits.push_back(0);
  1260. }
  1261.  
  1262. for (int i = 0; i< Nob; i++) {
  1263. TargInd[i] = -1;
  1264. }
  1265.  
  1266. if (!TheTargets.empty()) {
  1267. // empty the hash tables
  1268.  
  1269. for (int roH = 0; roH < HashRow; roH++) {
  1270. for (int coH = 0; coH < HashColumn; coH++) {
  1271.  
  1272. ObjectHash[roH][coH].clear();
  1273.  
  1274. ObjectIndHash[roH][coH].clear();
  1275.  
  1276. }
  1277. }
  1278.  
  1279.  
  1280.  
  1281.  
  1282.  
  1283.  
  1284. // move objects to the hash cells
  1285. for (int ko = 0; ko < Nob; ko++) {
  1286. int indrow = floor(TheObjects[ko].y / hashdec);
  1287. indrow = MAX(0, indrow);
  1288. indrow = MIN(HashRow - 1, indrow);
  1289. int indcol = floor(TheObjects[ko].x / hashdec);
  1290. indcol = MAX(0, indcol);
  1291. indcol = MIN(HashColumn - 1, indcol);
  1292.  
  1293. ObjectHash[indrow][indcol].push_back(TheObjects[ko]);
  1294. ObjectIndHash[indrow][indcol].push_back(ko);
  1295. }
  1296. // running through the targets and finding a close free object
  1297.  
  1298. for (int kt = 0; kt < TheTargets.size(); kt++) {
  1299. int indrow = floor(TheTargets[kt].y / hashdec);
  1300. int indcol = floor(TheTargets[kt].x / hashdec);
  1301. indrow = MAX(0, indrow);
  1302. indrow = MIN(HashRow - 1, indrow);
  1303. indcol = MAX(0, indcol);
  1304. indcol = MIN(HashColumn - 1, indcol);
  1305. int ww = 0; // window width
  1306. int wh = 0; // window height
  1307. bool MatchFound = false;
  1308. int winsize = 0;
  1309. while (!MatchFound) {
  1310. ww = winsize;
  1311. wh = winsize;
  1312. float MinDis = 10000 * 10000;
  1313. int MinIndi = 0;
  1314. for (int n = -ww; n <= ww; n++) {
  1315. for (int m = -wh; m <= wh; m++) {
  1316. // if the cell is not empty
  1317. if ((indcol + n >= 0) &&
  1318. (indrow + m >= 0) &&
  1319. (indcol + n < 16) &&
  1320. (indrow + m < 12) &&
  1321. (!ObjectHash[indrow + m][indcol + n].empty())
  1322. ) {
  1323. std::list<ofVec2f>::iterator
  1324. it1 = ObjectHash[indrow + m][indcol + n].begin();
  1325. std::list<int>::iterator
  1326. it2 = ObjectIndHash[indrow + m][indcol + n].begin();
  1327. int elemcount = 0;
  1328. int Theelement = 0;
  1329. while (it1 != ObjectHash[indrow + m][indcol + n].end())
  1330. {
  1331. ofVec2f theobj = (*it1);
  1332. float currDist = TheTargets[kt].squareDistance(theobj);
  1333. if (currDist < MinDis) {
  1334. MinDis = currDist;
  1335. MinIndi = (*it2);
  1336. Theelement = elemcount;
  1337. }
  1338. it1++;
  1339. it2++;
  1340. elemcount++;
  1341. }
  1342. TargInd[MinIndi] = kt;
  1343. MatchFound = true;
  1344. // delete element for the list
  1345. it1 = ObjectHash[indrow + m][indcol + n].begin();
  1346. it2 = ObjectIndHash[indrow + m][indcol + n].begin();
  1347. for (int ec = 0; ec < Theelement; ec++) {
  1348. it1++;
  1349. it2++;
  1350. }
  1351. ObjectHash[indrow + m][indcol + n].erase(it1);
  1352. ObjectIndHash[indrow + m][indcol + n].erase(it2);
  1353. n = ww + 1;
  1354. m = wh + 1;
  1355. break;
  1356. }
  1357. else { // increment area counter
  1358. // if last window cell
  1359. if ((n == ww) && (m == wh)) {
  1360. winsize++;
  1361. }
  1362. }
  1363. }// for m
  1364. }// for n
  1365. } // while
  1366. } // loop targets
  1367. } // end if targets non empty
  1368. }
  1369.  
  1370.  
  1371. void ofApp::MatchObjetTargetsPart2() {
  1372. float dt = 0.25;
  1373. for (int ko = 0; ko<Nob; ko++) {
  1374.  
  1375. float MinHits = 10000;
  1376. ofVec2f UpdateVec;
  1377. float MinDis = 10000 * 10000;
  1378. int MinIndi = 0;
  1379.  
  1380. if (TargInd[ko] == -1) {
  1381. MinDis = 10000 * 10000;
  1382. for (int kt = 0; kt<TheTargets.size(); kt++) {
  1383. ofVec2f ErrorVec;
  1384. ErrorVec = TheTargets[kt] - TheObjects[ko];
  1385. float dis = TargetHits[kt] * ErrorVec.lengthSquared();
  1386. if (dis < MinDis) {
  1387. MinDis = dis;
  1388. MinIndi = kt;
  1389. }
  1390.  
  1391. }
  1392.  
  1393. TargetHits[MinIndi]++;
  1394. TargInd[ko] = MinIndi;
  1395. }
  1396.  
  1397. UpdateVec = TheTargets[TargInd[ko]] - TheObjects[ko];
  1398. float newDis = UpdateVec.length();
  1399. UpdateVec.normalize();
  1400.  
  1401.  
  1402. ofVec2f acc;
  1403.  
  1404. if (newDis < ThDis) {
  1405. acc = (newDis / 10.0)*(Ktar*UpdateVec) - Kdam*TheVelocities[ko];
  1406. }
  1407. else {
  1408. acc = (Ktar*UpdateVec) - Kdam*TheVelocities[ko];
  1409. }
  1410. TheVelocities[ko] = TheVelocities[ko] - (-dt)*acc;
  1411. TheObjects[ko] = TheObjects[ko] - (-dt)*TheVelocities[ko];
  1412. }
  1413.  
  1414. }// closing function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement