Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PImage img, imgOriginal;//PImages
- byte[][] imageArray;//byte array for image
- byte[][] maskArray;//byte array for pixels that need to be removed
- int currentImage = 0;//sets current image to inital image
- String[] images = {"print1.png", "print1b.png", "print2.png", "print2b.png", "print3.png", "print3b.png"};//index to hold which image is being loaded
- byte[][]thinnedArray;
- void thinnedImage(byte[][]thinnedArray){
- for(int i = 1; i<img.width-1; i++){
- for (int j= 1; j<img.height-1;j++){
- if (brightness(img.get(i, j)) < 127){
- thinnedArray[i][j] = 1;//pixel is black
- }
- else
- {
- thinnedArray[i][j] = 0;//pixel is white
- }
- }
- }
- }
- void loadPixelArray(PImage anImage){//Loads pixels from its image into its pixel array
- anImage.loadPixels();
- }
- color getPixelColor(PImage anImage, int x, int y){//gets color of pixel from its pixel array at point x,y
- color pixelColor = anImage.get(x,y);
- return pixelColor;
- }
- void refreshImagePixels(PImage anImage){//refreshes pixels in pixel array, use to change pixels in image to what is in its array
- anImage.updatePixels();
- }
- 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.
- anImage.set(x,y,aColor);
- }
- void initImage(String fileName){//Creates initial Image and acts like setup
- img = loadImage(fileName);//loads the image from the image array that will be thinned
- imgOriginal = loadImage(fileName);//this stores the image for side by side comparison
- size(img.width * 2, img.height);//screen size
- loadPixelArray(img);//loads the pixels into into img pixel array
- imageArray = new byte[img.width][img.height];//array to determine whether a pixel is black or white
- thinnedArray= new byte[img.width][img.height];
- maskArray = new byte[img.width][img.height];//array that stores pixels that need to be removed
- for (int col = 1; col < img.width - 1; col++){//filling the arrays with respective values
- for (int row=1; row<img.height-1; row++){
- maskArray[col][row] = 0;
- if (brightness(img.get(col, row)) < 127){
- imageArray[col][row] = 1;//pixel is black
- }
- else
- {
- imageArray[col][row] = 0;//pixel is white
- }
- }
- }
- }
- void setup(){
- initImage(images[0]);//call the procedure and puts the first image
- }
- 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
- currentImage = currentImage + 1;
- if (currentImage >= images.length){
- currentImage = 0;
- }
- initImage(images[currentImage]);
- }
- void thinning(PImage img, byte[][] imageArray, byte [][] maskArray){//thinning procedure
- for(int a = 0; a<2;a++){//hardcoded value for now
- for(int i = 1; i<img.width-1; i++){//goes through each row
- for (int j =1; j<img.height-1;j++){//goes through each column
- if (imageArray[i][j]==1){//checks if pixel is black
- //First Pass, Black below and White above
- if(imageArray[i][j-1]==0 && imageArray[i][j+1]==1){//checks if pixel above is white and pixel below is black
- 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
- 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
- maskArray[i][j] = 1;//Says that this pixel will need to be removed
- }
- }
- }
- }
- }
- }
- processImagePixels();//procedure to refresh image and remove pixels from that pass
- for(int i = 0; i<img.width-1; i++){
- for (int j =0; j<img.height-1;j++){
- if (imageArray[i][j]==1){
- if(imageArray[i-1][j]==0 && imageArray[i+1][j]== 1){
- 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){
- 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){
- maskArray[i][j] = 1;
- }
- }
- }
- }
- }
- }
- processImagePixels();
- for(int i = 1; i<img.width-1; i++){
- for (int j =1; j<img.height-1;j++){
- if (imageArray[i][j]==1){
- if(imageArray[i][j+1]==0 && imageArray[i][j-1]== 1){
- 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){
- 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){
- maskArray[i][j] = 1;
- }
- }
- }
- }
- }
- }
- processImagePixels();
- for(int i = 1; i<img.width-1; i++){
- for (int j =1; j<img.height-1;j++){
- if (imageArray[i][j]==1){
- if(imageArray[i+1][j]==0 && imageArray[i-1][j]== 1){
- 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){
- 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){
- maskArray[i][j] = 1;
- }
- }
- }
- }
- }
- }
- processImagePixels();
- }
- }
- void processImagePixels(){
- for(int i = 0; i<img.width; i++){
- for (int j =0 ; j<img.height;j++){
- if (maskArray[i][j] == 1){
- setPixelColor(img,i,j, color(255));
- }
- maskArray[i][j] = 0;
- if (brightness(img.get(i, j)) < 127){
- imageArray[i][j] = 1;
- }
- else
- {
- imageArray[i][j] = 0;
- }
- }
- refreshImagePixels(img);
- }
- }
- void draw(){
- image(imgOriginal,0,0);
- thinning(img, imageArray, maskArray);
- image(img,img.width,0);
- }
Add Comment
Please, Sign In to add comment