PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/sketch_071021c/applet/sketch_071021c.pde

https://github.com/enjrolas/processingprojects
Processing | 327 lines | 246 code | 37 blank | 44 comment | 42 complexity | a26708048a25d64c005c8114fc3e9edb MD5 | raw file
  1. import processing.video.*;
  2. Capture cam;
  3. PImage img;
  4. boolean newFrame=false;
  5. int _id =0;
  6. ArrayList aboveThreshold;
  7. ArrayList clusters;
  8. // ==================================================
  9. // setup()
  10. // ==================================================
  11. void setup()
  12. {
  13. // Size of applet
  14. size(320, 240);
  15. // Capture
  16. cam = new Capture(this, 320, 240, 15);
  17. // BlobDetection
  18. // img which will be sent to detection (a smaller copy of the cam frame);
  19. img = new PImage(320,240);
  20. //theBlobDetection = new BlobDetection(img.width, img.height);
  21. //theBlobDetection.setPosDiscrimination(true);
  22. //theBlobDetection.setThreshold(0.2f); // will detect bright areas whose luminosity > 0.2f;
  23. clusters = new ArrayList();
  24. }
  25. // ==================================================
  26. // captureEvent()
  27. // ==================================================
  28. void captureEvent(Capture cam)
  29. {
  30. cam.read();
  31. aboveThreshold = new ArrayList();
  32. clusters = new ArrayList();
  33. smartPixel[][] newPixels = new smartPixel[cam.width][cam.height];
  34. int redthresh=50;
  35. int bluethresh=100;
  36. int greenthresh=100;
  37. int pointx=0;
  38. int pointy=0;
  39. int numpoints=0;
  40. double z;
  41. double f0=12;
  42. double k=1/14.0;
  43. for(int i=0;i<cam.width*cam.height;i++)
  44. {
  45. if((red(cam.pixels[i])<redthresh)||(blue(cam.pixels[i])<bluethresh)||(green(cam.pixels[i])<greenthresh)){
  46. cam.pixels[i]=color(0,0,0);
  47. newPixels[i%cam.width][i/cam.width] = new smartPixel(null, color(0,0,0), i%cam.width, i/cam.width);
  48. }else{
  49. int xpos = (i%cam.width);
  50. int ypos = (i/cam.width);
  51. pointx+=(i%cam.width);
  52. pointy+=(i/cam.width);
  53. numpoints++;
  54. smartPixel newp = new smartPixel(null, color(255,255,255), xpos, ypos);
  55. if (xpos > 0) {
  56. smartPixel p1 = newPixels[xpos-1][ypos];
  57. if (p1.c == color(255,255,255)) {
  58. p1.clust.sm.add(newp);
  59. newp.clust = p1.clust;
  60. p1.clust.centerx = (p1.clust.centerx*p1.clust.sm.size()+newp.x)/(p1.clust.sm.size()+1);
  61. p1.clust.centery = (p1.clust.centery*p1.clust.sm.size()+newp.y)/(p1.clust.sm.size()+1);
  62. }
  63. }
  64. if (ypos > 0 && xpos >0) {
  65. smartPixel p2 = newPixels[xpos-1][ypos-1];
  66. if (p2.c == color(255,255,255)) {
  67. if(newp.clust != null) {
  68. unionClusters(newp.clust, p2.clust);
  69. } else {
  70. p2.clust.sm.add(newp);
  71. newp.clust = p2.clust;
  72. p2.clust.centerx = (p2.clust.centerx*p2.clust.sm.size()+newp.x)/(p2.clust.sm.size()+1);
  73. p2.clust.centery = (p2.clust.centery*p2.clust.sm.size()+newp.y)/(p2.clust.sm.size()+1);
  74. }
  75. }
  76. }
  77. if (ypos > 0) {
  78. smartPixel p3 = newPixels[xpos][ypos-1];
  79. if (p3.c == color(255,255,255)) {
  80. if(newp.clust != null) {
  81. unionClusters(newp.clust, p3.clust);
  82. } else {
  83. p3.clust.sm.add(newp);
  84. newp.clust = p3.clust;
  85. p3.clust.centerx = (p3.clust.centerx*p3.clust.sm.size()+newp.x)/(p3.clust.sm.size()+1);
  86. p3.clust.centery = (p3.clust.centery*p3.clust.sm.size()+newp.y)/(p3.clust.sm.size()+1);
  87. }
  88. }
  89. }
  90. if(newp.clust == null) {
  91. cluster newc = new cluster(newp);
  92. clusters.add(newc);
  93. newp.clust = newc;
  94. }
  95. newPixels[xpos][ypos] = newp;
  96. aboveThreshold.add(newp);
  97. //cam.pixels[i]=color(255,255,255);
  98. }
  99. }
  100. /*
  101. if(numpoints!=0)
  102. {
  103. pointx=pointx/numpoints;
  104. pointy=pointy/numpoints;
  105. }
  106. cam.pixels[pointx+pointy*cam.width]=color(255,0,0);
  107. if(pointx<(cam.width/2))
  108. z=f0-k*((cam.width/2)-(double)pointx);
  109. else
  110. z=f0+k*((double)pointx-(cam.width/2));
  111. println("POINT: " + pointx);
  112. println(z);
  113. */
  114. for(int q=0;q<clusters.size();++q) {
  115. cluster cc = (cluster) clusters.get(q);
  116. cam.pixels[int(cc.centery)*cam.width+int(cc.centerx)] = color(255,0,0);
  117. println(cc.sm.size());
  118. }
  119. newFrame = true;
  120. }
  121. // ==================================================
  122. // draw()
  123. // ==================================================
  124. void draw()
  125. {
  126. if (newFrame)
  127. {
  128. background(255);
  129. newFrame=false;
  130. img.copy(cam, 0, 0, cam.width, cam.height,
  131. 0, 0, img.width, img.height);
  132. image(cam,0,0,width,height);
  133. //fastblur(img, 2);
  134. //theBlobDetection.computeBlobs(img.pixels);
  135. //drawBlobsAndEdges(true,true);
  136. }
  137. }
  138. // ==================================================
  139. // Super Fast Blur v1.1
  140. // by Mario Klingemann
  141. // <http://incubator.quasimondo.com>
  142. // ==================================================
  143. void fastblur(PImage img,int radius)
  144. {
  145. if (radius<1){
  146. return;
  147. }
  148. int w=img.width;
  149. int h=img.height;
  150. int wm=w-1;
  151. int hm=h-1;
  152. int wh=w*h;
  153. int div=radius+radius+1;
  154. int r[]=new int[wh];
  155. int g[]=new int[wh];
  156. int b[]=new int[wh];
  157. int rsum,gsum,bsum,x,y,i,p,p1,p2,yp,yi,yw;
  158. int vmin[] = new int[max(w,h)];
  159. int vmax[] = new int[max(w,h)];
  160. int[] pix=img.pixels;
  161. int dv[]=new int[256*div];
  162. for (i=0;i<256*div;i++){
  163. dv[i]=(i/div);
  164. }
  165. yw=yi=0;
  166. for (y=0;y<h;y++){
  167. rsum=gsum=bsum=0;
  168. for(i=-radius;i<=radius;i++){
  169. p=pix[yi+min(wm,max(i,0))];
  170. rsum+=(p & 0xff0000)>>16;
  171. gsum+=(p & 0x00ff00)>>8;
  172. bsum+= p & 0x0000ff;
  173. }
  174. for (x=0;x<w;x++){
  175. r[yi]=dv[rsum];
  176. g[yi]=dv[gsum];
  177. b[yi]=dv[bsum];
  178. if(y==0){
  179. vmin[x]=min(x+radius+1,wm);
  180. vmax[x]=max(x-radius,0);
  181. }
  182. p1=pix[yw+vmin[x]];
  183. p2=pix[yw+vmax[x]];
  184. rsum+=((p1 & 0xff0000)-(p2 & 0xff0000))>>16;
  185. gsum+=((p1 & 0x00ff00)-(p2 & 0x00ff00))>>8;
  186. bsum+= (p1 & 0x0000ff)-(p2 & 0x0000ff);
  187. yi++;
  188. }
  189. yw+=w;
  190. }
  191. for (x=0;x<w;x++){
  192. rsum=gsum=bsum=0;
  193. yp=-radius*w;
  194. for(i=-radius;i<=radius;i++){
  195. yi=max(0,yp)+x;
  196. rsum+=r[yi];
  197. gsum+=g[yi];
  198. bsum+=b[yi];
  199. yp+=w;
  200. }
  201. yi=x;
  202. for (y=0;y<h;y++){
  203. pix[yi]=0xff000000 | (dv[rsum]<<16) | (dv[gsum]<<8) | dv[bsum];
  204. if(x==0){
  205. vmin[y]=min(y+radius+1,hm)*w;
  206. vmax[y]=max(y-radius,0)*w;
  207. }
  208. p1=x+vmin[y];
  209. p2=x+vmax[y];
  210. rsum+=r[p1]-r[p2];
  211. gsum+=g[p1]-g[p2];
  212. bsum+=b[p1]-b[p2];
  213. yi+=w;
  214. }
  215. }
  216. }
  217. class smartPixel {
  218. smartPixel parent;
  219. color c;
  220. int numPix, x,y;
  221. float centerx, centery;
  222. cluster clust;
  223. smartPixel() {
  224. }
  225. smartPixel(smartPixel p, color _c, int _x, int _y) {
  226. parent = p;
  227. c = _c;
  228. numPix = 1;
  229. x = _x;
  230. y = _y;
  231. centerx = _x;
  232. centery = _y;
  233. clust = null;
  234. }
  235. smartPixel union(smartPixel n) {
  236. smartPixel temp1 = this;
  237. while(temp1.parent !=null) {
  238. //println(temp1.parent.x + " " + temp1.parent.y);
  239. temp1 = temp1.parent;
  240. }
  241. smartPixel temp = n;
  242. while(temp.parent!=null) {
  243. temp = temp.parent;
  244. //println("ELSE");
  245. }
  246. if(temp.x != temp1.x && temp.y != temp1.y) {
  247. temp.parent = temp1;
  248. //println("ASD: " + temp1.centerx + " " + temp1.centery + " " + numPix);
  249. temp1.centerx = (float)(temp1.centerx*temp1.numPix+temp.centerx*temp.numPix)/(float)(temp1.numPix+temp.numPix);
  250. temp1.centery = (float)(temp1.centery*temp1.numPix+temp.centery*temp.numPix)/(float)(temp1.numPix+temp.numPix);
  251. //println(temp1.centerx + " " + temp1.centery + " " + numPix);
  252. temp1.numPix += temp.numPix;
  253. }
  254. return temp1;
  255. }
  256. }
  257. class cluster {
  258. ArrayList sm;
  259. float centerx,centery;
  260. int id;
  261. cluster() {
  262. id = _id++;
  263. sm = new ArrayList();
  264. }
  265. cluster(smartPixel p) {
  266. id = _id++;
  267. sm = new ArrayList();
  268. sm.add(p);
  269. centerx = p.x;
  270. centery = p.y;
  271. }
  272. }
  273. void unionClusters(cluster c1, cluster c2) {
  274. if(c1.id == c2.id) return;
  275. for(int i=0;i<c2.sm.size();++i) {
  276. smartPixel pp = (smartPixel) c2.sm.get(i);
  277. c1.centerx = (c1.centerx*c1.sm.size()+pp.x)/(c1.sm.size()+1);
  278. c1.centery = (c1.centery*c1.sm.size()+pp.y)/(c1.sm.size()+1);
  279. pp.clust = c1;
  280. c1.sm.add(pp);
  281. }
  282. for(int i=0;i<clusters.size();++i) {
  283. cluster t = (cluster) clusters.get(i);
  284. if(t.id == c2.id) {
  285. clusters.remove(i);
  286. return;
  287. }
  288. }
  289. }