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
OpenCV和Windows 10透明图像_Opencv_Windows 10_Uwp - Fatal编程技术网

OpenCV和Windows 10透明图像

OpenCV和Windows 10透明图像,opencv,windows-10,uwp,Opencv,Windows 10,Uwp,假设我们有以下颜色: const Scalar TRANSPARENT2 = Scalar(255, 0, 255,0); 它是品红色但完全透明:alpha=0(完全不透明为255) 现在,我根据以下内容进行了测试: WriteableBitmap^Grabcut::TestTransparent() { 材料研究(400400,CV_8UC4); res.setTo(透明2); WriteableBitmap^wbmp=ref新的WriteableBitmap(res.cols,res.r

假设我们有以下颜色:

const Scalar TRANSPARENT2 = Scalar(255, 0, 255,0);
它是品红色但完全透明:alpha=0(完全不透明为255)

现在,我根据以下内容进行了测试:

WriteableBitmap^Grabcut::TestTransparent()
{
材料研究(400400,CV_8UC4);
res.setTo(透明2);
WriteableBitmap^wbmp=ref新的WriteableBitmap(res.cols,res.rows);
IBuffer^buffer=wbmp->PixelBuffer;
无符号字符*像素;
ComPtr pBufferByteAccess;
ComPtr pBuffer((IInspectable*)缓冲区);
pBuffer.As(&pBufferByteAccess);
pBufferByteAccess->Buffer(&dst像素);
memcpy(像素、分辨率数据、分辨率步长buf[1]*res.cols*res.rows);
返回wbmp;
}
我的问题是创建的图像不是完全透明的,它有一点alpha:

我知道memcpy数据中有一个fila,但我不确定如何解决这个问题。有没有办法把它送到阿尔法0

更多细节

为了查看保存图像后是否可以读取并测试它是否有效,我看到imwrite包含了一个关于透明度的片段,就像在图像中一样,但是imwrite还没有实现。但透明度方法也不起作用

这个片段有什么线索吗


谢谢。

最后我在C代码中进行了转换,首先避免调用CreateAlphaMat。 然后我使用位图编码器转换数据:

WriteableBitmap wb = new WriteableBitmap(bitmap.PixelWidth, bitmap.PixelHeight);
        using (IRandomAccessStream stream = new InMemoryRandomAccessStream())
        {
            BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
            Stream pixelStream = bitmap.PixelBuffer.AsStream();
            byte[] pixels = new byte[pixelStream.Length];
            await pixelStream.ReadAsync(pixels, 0, pixels.Length);

            encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied,
                        (uint)bitmap.PixelWidth, (uint)bitmap.PixelHeight, 96.0, 96.0, pixels);
            await encoder.FlushAsync();
            wb.SetSource(stream);
        }
        this.MainImage.Source = wb;
其中位图是OpenCV结果中的可写位图。现在图像是完全透明的

注意:不要使用MemoryStream和then.AsRandomAccessStream,因为它不会进行FlushAsync

WriteableBitmap wb = new WriteableBitmap(bitmap.PixelWidth, bitmap.PixelHeight);
        using (IRandomAccessStream stream = new InMemoryRandomAccessStream())
        {
            BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
            Stream pixelStream = bitmap.PixelBuffer.AsStream();
            byte[] pixels = new byte[pixelStream.Length];
            await pixelStream.ReadAsync(pixels, 0, pixels.Length);

            encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied,
                        (uint)bitmap.PixelWidth, (uint)bitmap.PixelHeight, 96.0, 96.0, pixels);
            await encoder.FlushAsync();
            wb.SetSource(stream);
        }
        this.MainImage.Source = wb;