如何使用Java从像素值创建灰度图像 [英] how to create a gray scale image from pixel values using java

查看:179
本文介绍了如何使用Java从像素值创建灰度图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求是,我需要将彩色图像转换为灰度图像,并将灰度图像的像素值获取到一个数组,然后对该数组执行某种加密算法,然后再次使用此更改后的像素数组,我需要转换返回/创建一个灰度图像并显示它。
这是我的疑问。

My requirement is that, I need to convert a color image into gray scale image and obtain pixels values of gray scale image to an array and perform some encryption algorithm on that array and again using this changed pixel array, I need to convert back/create a gray scale image and display it. Here are my doubts.


  1. 使用彩色图像,我获得了三个不同阵列中的RGB像素值。据我所知,可以通过执行 red + green + blue / 3 = gray 获得灰度像素。红色,蓝色,绿色,灰色是二维数组。这是获取灰度像素值的正确方法吗?

  1. Using the color image I have obtained the RGB pixel values in three different arrays. As per my knowledge, gray scale pixels can be obtained by doing red+green+blue/3=gray. Here red, blue, green, gray are 2-D arrays. Is this right way to obtain gray scale pixel values?

gray[x][y] = (r[x][y]+g[x][y]+b[x][y])/3;


  • 如果这是正确的,那么我可以轻松地对该数组执行算法。真正的问题出现在这里。如何使用该像素阵列转换回/创建灰度图像。展示如何使用像素值创建灰度图像的示例将非常有帮助。谢谢。

  • If this is correct, then I can easily perform algorithm on that array. The real problem arises here. How to convert back/create a gray scale image using that pixel array. An example to show how a gray scale image can be created using pixel values will be really helpful. Thank you.

    for(int x = 0;x<=height;x++) {
        for(int y = 0;y<=width;y++) {
            gray[x][y] = (r[x][y]+g[x][y]+b[x][y])/3;
            System.out.print(gray[x][y]+"\t");
        }
         System.out.println(" ");
    }
    



  • 推荐答案

    建议您创建一个新的TYPE_BYTE_GRAY BufferedImage,而不是通过手动操作将像素转换为灰度。然后您可以直接操纵像素。

    Instead of converting the image to grayscale by manipulating the pixels yourself I'd suggest creating a new BufferedImage of TYPE_BYTE_GRAY. Then you can manipulate the pixels directly.

    public static BufferedImage convertToGray(BufferedImage biColor)
    {
        BufferedImage biGray = new BufferedImage(biColor.getWidth(), biColor.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
        biGray.getGraphics().drawImage(biColor, 0, 0, null);
        return biGray;
    }
    
    public static void manipulatePixels(BufferedImage biGray)
    {
        byte [] imageData = ((DataBufferByte)biGray.getRaster().getDataBuffer()).getData();
    
        for (int i = 0; i < imageData.length; i++)
        {
            // do manipulation/encryption here - or on the entire array at once
        }
    }
    

    如果出于某些原因要从中创建全新的图像字节数组,只需创建一个新图像并将像素复制到其字节数组中即可。

    If for some reason you want to create a brand new image from the byte array, just create a new image and copy the pixels into its byte array.

    public static BufferedImage createImageFromArray(byte[] imageData, int width, int height)
    {
        BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
        byte [] newData = ((DataBufferByte) newImage.getRaster().getDataBuffer()).getData();
    
        for (int i = 0; i < imageData.length; i++)
        {
            newData[i] = imageData[i];
        }
        return newImage;
    }
    

    这篇关于如何使用Java从像素值创建灰度图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆