在写入图像时获取透明图像

时间:2016-04-17 21:46:39

标签: java opencv image-processing javax.imageio

我使用openCv来应用过滤器并遇到了问题。 我在这个特定的代码中所做的是首先尝试获得灰度图像,但保留它工作正常的颜色通道但是当我使用Image IO写入文件时,我发现alpha已被更改。所以我检查了BGRA和ABGR色彩空间,但它仍然不起作用,并给我一个透明的图像。

public static BufferedImage sepia(BufferedImage image,int intensity)
{
    Mat imageMat = bufferedToMat(image);
    int sepiaDepth = 20;
    int width = image.getWidth();
    int height = image.getHeight();
    Mat grayScaleMat = new Mat(imageMat.height(),imageMat.width(),CvType.CV_8UC4);
    imageMat.copyTo(grayScaleMat);
   // double[] test = imageMat.get(0, 0);
   // System.out.println(test[0]+" "+test[1]+" "+test[2]+" "+test[3]);
    for(int i=0;i<grayScaleMat.cols();i++)
    {
        for(int j=0;j<grayScaleMat.rows();j++)
        {
            //can be optimised
            double[] data = grayScaleMat.get(j, i);
            //System.out.println(data.length);

            double blue = data[0];
            double green =  data[1];
            double red = data[2];
            //System.out.println(red+" "+blue+" "+green);
            double gray = (red + blue + green)/3.0;
            //data[0] = gray;
            data[0] = gray;
            data[1] = gray;
            data[2] = gray;
            grayScaleMat.put(j, i, data);

        }
    }

    return (Utility.matToBuffered(grayScaleMat));
}


//Only Testing Remove Later
public static void main(String[] args)
{
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    int beta  = 25;
    String imagePath = "/home/arjun/Pictures/Lenna.png";
    BufferedImage image = null;
    try{

        image = ImageIO.read(new File(imagePath));
    }catch(IOException e)
    {
        e.printStackTrace();
    }
    int x = image.getType();
    System.out.println(x);
    BufferedImage output = sepia(image,beta);
    int y = output.getType();
    System.out.println(y);
    File outputfile = new File("/home/arjun/Pictures/sepia2.png");
    try{

        ImageIO.write(output, "png", outputfile);
    }catch(IOException e)
    {
        e.printStackTrace();
    }
}

此处的缓冲和匹配转换

public static Mat bufferedToMat(BufferedImage image)
{
    byte[] pixels = ((DataBufferByte)image.getRaster().getDataBuffer()).getData();
    Mat imageMat = new Mat(image.getHeight(),image.getWidth(),CvType.CV_8UC4);
    imageMat.put(0, 0, pixels);
    return imageMat;
}


public static BufferedImage matToBuffered(Mat imageMat)
{
    BufferedImage out;
    byte[] data = new byte[imageMat.cols()*imageMat.rows()*(int)imageMat.elemSize()];
    imageMat.get(0, 0,data);
    int type = BufferedImage.TYPE_3BYTE_BGR;
    if(imageMat.channels() == 1)
    {
        type = BufferedImage.TYPE_BYTE_GRAY;
    }
    else if(imageMat.channels() == 3)
    {
        type = BufferedImage.TYPE_3BYTE_BGR;
    }
    else if(imageMat.channels() == 4)
    {
        type = BufferedImage.TYPE_4BYTE_ABGR;
    }
    out = new BufferedImage(imageMat.cols(),imageMat.rows(),type);
    out.getRaster().setDataElements(0,0,imageMat.cols(),imageMat.rows(),data);  
    return out;
}

输入图片:

enter image description here

输出图片:

enter image description here

0 个答案:

没有答案