如何在 Android 中模糊图像的某些部分?
我在一个项目中工作,我必须清晰地显示图像的某些部分并使图像的其余部分变得模糊.模糊应由滑块管理.意味着它可以增加或减少.最终结果图像应如下所示.
I am working in a project where I have to show some portion of the image clear and make rest part of the image blur. The blur should be managed by slider. Means it can be increase or decrease. The final result image should look alike below.
在我的研究过程中,我发现以下链接很有用
During my research for this I found below links useful
https://github.com/kikoso/android-stackblur
http://blog.neteril.org/blog/2013/08/12/blurring-images-on-android/
但上面链接中的问题是它们都会使图像完全模糊.不是图像的某些部分.
But the issue in above links is they all make complete image blur. Not some part of image.
请提出一些解决方案来实现这一目标.提前致谢.
Kindly suggest some solution to achieve this. Thanks in advance.
做几次蒙版模糊....
do a masked blur few times ....
创建蒙版
0
表示模糊(黑色),>=1
表示不模糊(白色).将此部分初始化为足够大的值,例如 w=100
像素
0
means blur (black) and >=1
means not blur (white). Init this part by big enough value for example w=100
pixels
创建蒙版模糊功能
只是一个带有一些矩阵的普通卷积
just a common convolution with some matrix like
0.0 0.1 0.0
0.1 0.6 0.1
0.0 0.1 0.0
但仅对蒙版为 ==0
的目标像素执行此操作,在图像模糊后,蒙版也会模糊.这应该会稍微扩大白色区域(每次迭代按像素,但在边界上会损失幅度,这就是 w>1
的原因).
but do it only for target pixels where mask is ==0
after image is blurred blur also the mask. This should enlarge the white area a bit (by pixel per iteration but losing magnitude on borders that is why w>1
).
循环子弹 #2 N
次
loop bullet #2 N
times
N
确定模糊/非模糊渐变深度 w
只是为了确保毛刺蒙版会增长......每次模糊蒙版都会增加其白色部分
N
determines blur/non-blur gradient depth the w
is only to assure that burred mask will grow... Each time the blur mask will increase its white part
这应该可以解决问题,您还可以使用遮罩的扩张而不是模糊它.
That should do the trick, You can also use dilatation of the mask instead of blurring it.
[edit1] 实施
今天玩了一会儿,发现蒙版不够平滑,所以我稍微改变了算法(这里是我的代码 C++):
Have played with this a bit today and found out that the mask is not growing enough with smooth so I change the algo a bit (here mine code C++):
picture pic0,pic1,pic2;
// pic0 - source
// pic1 - output
// pic2 - mask
int x0=400,y0=330,r0=100,dr=200;
// x0,y0,r0 - masked area
// dr - blur gradient size
int i,r;
// init output as sourceimage
pic1=pic0;
// init mask (size of source image) with gradient circles
pic2.resize(pic0.xs,pic0.ys);
pic2.clear(0);
for (i=1;i<=255;i++)
{
r=r0+dr-((dr*i)>>8);
pic2.bmp->Canvas->Brush->Color=TColor(i<<16); // shifted because GDI has inverse channel layout then direct pixel access
pic2.bmp->Canvas->Pen ->Color=TColor(i<<16);
pic2.bmp->Canvas->Ellipse(x0-r,y0-r,x0+r,y0+r);
}
for (i=1;i<255;i+=10) pic1.rgb_smooth_masked(pic2,i);
这里是平滑函数:
//---------------------------------------------------------------------------
void picture::rgb_smooth_masked(const picture &mask,DWORD treshold)
{
int i,x,y;
color *q0,*q1,*m0,c0,c1,c2;
if ((xs<2)||(ys<2)) return;
for (y=0;y<ys-1;y++)
{
q0=p[y ]; m0=mask.p[y];
q1=p[y+1];
for (x=0;x<xs-1;x++)
if (m0[x].dd<treshold)
{
c0=q0[x];
c1=q0[x+1];
c2=q1[x];
for (i=0;i<4;i++)
q0[x].db[i]=DWORD((DWORD(c0.db[i])+DWORD(c0.db[i])+DWORD(c1.db[i])+DWORD(c2.db[i]))>>2);
}
}
}
//---------------------------------------------------------------------------
创建渐变蒙版,圆圈的颜色从
1
增加到255
create gradient mask with circles increasing in color from
1
to255
rest 为黑色渐变宽度为 dr
并确定平滑锐度.
rest is black the gradient width is dr
and determine the smoothing sharpness.
使用蒙版和阈值创建平滑蒙版
平滑掩码像素小于等于的所有像素临界点.请参阅函数 rgb_smooth_masked
.它使用2x2
卷积矩阵
smooth all pixels where mask pixel is < threshold. See the function rgb_smooth_masked
. It uses 2x2
convolution matrix
0.50,0.25
0.25,0.00
循环阈值从 1
到 255
一步
loop threshold from 1
to 255
by some step
该步骤决定了图像模糊强度.
the step determines the image blur strength.
最后这里有一些视觉效果,这是我用相机拍摄的源图像:
And finally here some visual results this is source image I taken with my camera:
这里是左边的输出和右边的掩码:
And here the output on the left and mask on the right:
蓝色表示 values (B为最低8位颜色)
the blue color means values < 256
(B is lowest 8 bits of color)
我使用自己的图片类来处理图像,因此一些成员是:
I use my own picture class for images so some members are:
-
xs,ys
图像的像素大小 -
p[y][x].dd
是(x,y)
位置的像素,为 32 位整数类型 -
clear(color)
- 清除整个图像 -
resize(xs,ys)
- 将图像大小调整为新的分辨率
-
xs,ys
size of image in pixels -
p[y][x].dd
is pixel at(x,y)
position as 32 bit integer type -
clear(color)
- clears entire image -
resize(xs,ys)
- resizes image to new resolution