Advertisement
Guest User

Untitled

a guest
Apr 28th, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.99 KB | None | 0 0
  1. import processing.core.PApplet;
  2. import processing.core.PImage;
  3.  
  4.  
  5. @SuppressWarnings("serial")
  6. public class ImageProcessing extends PApplet {
  7. PImage img;
  8. HScrollbar thresholdBarHigh;
  9. HScrollbar thresholdBarLow;
  10.  
  11. public void setup() {
  12. size(800, 600);
  13. img = loadImage("board1.jpg");
  14. thresholdBarHigh = new HScrollbar(this,0,550,800,20);
  15. thresholdBarLow = new HScrollbar(this,0,580,800,20);
  16.  
  17. //noLoop(); // no interactive behaviour: draw() will be called only once.
  18. }
  19.  
  20. public void draw() {
  21. background(color(0,0,0));
  22. // image(img,0,0);
  23.  
  24. /*
  25. float thresholdValueHigh = thresholdBarHigh.getPos() * 255;
  26. float thresholdValueLow = thresholdBarLow.getPos() * 255;
  27.  
  28. image(hueImage(thresholdValueHigh, thresholdValueLow ,img), 0, 0);
  29.  
  30. thresholdBarHigh.display();
  31. thresholdBarHigh.update();
  32.  
  33. thresholdBarLow.display();
  34. thresholdBarLow.update();
  35. */
  36.  
  37. // image(applyKernel(img, getGaussianBlur()),900,0);
  38.  
  39.  
  40. // image(sobel(img), 900, 0);
  41.  
  42. image(hough(sobel(img)), 0,0);
  43.  
  44. // image(img,0,0);
  45. // hough(sobel(img));
  46. }
  47.  
  48.  
  49. private PImage thresholdBinary(float threshold, PImage img) {
  50. PImage result = createImage(width, height, RGB);
  51.  
  52. for(int i=0; i < img.width ; i++){
  53. for(int j=0; j < img.height ; j++){
  54.  
  55. if (brightness(img.get(i, j)) > threshold) {
  56. result.set(i, j, color(255));
  57. }
  58. else {
  59. result.set(i, j, color(0));
  60. }
  61.  
  62. }
  63. }
  64.  
  65. return result;
  66. }
  67.  
  68. private PImage thresholdInvertBinary(float threshold, PImage img) {
  69. PImage result = createImage(width, height, RGB);
  70.  
  71. for(int i=0; i < img.width ; i++){
  72. for(int j=0; j < img.height ; j++){
  73.  
  74. if (brightness(img.get(i, j)) > threshold) {
  75. result.set(i, j, color(0));
  76. }
  77. else {
  78. result.set(i, j, color(255));
  79. }
  80.  
  81. }
  82. }
  83.  
  84. return result;
  85. }
  86.  
  87.  
  88. private PImage hueImage(float thresholdHigh, float thresholdLow, PImage img) {
  89. //WILL ONLY KEEP THE PIXEL COLOR OF THE ORIGINAL IMAGE IFF BETWEEN THE TWO THRESHOLD
  90. PImage result = createImage(width, height, RGB);
  91.  
  92. for(int i=0; i < img.width ; i++){
  93. for(int j=0; j < img.height ; j++){
  94.  
  95. if (brightness(img.get(i, j)) < thresholdHigh &&
  96. brightness(img.get(i, j)) > thresholdLow) {
  97. result.set(i, j, img.get(i,j));
  98. }
  99. else {
  100. result.set(i, j, color(0));
  101. }
  102.  
  103. }
  104. }
  105.  
  106. return result;
  107. }
  108.  
  109.  
  110.  
  111. public PImage applyKernel(PImage img, float[][] kernel) {
  112.  
  113. float weight = 1.0f;
  114.  
  115. // create a greyscale image (type: ALPHA) for output
  116. PImage result = createImage(img.width, img.height, ALPHA);
  117.  
  118. // kernel size N = 3
  119. //
  120. // for each (x,y) pixel in the image:
  121. // - multiply intensities for pixels in the range
  122. // (x - N/2, y - N/2) to (x + N/2, y + N/2) by the
  123. // corresponding weights in the kernel matrix
  124. // - sum all these intensities and divide it by the weight
  125. // - set result.pixels[y * img.width + x] to this value
  126.  
  127. for(int i= 2; i < img.width-2; i++){
  128. for (int j = 2; j<img.height-2; j++){
  129.  
  130. int intensities = 0;
  131.  
  132. for(int x = i-1; x < i+1 ; x++){
  133. for(int y = j-1; y < j+1 ; y++){
  134. intensities += img.get(x,y) * kernel[x-i+1][y-j+1];
  135. }
  136. }
  137.  
  138. intensities /= weight;
  139. result.set(i, j, intensities);
  140. }
  141. }
  142.  
  143.  
  144. return result;
  145. }
  146.  
  147.  
  148.  
  149.  
  150.  
  151. public PImage sobel(PImage img) {
  152.  
  153. float[][] hKernel = { { 0, 1, 0 },
  154. { 0, 0, 0 },
  155. { 0, -1, 0 } };
  156.  
  157. float[][] vKernel = { { 0, 0, 0 },
  158. { 1, 0, -1 },
  159. { 0, 0, 0 } };
  160.  
  161.  
  162. PImage result = createImage(img.width, img.height, ALPHA);
  163.  
  164. // clear the image
  165. for (int i = 0; i < img.width * img.height; i++) {
  166. result.pixels[i] = color(0);
  167. }
  168.  
  169. float max=0;
  170. float[] buffer = new float[img.width * img.height];
  171.  
  172.  
  173. for(int i= 1; i < img.width-1; i++){
  174. for (int j = 1; j<img.height-1; j++){
  175.  
  176. int sum_h = 0;
  177. int sum_v = 0;
  178.  
  179. for(int x = i-1; x < i+2 ; x++){
  180. for(int y = j-1; y < j+2 ; y++){
  181. sum_h += img.get(x,y) * hKernel[x-i+1][y-j+1];
  182. sum_v += img.get(x,y) * vKernel[x-i+1][y-j+1];
  183. }
  184. }
  185.  
  186. float sum = sqrt(pow(sum_h,2) + pow(sum_v,2));
  187. buffer[j*img.width + i] = sum;
  188.  
  189. if(sum > max){
  190. max = sum;
  191. }
  192. }
  193. }
  194.  
  195.  
  196. for (int y = 2; y < img.height - 2; y++) { // Skip top and bottom edges
  197. for (int x = 2; x < img.width - 2; x++) { // Skip left and right
  198.  
  199. if (buffer[y * img.width + x] > (int)(max * 0.3f)) { // 30% of the max
  200. result.pixels[y * img.width + x] = color(255);
  201. } else {
  202. result.pixels[y * img.width + x] = color(0);
  203. }
  204. }
  205. }
  206.  
  207. return result;
  208. }
  209.  
  210.  
  211.  
  212. public PImage hough(PImage edgeImg) {
  213.  
  214. float discretizationStepsPhi = 0.06f;
  215. float discretizationStepsR = 2.5f;
  216.  
  217. // dimensions of the accumulator
  218. int phiDim = (int) (Math.PI / discretizationStepsPhi);
  219. int rDim = (int) (((edgeImg.width + edgeImg.height) * 2 + 1) / discretizationStepsR);
  220.  
  221. // our accumulator (with a 1 pix margin around)
  222. int[] accumulator = new int[(phiDim + 2) * (rDim + 2)];
  223.  
  224. // Fill the accumulator: on edge points (ie, white pixels of the edge
  225. // image), store all possible (r, phi) pairs describing lines going
  226. // through the point.
  227. for (int x = 0; x < edgeImg.width; x++) {
  228. for (int y = 0; y < edgeImg.height; y++) {
  229. // Are we on an edge?
  230. if (brightness(edgeImg.pixels[y * edgeImg.width + x]) != 0) {
  231.  
  232. for (float phi=0; phi < phiDim ; phi+= discretizationStepsPhi){
  233. double r = x*cos(phi) + y*sin(phi);
  234.  
  235. // r += (rDim-1)/2; //Center the r
  236.  
  237.  
  238. int accPhi = (int) (phi / discretizationStepsPhi);
  239. int accR = (int) ((r/discretizationStepsR) + (rDim-1)/2) ;
  240.  
  241. int idx = accR + 1 + (accPhi+1)*(rDim+2);
  242.  
  243. accumulator[ idx ]++; //increment the accumulator at pos (r,phi)
  244. }
  245. }
  246. }
  247. }
  248.  
  249.  
  250. PImage houghImg = createImage(rDim + 2, phiDim +2, ALPHA);
  251.  
  252. for(int i=0; i< accumulator.length; i++){
  253. houghImg.pixels[i] = color(min(255, accumulator[i]));
  254. }
  255.  
  256. houghImg.updatePixels();
  257.  
  258. return houghImg;
  259.  
  260.  
  261. /*
  262.  
  263. for (int idx = 0; idx < accumulator.length; idx++) {
  264. if (accumulator[idx] > 200) {
  265. // first, compute back the (r, phi) polar coordinates:
  266. int accPhi = (int) (idx / (rDim + 2)) - 1;
  267. int accR = idx - (accPhi + 1) * (rDim + 2) - 1;
  268. float r = (accR - (rDim - 1) * 0.5f) * discretizationStepsR;
  269. float phi = accPhi * discretizationStepsPhi;
  270.  
  271. // Cartesian equation of a line: y = ax + b
  272. // in polar, y = (-cos(phi)/sin(phi))x + (r/sin(phi))
  273. // => y = 0 : x = r / cos(phi)
  274. // => x = 0 : y = r / sin(phi)
  275.  
  276. // compute the intersection of this line with the 4 borders of
  277. // the image
  278. int x0 = 0;
  279. int y0 = (int) (r / sin(phi));
  280. int x1 = (int) (r / cos(phi));
  281. int y1 = 0;
  282. int x2 = edgeImg.width;
  283. int y2 = (int) (-cos(phi) / sin(phi) * x2 + r / sin(phi));
  284. int y3 = edgeImg.width;
  285. int x3 = (int) (-(y3 - r / sin(phi)) * (sin(phi) / cos(phi)));
  286.  
  287. // Finally, plot the lines
  288. stroke(204,102,0);
  289. if (y0 > 0) {
  290. if (x1 > 0)
  291. line(x0, y0, x1, y1);
  292. else if (y2 > 0)
  293. line(x0, y0, x2, y2);
  294. else
  295. line(x0, y0, x3, y3);
  296. }
  297. else {
  298. if (x1 > 0) {
  299. if (y2 > 0)
  300. line(x1, y1, x2, y2);
  301. else
  302. line(x1, y1, x3, y3);
  303. }
  304. else
  305. line(x2, y2, x3, y3);
  306. }
  307. }
  308. }
  309. */
  310.  
  311. }
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318. public float[][] getConvolute1(){
  319. float[][] kernel = { { 0, 0, 0 },
  320. { 0, 2, 0 },
  321. { 0, 0, 0 }};
  322. return kernel;
  323. }
  324.  
  325.  
  326. public float[][] getConvolute2(){
  327. float[][] kernel = { { 0, 1, 0 },
  328. { 1, 0, 1 },
  329. { 0, 1, 0 }};
  330. return kernel;
  331. }
  332.  
  333. public float[][] getGaussianBlur(){
  334. float[][] kernel = { { 9, 12, 9 },
  335. { 12, 15, 12 },
  336. { 9, 12, 9 }};
  337. return kernel;
  338. }
  339.  
  340. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement