Guest User

Untitled

a guest
Jul 22nd, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.26 KB | None | 0 0
  1. PImage img, imgOriginal;//PImages
  2. byte[][] imageArray;//byte array for image
  3. byte[][] maskArray;//byte array for pixels that need to be removed
  4. int currentImage = 0;//sets current image to inital image
  5. String[] images = {"print1.png", "print1b.png", "print2.png", "print2b.png", "print3.png", "print3b.png"};//index to hold which image is being loaded
  6.  
  7. byte[][]thinnedArray;
  8.  
  9. void thinnedImage(byte[][]thinnedArray){
  10. for(int i = 1; i<img.width-1; i++){
  11. for (int j= 1; j<img.height-1;j++){
  12. if (brightness(img.get(i, j)) < 127){
  13. thinnedArray[i][j] = 1;//pixel is black
  14. }
  15. else
  16. {
  17. thinnedArray[i][j] = 0;//pixel is white
  18. }
  19. }
  20. }
  21. }
  22.  
  23.  
  24. void loadPixelArray(PImage anImage){//Loads pixels from its image into its pixel array
  25. anImage.loadPixels();
  26. }
  27.  
  28. color getPixelColor(PImage anImage, int x, int y){//gets color of pixel from its pixel array at point x,y
  29. color pixelColor = anImage.get(x,y);
  30. return pixelColor;
  31. }
  32.  
  33. void refreshImagePixels(PImage anImage){//refreshes pixels in pixel array, use to change pixels in image to what is in its array
  34. anImage.updatePixels();
  35. }
  36.  
  37. void setPixelColor(PImage anImage, int x, int y, color aColor){//sets the color of a pixel in an images pixel array to the color specified.
  38. anImage.set(x,y,aColor);
  39. }
  40.  
  41. void initImage(String fileName){//Creates initial Image and acts like setup
  42. img = loadImage(fileName);//loads the image from the image array that will be thinned
  43. imgOriginal = loadImage(fileName);//this stores the image for side by side comparison
  44. size(img.width * 2, img.height);//screen size
  45. loadPixelArray(img);//loads the pixels into into img pixel array
  46. imageArray = new byte[img.width][img.height];//array to determine whether a pixel is black or white
  47. thinnedArray= new byte[img.width][img.height];
  48. maskArray = new byte[img.width][img.height];//array that stores pixels that need to be removed
  49. for (int col = 1; col < img.width - 1; col++){//filling the arrays with respective values
  50. for (int row=1; row<img.height-1; row++){
  51. maskArray[col][row] = 0;
  52. if (brightness(img.get(col, row)) < 127){
  53. imageArray[col][row] = 1;//pixel is black
  54. }
  55. else
  56. {
  57. imageArray[col][row] = 0;//pixel is white
  58. }
  59. }
  60. }
  61. }
  62.  
  63. void setup(){
  64. initImage(images[0]);//call the procedure and puts the first image
  65. }
  66.  
  67. void mousePressed(){//when mouse is pressed, the image is cycled to the next one in the array, then loops back when it reaches the end of the array
  68. currentImage = currentImage + 1;
  69. if (currentImage >= images.length){
  70. currentImage = 0;
  71. }
  72. initImage(images[currentImage]);
  73. }
  74.  
  75. void thinning(PImage img, byte[][] imageArray, byte [][] maskArray){//thinning procedure
  76. for(int a = 0; a<2;a++){//hardcoded value for now
  77. for(int i = 1; i<img.width-1; i++){//goes through each row
  78. for (int j =1; j<img.height-1;j++){//goes through each column
  79. if (imageArray[i][j]==1){//checks if pixel is black
  80. //First Pass, Black below and White above
  81. if(imageArray[i][j-1]==0 && imageArray[i][j+1]==1){//checks if pixel above is white and pixel below is black
  82. if(imageArray[i-1][j]== 1 || imageArray[i-1][j+1]==1 || imageArray[i-1][j-1]==1 || imageArray[i+1][j]==1 || imageArray[i+1][j-1]==1 || imageArray[i+1][j+1] == 1){//checks if any nearby pixels are black
  83. if ((1-imageArray[i+1][j]) - ((1-imageArray[i+1][j])*(1-imageArray[i+1][j-1])*(1-imageArray[i][j-1])) + (1-imageArray[i][j-1]) - ((1-imageArray[i][j-1])*(1-imageArray[i-1][j-1])*(1-imageArray[i-1][j])) + (1-imageArray[i-1][j]) - ((1-imageArray[i-1][j])*(1-imageArray[i-1][j+1])*(1-imageArray[i][j+1])) + (1-imageArray[i][j+1]) - ((1-imageArray[i][j+1])*(1-imageArray[i+1][j+1])*(1-imageArray[i+1][j]))==1){//Determine connectivity
  84. maskArray[i][j] = 1;//Says that this pixel will need to be removed
  85. }
  86. }
  87. }
  88. }
  89. }
  90. }
  91. processImagePixels();//procedure to refresh image and remove pixels from that pass
  92. for(int i = 0; i<img.width-1; i++){
  93. for (int j =0; j<img.height-1;j++){
  94. if (imageArray[i][j]==1){
  95. if(imageArray[i-1][j]==0 && imageArray[i+1][j]== 1){
  96. if(imageArray[i-1][j+1]== 1 || imageArray[i-1][j-1]==1 || imageArray[i][j-1]==1 || imageArray[i][j+1]==1 || imageArray[i+1][j-1]==1 || imageArray[i+1][j+1] == 1){
  97. if ((1-imageArray[i+1][j]) - ((1-imageArray[i+1][j])*(1-imageArray[i+1][j-1])*(1-imageArray[i][j-1])) + (1-imageArray[i][j-1]) - ((1-imageArray[i][j-1])*(1-imageArray[i-1][j-1])*(1-imageArray[i-1][j])) + (1-imageArray[i-1][j]) - ((1-imageArray[i-1][j])*(1-imageArray[i-1][j+1])*(1-imageArray[i][j+1])) + (1-imageArray[i][j+1]) - ((1-imageArray[i][j+1])*(1-imageArray[i+1][j+1])*(1-imageArray[i+1][j]))==1){
  98. maskArray[i][j] = 1;
  99. }
  100. }
  101. }
  102. }
  103. }
  104. }
  105. processImagePixels();
  106. for(int i = 1; i<img.width-1; i++){
  107. for (int j =1; j<img.height-1;j++){
  108. if (imageArray[i][j]==1){
  109. if(imageArray[i][j+1]==0 && imageArray[i][j-1]== 1){
  110. if(imageArray[i-1][j+1]== 1 || imageArray[i-1][j-1]==1 || imageArray[i-1][j]==1 || imageArray[i+1][j]==1 || imageArray[i+1][j-1]==1 || imageArray[i+1][j+1] == 1){
  111. if ((1-imageArray[i+1][j]) - ((1-imageArray[i+1][j])*(1-imageArray[i+1][j-1])*(1-imageArray[i][j-1])) + (1-imageArray[i][j-1]) - ((1-imageArray[i][j-1])*(1-imageArray[i-1][j-1])*(1-imageArray[i-1][j])) + (1-imageArray[i-1][j]) - ((1-imageArray[i-1][j])*(1-imageArray[i-1][j+1])*(1-imageArray[i][j+1])) + (1-imageArray[i][j+1]) - ((1-imageArray[i][j+1])*(1-imageArray[i+1][j+1])*(1-imageArray[i+1][j]))==1){
  112. maskArray[i][j] = 1;
  113. }
  114. }
  115. }
  116. }
  117. }
  118. }
  119. processImagePixels();
  120. for(int i = 1; i<img.width-1; i++){
  121. for (int j =1; j<img.height-1;j++){
  122. if (imageArray[i][j]==1){
  123. if(imageArray[i+1][j]==0 && imageArray[i-1][j]== 1){
  124. if(imageArray[i-1][j+1]== 1 || imageArray[i-1][j-1]==1 || imageArray[i][j-1]==1 || imageArray[i][j+1]==1 || imageArray[i+1][j-1]==1 || imageArray[i+1][j+1] == 1){
  125. if ((1-imageArray[i+1][j]) - ((1-imageArray[i+1][j])*(1-imageArray[i+1][j-1])*(1-imageArray[i][j-1])) + (1-imageArray[i][j-1]) - ((1-imageArray[i][j-1])*(1-imageArray[i-1][j-1])*(1-imageArray[i-1][j])) + (1-imageArray[i-1][j]) - ((1-imageArray[i-1][j])*(1-imageArray[i-1][j+1])*(1-imageArray[i][j+1])) + (1-imageArray[i][j+1]) - ((1-imageArray[i][j+1])*(1-imageArray[i+1][j+1])*(1-imageArray[i+1][j]))==1){
  126. maskArray[i][j] = 1;
  127. }
  128. }
  129. }
  130. }
  131. }
  132. }
  133. processImagePixels();
  134. }
  135. }
  136.  
  137.  
  138.  
  139. void processImagePixels(){
  140. for(int i = 0; i<img.width; i++){
  141. for (int j =0 ; j<img.height;j++){
  142. if (maskArray[i][j] == 1){
  143. setPixelColor(img,i,j, color(255));
  144. }
  145. maskArray[i][j] = 0;
  146. if (brightness(img.get(i, j)) < 127){
  147. imageArray[i][j] = 1;
  148. }
  149. else
  150. {
  151. imageArray[i][j] = 0;
  152. }
  153. }
  154. refreshImagePixels(img);
  155. }
  156. }
  157.  
  158.  
  159. void draw(){
  160. image(imgOriginal,0,0);
  161. thinning(img, imageArray, maskArray);
  162. image(img,img.width,0);
  163. }
Add Comment
Please, Sign In to add comment