Java OpenCV Android:Import.png保留空白区域的透明度
我目前正在尝试将一个input.png文件转换为OpenCV:Mat,在空白区域保留其透明度。使用openCV4Android在eclipse上工作 我已经尝试过这个(使用可拖动的): 输入:Java OpenCV Android:Import.png保留空白区域的透明度,java,android,opencv,png,Java,Android,Opencv,Png,我目前正在尝试将一个input.png文件转换为OpenCV:Mat,在空白区域保留其透明度。使用openCV4Android在eclipse上工作 我已经尝试过这个(使用可拖动的): 输入: icon.png是正确加载为RGBA(4个通道)的资源文件 Mat mcSprite(全局字段) 生成的图像: PS:如果我使用导入: mcSprite=Utils.loadResource(这个,R.drawable.icon,-1) >0返回3通道彩色图像。 =0返回灰度图像。 已解决:
- icon.png是正确加载为RGBA(4个通道)的资源文件
- Mat mcSprite(全局字段)
>0返回3通道彩色图像。
=0返回灰度图像。
已解决:
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);
}
}
}
public void overlayImage(垫背景、垫前景、垫输出)/,点位置)
{
background.copyTo(输出);
Mat dst=新Mat();
resize(前台、dst、后台.size());
双α;
//从第0行/第0列开始
对于(int y=0;y0。
如果(alpha>0)//这是我需要的
{
double infof[]=dst.get(y,x);
输出put(y,x,infof);
}
}
}
最终结果:
使用遮罩,解决方案速度更快:
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);
}
}
}
public Mat overlayImage(Mat background,Mat foreground)/,点位置)
{
Mat mask=新Mat();
resize(mCurrentMask,mask,background.size());
材料来源=新材料();
resize(前景、源、背景.size());
source.copyTo(背景、掩码);
source.release();
面具。释放();
返回背景;
}
公共虚空createMask(Mat sprite){
mCurrentMask=new Mat(sprite.height(),sprite.width(),24);
双f[]={1,1,1,0};
双e[]={0,0,0,0};
对于(int y=0;y<(int)(sprite.rows());+y)
{
对于(int x=0;x<(int)(sprite.cols());++x)
{
双信息[]=sprite.get(y,x);
if(info[3]>0)//粗鲁,但这正是我需要的
{
mCurrentMask.put(y,x,f);
}
else mCurrentMask.put(y,x,e);
}
}
}
+1此解决方案对于Java来说要好得多。但是,我认为它会崩溃,因为您的掩码不是CV_8U
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);
}
}
}
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);
}
}
}