import java.awt.color.ColorSpace; import java.awt.image.*; import java.awt.*; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import java.util.PriorityQueue; import java.util.Scanner; import javax.swing.JFrame; import java.awt.event.*; public class Iscissor { private static int z=1,w=1,xb,click_count=0,live_wire_length,min_x=0,min_y=0,max_x=0,max_y=0,zn=1,wn=1,xbn=0; private static BufferedImage img_copy= null; public static int SIZE_MAX=1000; //This is the maximum no. of points in the returning array when the shortest_path is run between two points public static int LIVE_SIZE_MAX=5000; //This is the maximum no. of points on the boundary of cropped image public static final float[] XDERIV = { -1f, 0f, 1f, // horizontal derivative filter kernel -1f, 0f, 1f, -1f, 0f, 1f }; private static BufferedImage convertToGrayscale(BufferedImage source) { BufferedImageOp op = new ColorConvertOp( ColorSpace.getInstance(ColorSpace.CS_GRAY), null); return op.filter(source, null); } private static BufferedImage copyBufferedImage (BufferedImage source) { BufferedImage target = new BufferedImage(source.getWidth(), source.getHeight(), source.getType()); Graphics g = target.getGraphics(); g.drawImage(source, 0, 0, null); return target; } public static double absolute(double value) { if (value<0) value=-value; return value; } private static BufferedImage readImage() { final String dir = "/host/Users/Arijit/Pictures/CMSC426/"; //The path needs to be changed from a machine to another BufferedImage img = null; BufferedImage imggray = null; String fname; Scanner scan = new Scanner (System.in); System.out.println ("Enter file name:"); fname = scan.nextLine(); File dirfile = new File(dir + fname); try { img = ImageIO.read(dirfile); } catch (IOException e) {System.out.println("Image not read"); } // imggray = convertToGrayscale(img); return img; } public static BufferedImage setPixel (BufferedImage image, int x, int y, Color color,int value ) { Graphics g = image.getGraphics( ); g.setColor( color ); g.fillRect( x, y, 3, 3 ); if(value==1) {g.fillRect( x, y, 6, 6 );} g.dispose( ); return image; } private static double[][][] getDerivativAarray(BufferedImage img,int hgt,int wdth) {double arr[][][]=new double[hgt][wdth][8]; //The arr[][][] is the array in which costs of eight links around each of the pixel is stored and returned. //This array is used to calculate the shortest path. //This method is called from the following LIVEWIRE method. int arr_store_pixel[][]=new int[hgt][wdth]; byte pix[]=new byte[1]; double max; int i,j; System.out.println("stage 0"); //initialize the array which will be returned for (i=0;i=1){ // arr_ret=Short_Path_modified.find_shortest_path(arr,array1[p-1],array1[p],img_height,img_width); arr_ret=Linear_path.find_linear_path(array1[p-1], array1[p]); //array1[p-1] is the starting point and array1[p] is the end point for which the SHORTEST PATH METHOD will be called. //The method should return an array(arr_ret) of size SIZE_MAX with the end point being stored in arr_ret[0] and then //points on the shortest path are stored(arr_ret[1] onwards) until we reach the starting point.(exclude the starting point). //arr_ret[i][0] will store the X-coordinate of the i-th point and arr_ret[i][1] will store the Y-coordinate of the i-th point. //REMEMBER : X and Y coordinates are opposite to the graphics coordinates.(X is vertical and Y is horizontal in our usage) distance_terminate=(array1[0][0]-array1[p][0])*(array1[0][0]-array1[p][0])+(array1[0][1]-array1[p][1])*(array1[0][1]-array1[p][1]); live_temp=live_index; for(int k=0;k0 && arr_ret[k][1]>0){ livew[live_index][0]=arr_ret[k][0]; livew[live_index][1]=arr_ret[k][1]; live_index++; } } for (int k=0;k pq=new PriorityQueue(); for(int i=0;imax_x) max_x=live_wire[l][0]; if (live_wire[l][1]>max_y) max_y=live_wire[l][1]; live_pair[l][0]=0; live_pair[l][1]=0; } Point point1=new Point(); Point point_expand=new Point(); int temp_g=0,temp_r=0,temp_b=0,pixel=0; point1.cost=2.0; Test_q.Insert(pq,(min_x+max_x)/2,(min_y+max_y)/2, 0.00);// This is the start point and it must be within the cropping region //I think the probability of that point to be within the cropping region is maximum. data_index=0; while(pq.size()!=0){ point_expand=pq.poll(); array_ind[point_expand.x][point_expand.y]=2; live_data[data_index][0]=point_expand.x; live_data[data_index][1]=point_expand.y; live_data[data_index][2]=img_copy.getRGB(point_expand.y,point_expand.x); data_index++; for(int i=-1;i<=1;i++){ for(int j=-1;j<=1;j++){ pixel=img.getRGB(point_expand.y+j, point_expand.x+i); temp_r = (pixel >> 16) & 0xff; temp_g = (pixel >> 8) & 0xff; temp_b = (pixel >> 0) & 0xff; if((temp_g!=255||(temp_b!=0||temp_r!=0)) && array_ind[point_expand.x+i][point_expand.y+j]==0 && point_expand.x+i1 && point_expand.y+j>1){ array_ind[point_expand.x+i][point_expand.y+j]=1; Test_q.Insert(pq,point_expand.x+i ,point_expand.y+j, 0.00); } } } } while(xbn!=1) { System.out.println("Paste the image" + data_index); } for(int l=0;l127){ live_data[l][2]=-(256-live_data[l][2]); } pix[0]=(byte) live_data[l][2]; imgc.setRGB(live_data[l][1]-(min_y+max_y)/2+zn,live_data[l][0]-(min_x+max_x)/2+wn,live_data[l][2]); } } for(int k=0;k<20;k++){// 20 the maximum no. of times you can change your //decision where you would place your image finally in the new image imgc=copyBufferedImage(img_temp); while(xbn==0){System.out.println("Paste the image again if you want");} for(int l=0;l127){ live_data[l][2]=-(256-live_data[l][2]); } pix[0]=(byte) live_data[l][2]; imgc.setRGB(live_data[l][1]-(min_y+max_y)/2+zn,live_data[l][0]-(min_x+max_x)/2+wn,live_data[l][2]); } } MyImagePanel paneln1 = new MyImagePanel (imgc,3200,3200); fn.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); fn.setPreferredSize(new Dimension (img_width_n,img_height_n)); if(xbn==1) {fn.getContentPane().add(paneln1);} if(xbn==3) break; fn.pack(); fn.setVisible(true); xbn=0; } } public static void main(String[] args) { BufferedImage img = null; BufferedImage imgc=null; BufferedImage imgderiv = null; int img_height,img_width; img = readImage(); imgc=readImage(); img_copy=copyBufferedImage(img); // This isn't doing anything for your program. It is showing an example of // computing the derivative in the x direction, and writing the result to temp.jpg. /* ConvolveOp cop = new ConvolveOp(new Kernel(3, 3, XDERIV), ConvolveOp.EDGE_NO_OP, null); imgderiv = cop.filter(img, imgderiv); try { File outputfile = new File("temp.jpg"); ImageIO.write(imgderiv, "jpeg", outputfile); } catch (IOException e) { System.out.println("Image not saved"); }*/ System.out.println ("Done"); img_height = img.getHeight(); img_width = img.getWidth(); int live_wire[][]=livewire(img,img_height,img_width); copypaste(img,imgc,img_height,img_width,live_wire); } static class AL extends MouseAdapter{ public void mouseClicked(MouseEvent e){ z=e.getX(); w=e.getY(); xb=e.getButton(); click_count++; } } static class ALn extends MouseAdapter{ public void mouseClicked(MouseEvent e){ zn=e.getX(); wn=e.getY(); xbn=e.getButton(); click_count++; } } }