Advertisement
rabirajkhadka

Histogram

Feb 6th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. /**
  2. * Histogram Viewer derived from the "Histogram" built-in example sketch.
  3. *
  4. * Calculates the histogram based on the image from the camera feed.
  5. */
  6.  
  7. import gohai.glvideo.*;
  8. GLCapture video;
  9.  
  10. void setup() {
  11. size(640, 480, P2D);
  12.  
  13. String[] devices = GLCapture.list();
  14. println("Devices:");
  15. printArray(devices);
  16.  
  17. // Use camera resolution of 640x480 pixels at 24 frames per second
  18. video = new GLCapture(this, devices[0], 640, 480, 24);
  19. video.start();
  20. }
  21.  
  22. void draw() {
  23. background(0);
  24. if (video.available()) {
  25. video.read();
  26. }
  27.  
  28. image(video, 0, 0);
  29. int[] hist = new int[256];
  30.  
  31. // Calculate the histogram
  32. for (int i = 0; i < video.width; i++) {
  33. for (int j = 0; j < video.height; j++) {
  34. int bright = int(brightness(get(i, j)));
  35. hist[bright]++;
  36. }
  37. }
  38.  
  39. // Find the largest value in the histogram
  40. int histMax = max(hist);
  41.  
  42. stroke(255);
  43. // Draw half of the histogram (skip every second value)
  44. for (int i = 0; i < video.width; i += 2) {
  45. // Map i (from 0..img.width) to a location in the histogram (0..255)
  46. int which = int(map(i, 0, video.width, 0, 255));
  47. // Convert the histogram value to a location between
  48. // the bottom and the top of the picture
  49. int y = int(map(hist[which], 0, histMax, video.height, 0));
  50. line(i, video.height, i, y);
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement