Java image processing

Java image processing

This paper introduces the implementation of a simple image processing interface

1. Front end interface production

First create the form object

		JFrame jf=new JFrame();
		jf.setSize(640,500);
		jf.setTitle("Beauty Camera");
		jf.setLocationRelativeTo(null);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//Set size, title, position, exit

Create a panel container JPanel for the menu and picture display area on the form

		//Set menu area
	    JPanel jmenu=new JPanel();
	    jme.setBackground(Color.BLACK);  
	    jf.add(jmenu,BorderLayout.CENTER); 
	    //Set picture area
 		JPanel jiamge = new JPanel();
 		jiamge.setBackground(jf.getBackground());
 		jf.add(jimage,BorderLayout.NORTH);

Use the Menu design interface to create the Menu bar on the form object. The Menu on the Menu bar consists of three parts:
javax.swing.JMenuBar class: a menu bar for placing menus
javax.swing.JMenu class: menu directory object, file and edit in the example
javax.swing.JMenuItem: menu bar directory, such as new, open, original, etc
Step 1: create a menu bar object
2. Create a menu directory and add the menu directory to the menu bar
3. Create menu items and add them to the menu directory. Add action listeners to each menu item

		JMenuBar Mb=new JMenuBar();
		jimage.add(Mb,BorderLayout.EAST);
 		String[] menu= {"file","edit"};
 		String[][] item= {{"newly build","open","sign out"},{"Original drawing","Grayscale","Mosaic","marker pen","vague","feature extraction "}};
 		for(int i=0;i<menu.length;i++) {
 			JMenu me=new JMenu(menu[i]);
 			Mb.add(me);
 			for(int j=0;j<item[i].length;j++) {
 				JMenuItem mi=new JMenuItem(item[i][j]);
 				me.add(mi);
 				mi.addActionListener(listener);
 			}
 		}	

Set the visibility of the component

	jf.setVisible(true);

Add a brush to the picture display area

	jf.setVisible(true);
	Graphics g=jmenu.getGraphics();

The interface design structure is shown in the figure below

2. File selector

For menu – file – select Add action listener to enable it to open the file selection window

 			  JFileChooser chooser = new JFileChooser();
			  chooser.showOpenDialog(null);//Open the file selection window
			  File file=chooser.getSelectedFile();//Get selected file
			  String path=chooser.getSelectedFile().getPath());//Get file path

3. Save pictures as an array

The color picture exists in the form of two-dimensional array in the computer. Its color is prepared by RGB three primary colors. The data information of each point of the color picture includes its position and RGB value. The way to process the picture, that is, change the array.

			//Save the image of the specified path as an array through the function
			public int[][]getIamgePixel(String path) {
			//Save the picture of the specified path
			File file = new File(path);
			//New buffer object
			BufferedImage bfi = null;
			try {
			//Read picture to buffer
				bfi = ImageIO.read(file);
			} catch (IOException e) {
				e.printStackTrace();
			}
			//Create a two-dimensional array according to the size of the picture
			int width=bfi.getWidth();
			int height=bfi.getHeight();
			int arr[][]=new int[width][height];
			for (int i=0;i<width;i++) {
				for (int j=0;j<height;j++) {
				//Save the RGB value of pixels to the corresponding position of the array
					int pixel=bfi.getRGB(i, j);
					arr[i][j]=pixel;
				}
			}
			return arr;
		}

4. Output the array as a picture

One method: create a buffer, set the RGB value of the two-dimensional array to the color of the brush in the buffer, and finally display the buffer

public void drawPixel(int[][] arrPixel){
		BufferedImage buffImage = new BufferedImage(arrPixel.length, arrPixel[0].length, 
				BufferedImage.TYPE_INT_RGB); 
		//Draw pixels on buffer (get buffer brush)
		Graphics buffG = buffImage.getGraphics();
		//Traverse the arrPixel pixel value and draw pixels
		for(int i=0;i<arrPixel.length;i++){
			for(int j=0;j<arrPixel[i].length;j++){
				int pixel = arrPixel[i][j];
				Color color = new Color(pixel);
//				Color newColor = new Color(newPixel,newPixel,newPixel);
				buffG.setColor(color);
				
				//Set pixel value
//				buffImage.setRGB(i, j,newColor.getRGB());
				
				//Draw pixels: JVM > OS > graphics card > Screen
				buffG.fillRect(i, j, 1, 1);
			}
		}
		//Display buffer
		g.drawImage(buffImage, 80, 80, null);
	}

Another method: draw points on the canvas without using the buffer.

			public void drawPixel(int[][] arr){	
			for(int i=0;i<arr.length;i++) {
				for(int j=0;j<arr[0].length;j++) {
				Color color =new Color(arr[i][j]);			
				g.setColor(color);
				g.drawLine(i,j,i,j);
				}
			}

5. Gray processing

The R, G and B values of gray are similar. With the increase of RGB value, the color gradually changes from light gray to dark gray. Therefore, the average value of RGB can be taken as the new R, G and B value.

public void drawgrey(int [][]arrPixel) {
		BufferedImage buffImage = new BufferedImage(arrPixel.length, arrPixel[0].length, 
				BufferedImage.TYPE_INT_RGB); 
		Graphics buffG = buffImage.getGraphics();
		for(int i=0;i<arrPixel.length;i++){
			for(int j=0;j<arrPixel[i].length;j++){
				int pixel = arrPixel[i][j];
				Color color = new Color(pixel);
				
				int grey=(color.getRed()+color.getGreen()+color.getBlue())/3;
				Color newColor = new Color(grey,grey,grey);
				
				buffG.setColor(color);				
				buffImage.setRGB(i, j,newColor.getRGB());
				//Draw pixels: JVM > OS > graphics card > Screen
//				buffG.fillRect(i, j, 1, 1);
			}
		}
		gr.drawImage(buffImage, 0, 0, null);
	}

6. Mosaic processing

One way of mosaic is to set the pixels in a certain area to the same color.
So you can set a color every 10 pixels.

public void drawmisake(int [][]arrPixel) {
			BufferedImage buffImage = new BufferedImage(arrPixel.length, arrPixel[0].length, 
					BufferedImage.TYPE_INT_RGB); 
			Graphics buffG = buffImage.getGraphics();
			for(int i=0;i<arrPixel.length;i=i+10){
				for(int j=0;j<arrPixel[i].length;j=j+10){
					int pixel = arrPixel[i][j];
					Color color = new Color(pixel);
					buffG.setColor(color);				
					buffG.fillRect(i, j, 10, 10);
				}
			}
			gr.drawImage(buffImage, 0, 0, null);
		}

7. Marker pen

The marker draws the desired line through the mouse, which is realized by calling the MouseDragged () method in MouseMotionListener.

public void mouseDragged(MouseEvent e){
    	System.out.println("drag ");
    	if(BottomName=="marker pen") {
    		System.out.println("dianji");
    		int x=e.getX();
    		int y=e.getY();
    		g.setColor(Color.red);
    		g.drawOval(x-4, y-4,8, 8);	
    	}
    }

8. Fuzzy processing

Blur processing, reset the color of the pixels of the original picture. The more common method is to weighted average the RGB value of the middle pixels and the RGB value of the surrounding pixels to obtain the new RGB value of the middle pixels.

		public void drawmisake(int [][]arrPixel) {
			for(int i=1;i<arr_width-2;i++) {
				for(int j=1;j<arr_length-2;j++) {
					r1_value[i][j]=(int)(0.094*r_value[i-1][j-1]+0.118*r_value[i][j-1]+0.094*r_value[i+1][j-1]+0.118*r_value[i-1][j]+0.147*r_value[i][j]+0.118*r_value[i+1][j]+0.094*r_value[i-1][j+1]+0.118*r_value[i][j+1]+0.094*r_value[i+1][j+1]);
					g1_value[i][j]=(int)(0.094*g_value[i-1][j-1]+0.118*g_value[i][j-1]+0.094*g_value[i+1][j-1]+0.118*g_value[i-1][j]+0.147*g_value[i][j]+0.118*g_value[i+1][j]+0.094*g_value[i-1][j+1]+0.118*g_value[i][j+1]+0.094*g_value[i+1][j+1]);
					b1_value[i][j]=(int)(0.094*b_value[i-1][j-1]+0.118*b_value[i][j-1]+0.094*b_value[i+1][j-1]+0.118*b_value[i-1][j]+0.147*b_value[i][j]+0.118*b_value[i+1][j]+0.094*b_value[i-1][j+1]+0.118*b_value[i][j+1]+0.094*b_value[i+1][j+1]);
					Color color =new Color(r1_value[i][j],g1_value[i][j],b1_value[i][j]);			
					g.setColor(color);
					g.drawLine(80+i,80+j,80+i,80+j);
				}
			}
		}

9. Feature extraction

//float filter[][]= {{0.8f,0,0.8f},{-1,1.8f,-1},{-1,0.8f,-1}};
		float filter[][]= {{0,-1,0},{-1,4,-1},{0,-1,0}};
		int temr[][]= new int[3][3];
		int temg[][]= new int[3][3];
		int temb[][]= new int[3][3];
		for(int i=0;i<178;i++) {
			for(int j=0;j<178;j++) {
				for(int y=0;y<3;y++) {
					for(int z=0;z<3;z++) {
					temr[y][z]=(int)(r_value[i+y][j+z]*filter[y][z]);	
					temg[y][z]=(int)(g_value[i+y][j+z]*filter[y][z]);	
					temb[y][z]=(int)(b_value[i+y][j+z]*filter[y][z]);	
					}
				}
				int rgb[]= {0,0,0};
				for(int m=0;m<3;m++) {
					for(int y=0;y<3;y++) {
						for(int z=0;z<3;z++) {
							rgb[m]+=temr[y][z];		
						}
					}
					if(rgb[m]<0)rgb[m]=0;
				if(rgb[m]>255)rgb[m]=255;
				}
				r1_value[i][j]=rgb[0];
				g1_value[i][j]=rgb[1];
				b1_value[i][j]=rgb[2];	
			}
		}
		for(int i1=0;i1<arr_width-2;i1++) {
			for(int j=0;j<arr_length-2;j++) {
				Color color =new Color(r1_value[i1][j],g1_value[i1][j],b1_value[i1][j]);			
				g.setColor(color);
				g.drawLine(80+i1,80+j,80+i1,80+j);
			}
		}

Keywords: Java

Added by stangoe on Fri, 04 Mar 2022 08:23:31 +0200