图像处理------透明混合 - Alpha Blending效果

简介: <p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">基本原理:</p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">图像的透明混

基本原理:

图像的透明混合有个专属名词– Alpha Blending

 

对任意两张图像可以合成为一张图像,合成图像的像素取值根据数学公式:

RGB3 = (1- a) * RGB1 + a * RGB2

其中a为混合透明度取值范围[0, 1]之间, RGB3为目标像素值, RGB1与RGB2的值分别来自两

张不同的图像。


两张源图像分别为:


第二张源图像是房屋设计图



三:最终程序效果如下



四:程序关键代码及解释

获取BufferedImage对象中像素数据的代码如下:

[java]  view plain copy
  1. public void getRGB(BufferedImage img, int x, int y, int width, int height, int[] pixelsData) {  
  2.         int type = img.getType();  
  3.         if (type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB) {  
  4.             img.getRaster().getDataElements(x, y, width, width, pixelsData);  
  5.         } else {  
  6.             img.getRGB(x, y, width, height, pixelsData, 0, img.getWidth());  
  7.         }  
  8.     }  

将处理后的像素数组写到新创建的BufferedImage对象中的代码如下:

[java]  view plain copy
  1. public void setRGB(BufferedImage img, int x, int y, int width, int height, int[] pixelsData) {  
  2.         int type = img.getType();  
  3.         if (type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB) {  
  4.             img.getRaster().setDataElements(x, y, width, height, pixelsData);  
  5.         } else {  
  6.             img.setRGB(x, y, width, height, pixelsData, 0, width);  
  7.         }  
  8.     }  

创建一个新BufferedImage对象代码如下:

[java]  view plain copy
  1. alphaBlendingImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);  

实现Alpha Blending的代码如下:

[java]  view plain copy
  1. float blendingRate = 0.5f;  
  2.     private void processPixels(int[] inPixelsOne, int[] inPixelsTwo, int[] outPixelsData, int width, int height) {  
  3.         int index = 0;  
  4.         for(int row=0; row<height; row++) {  
  5.             for(int col=0; col<width; col++) {  
  6.                 int ta = 0, tr = 0, tg = 0, tb = 0;  
  7.                 int rgb1 = inPixelsOne[index];  
  8.                 int rgb2 = inPixelsTwo[index];  
  9.                 ta = ((rgb1 >> 24) & 0xff) + ((rgb2 >> 24) & 0xff);  
  10.                 tr = ((rgb1 >> 16) & 0xff) + ((rgb2 >> 16) & 0xff);  
  11.                 tg = ((rgb1 >> 8) & 0xff) + ((rgb2 >> 8) & 0xff);  
  12.                 tb = (rgb1 & 0xff) + (rgb2 & 0xff);  
  13.                   
  14.                 int a = 0, r=0, g=0, b=0;  
  15.                 a = (int)(blendingRate *(float)ta);  
  16.                 r = (int)(blendingRate *(float)tr);  
  17.                 g = (int)(blendingRate *(float)tg);  
  18.                 b = (int)(blendingRate *(float)tb);  
  19.                   
  20.                 outPixelsData[index] = ((a << 24) & 0xFF000000)  
  21.                 | ((r << 16) & 0x00FF0000)  
  22.                 | ((g << 8) & 0x0000FF00)  
  23.                 | ((b) & 0x000000FF);  
  24.                 index++;  
  25.             }  
  26.         }  
  27.           
  28.     }  

本例中,为了简化计算假设alpah blending系数为0.5

加载/读取图像文件的代码如下:

[java]  view plain copy
  1. if (srcImageOne == null) {  
  2.     File file_001 = new File("D:\\resource\\350_001.png");  
  3.     srcImageOne = ImageIO.read(file_001);  
  4. }  
  5. if (srcImageTwo == null) {  
  6.     File file_002 = new File("D:\\resource\\350_002.png");  
  7.     srcImageTwo = ImageIO.read(file_002);  
  8. }  
相关文章
|
算法 数据可视化
Halcon边缘检测和线条检测(3),文章含BLOB检测常用方法和shape_trans内接和外接算子的说明
Halcon边缘检测和线条检测(3),文章含BLOB检测常用方法和shape_trans内接和外接算子的说明
1523 0
Halcon边缘检测和线条检测(3),文章含BLOB检测常用方法和shape_trans内接和外接算子的说明
|
4月前
|
数据采集 人工智能 计算机视觉
CLIP的升级版Alpha-CLIP:区域感知创新与精细控制
为了增强CLIP在图像理解和编辑方面的能力,上海交通大学、复旦大学、香港中文大学、上海人工智能实验室、澳门大学以及MThreads Inc.等知名机构共同合作推出了Alpha-CLIP。这一创新性的突破旨在克服CLIP的局限性,通过赋予其识别特定区域(由点、笔画或掩码定义)的能力。Alpha-CLIP不仅保留了CLIP的视觉识别能力,而且实现了对图像内容强调的精确控制,使其在各种下游任务中表现出色。
85 1
|
计算机视觉
目标检测的Tricks | 【Trick8】数据增强——随机旋转、平移、缩放、错切、hsv增强
目标检测的Tricks | 【Trick8】数据增强——随机旋转、平移、缩放、错切、hsv增强
551 0
目标检测的Tricks | 【Trick8】数据增强——随机旋转、平移、缩放、错切、hsv增强
数据增强 | 旋转、平移、缩放、错切、HSV增强
数据增强 | 旋转、平移、缩放、错切、HSV增强
221 0
数据增强 | 旋转、平移、缩放、错切、HSV增强
|
传感器 数据采集
Google Earth Engine ——ANDSAT/LM05/C01/T1-T2经过缩放、校准的传感器辐射度数据集
Google Earth Engine ——ANDSAT/LM05/C01/T1-T2经过缩放、校准的传感器辐射度数据集
122 0
Google Earth Engine ——ANDSAT/LM05/C01/T1-T2经过缩放、校准的传感器辐射度数据集
Halcon颜色通道分离与合并RGB--HSI--HSV;饱和度/亮度均衡
Halcon颜色通道分离与合并RGB--HSI--HSV;饱和度/亮度均衡
792 0
|
计算机视觉 Python
【python图像处理】给图像添加透明度(alpha通道)
【python图像处理】给图像添加透明度(alpha通道) 我们常见的RGB图像通常只有R、G、B三个通道,在图像处理的过程中会遇到往往需要向图像中添加透明度信息,如公司logo的设计,其输出图像文件就需要添加透明度,即需要在RGB三个通道的基础上添加alpha通道信息。
6488 0
|
算法 C# 计算机视觉
Win8 Metro(C#)数字图像处理--2.46图像RGB分量增强效果
原文:Win8 Metro(C#)数字图像处理--2.46图像RGB分量增强效果  [函数名称] RGB分量调整         RGBAdjustProcess(WriteableBitmap src, i...
903 0