Grayscale processing method of Java picture

Source: Internet
Author: User

By looking at the summary of the various Daniel on the net, and self-test summary of the Java Image Grayscale processing method

(1) The pixels in the image we are familiar with have RGB values.

(2) The image grayscale method is roughly divided into four kinds, the first is the maximum method (take the maximum value of the color RGB as the gray value); the second is the minimum method (the minimum value of the color RGB is used as the gray value); The third is the mean method (the average value of the RGB color is the grayscale value) The fourth is the weighted method of grayscale (how to weighted the most suitable, the best effect, Baidu Encyclopedia is said to be comprehensive).

(3) Don't say a word, record the effect and code I have implemented in the four methods described above:

Original

According to the above four methods, the effect of grayscale is as follows four figure

(4) The example code is as follows

Package Testhuidu;import Java.awt.image.bufferedimage;import Java.io.file;import java.io.ioexception;import  Javax.imageio.imageio;public class Testhuidu {/** * color component converted to RGB value * @param alpha * @param red * @param green * @param Blue * @return */private static int colortorgb (int alpha, int red, int green, int blue) {int newpixel = 0;newpixel + = Alpha;ne  Wpixel = Newpixel << 8;newpixel + = Red;newpixel = Newpixel << 8;newpixel + = Green;newpixel = Newpixel << 8;newpixel + = Blue;return newpixel;} public static void Main (string[] args) throws IOException {grayimage (1, "ff.jpg", "1.jpg");//maximum grayscale grayimage (2, "ff.jpg "2.jpg");//Minimum Gray grayimage (3, "ff.jpg", "3.jpg");//Average method grayscale Grayimage (4, "ff.jpg", "4.jpg");//Weighted method grayscale}/** * Image Grayscale method * @param status Grayscale method, 1 means the maximum method, 2 means the minimum value method, 3 means the mean value method, 4 weighted method * @param imagePath The position of the image that needs to be grayscale * @param the location of the new grayscale image generated after grayscale processing Outpath Place * @throws ioexception */public static void grayimage (int status,string imagePath, String outpath) throws IOException {F ile file = nEW File (ImagePath); BufferedImage image = Imageio.read (file), int width = image.getwidth (); int height = image.getheight (); BufferedImage grayimage = new BufferedImage (width, height, image.gettype ());//bufferedimage grayimage = new Bufferedimag E (width, height, bufferedimage.type_byte_gray); for (int i = 0; i < width; i++) {for (int j = 0; j < height; J + +) {I NT color = Image.getrgb (i, j); final int r = (color >>) & 0xff;final int g = (color >> 8) & 0xff;fin Al int b = color & 0xff;int gray=0;if (status==1) {Gray=getbigger (R, G, b);//Max-method grayscale}else if (status==2) {Gray=getsmall ( R, G, b);//Minimum method grayscale}else if (status==3) {Gray=getavg (R, G, b);//mean grayscale}else if (status==4) {gray = (int) (0.3 * r + 0.59 * g + 0.11 * b);//Weighted method grayscale}system.out.println ("pixel coordinates:" + "x=" + i + "y=" + j + "Grayscale value =" + Gray); Grayimage.setrgb (i, J, ColorTo RGB (0, Gray, Gray, Gray));}} File NewFile = new file (Outpath); Imageio.write (grayimage, "JPG", newFile);} Compares the size of a three number public static int Getbigger (int x,intY,int z) {if (x>=y&&x>=z) {return x;} else if (y>=x&&y>=z) {return y;} else if (z>=x&&z>=y) {return z;} Else{return 0;}} Compare three is the size of the smallest public static int getsmall (int x,int y,int z) {if (x<=y&&x<=z) {return x;} else if (y>=x&&y>=z) {return y;} else if (z>=x&&z>=y) {return z;} Else{return 0;}} The mean method public static int getavg (int x,int y,int z) {int avg= (X+Y+Z)/3;return avg;}}

  

Grayscale processing method of Java picture

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.