Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 写入图像时获取透明图像_Java_Opencv_Image Processing_Javax.imageio - Fatal编程技术网

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

Java 写入图像时获取透明图像,java,opencv,image-processing,javax.imageio,Java,Opencv,Image Processing,Javax.imageio,我使用openCv应用过滤器时遇到了一个问题。 我在这段特定代码中做的是,首先尝试获取灰度图像,但保留颜色通道,效果很好,但当我使用图像IO写入文件时,我发现alpha已被更改。所以我检查了BGRA和ABGR颜色空间,但它仍然不起作用,给了我一个透明的图像 public static BufferedImage sepia(BufferedImage image,int intensity) { Mat imageMat = bufferedToMat(image); int s

我使用openCv应用过滤器时遇到了一个问题。 我在这段特定代码中做的是,首先尝试获取灰度图像,但保留颜色通道,效果很好,但当我使用图像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();
    }
}
输入图像:

输出图像:


我忘了补充一点,做ABGR会得到相同的带有redish色调的透明图像。可能使用
CV\u 8UC3
?或者将
数据[3]
设置为相关内容?我在输出图像中看不到任何透明或重新着色。。。?还要注意,
double gray=(红色+蓝色+绿色)/3.0不是转换为灰度的方式,不同的颜色具有非常不同的强度。
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;
}