2

我目前正在尝试将输入 .png 文件转换为 OpenCV:Mat,在空白区域保留其透明度。在eclipse上工作,使用openCV4Android。

我试过这个(使用Drawable):

输入:

  • icon.png 是正确加载为 RGBA(4 通道)的资源文件。
  • Mat mcSprite(全局字段)。

    onCameraFrame method{
    try {
        mcSprite = Utils.loadResource(this, R.drawable.icon);
        System.out.println(mcSprite.empty());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Imgproc.resize(mcSprite, mZoomWindow, mZoomWindow.size());
    }
    

结果图像:

黑区

PS:如果我使用以下方式导入:

mcSprite = Utils.loadResource(this, R.drawable.icon , -1 );

>0 Return a 3-channel color image.
=0 Return a grayscale image.
<0 Return the loaded image as is (with alpha channel).

显示图像。

4

2 回答 2

2

更快的解决方案,使用面具:

    public Mat overlayImage(Mat background, Mat foreground)//, Point location)
    {
          Mat mask = new Mat();
          Imgproc.resize(mCurrentMask, mask, background.size());

          Mat source = new Mat();             
          Imgproc.resize(foreground, source, background.size()); 

          source.copyTo(background,mask);
          source.release();
          mask.release();
          return background;
    }

    public void createMask (Mat sprite){
        mCurrentMask = new Mat(sprite.height(),sprite.width(),24);
        double f[] = {1,1,1,0};
        double e[] = {0,0,0,0};
        for(int y = 0; y < (int)(sprite.rows()) ; ++y)
        {
              for(int x = 0; x < (int)(sprite.cols()) ; ++x)
              {     
                  double info[] = sprite.get(y, x);                 
                  if(info[3]>0) //rude but this is what I need
                  {
                      mCurrentMask.put(y, x, f);
                  } 
                  else mCurrentMask.put(y, x, e);
              }
        } 
    }   
于 2013-10-17T13:33:24.500 回答
1

解决了:

    public void overlayImage(Mat background, Mat foreground,Mat output)//, Point location)
    {
      background.copyTo(output);
      Mat dst = new Mat();
      Imgproc.resize(foreground, dst, background.size());
      double alpha;
      // start at row 0/col 0
      for(int y = 0; y < background.rows() ; ++y)
      {
          for(int x = 0; x < background.cols() ; ++x)
          {
              double info[] = dst.get(y, x);
              alpha = info[3];
              // and now combine the background and foreground pixel, using the opacity,but only if opacity > 0. 
              if(alpha>0) //rude but this is what I need
              {
                  double infof[] = dst.get(y, x);
                  output.put(y, x, infof);
              }
          }
      } 

最后结果:

在此处输入图像描述

于 2013-10-17T07:37:44.073 回答