常用的像素操作算法:图像加法、像素混合、提取图像中的ROI

简介: 常用的像素操作算法:图像加法、像素混合、提取图像中的ROI

图像可以是看成是一个多维的数组。读取一张图片,可以看成是读入了一系列的像素内容。这些像素内容,按照不同的模式具有不同的格式。对于三通道的 RGB 位图来说,每个像素是一个 8-bit 整数的三元组。图像的像素操作是比较基础的图像算法,下面列举三个常用的像素操作算法。


图像加法



图像的加法表示两个输入图像在同一位置上的像素相加,得到一个输出图像的过程。

imageProcessor = Operator.add(imageProcessor1,imageProcessor2);
        if (imageProcessor!=null) {
            CV4JImage resultCV4JImage = new CV4JImage(imageProcessor.getWidth(), imageProcessor.getHeight(), imageProcessor.getPixels());
            result.setImageBitmap(resultCV4JImage.getProcessor().getImage().toBitmap());
        }


image.png

图像加法.png


Operator的add表示矩阵加法,有一个要求两个图像必须大小一致。

public static ImageProcessor add(ImageProcessor image1, ImageProcessor image2) {
        if(!checkParams(image1, image2)) {
            return null;
        }
        int channels = image1.getChannels();
        int w = image1.getWidth();
        int h = image1.getHeight();
        ImageProcessor dst = (channels == 3) ? new ColorProcessor(w, h) : new ByteProcessor(w, h);
        int size = w*h;
        int a=0, b=0;
        int c=0;
        for(int i=0; i<size; i++) {
            for(int n=0; n<channels; n++) {
                a = image1.toByte(n)[i]&0xff;
                b = image2.toByte(n)[i]&0xff;
                c = Tools.clamp(a + b);
                dst.toByte(n)[i] = (byte)c;
            }
        }
        return dst;
    }


在实际工作中,可以通过一张原图和一个mask图像来相加合成一些不规则的效果图片。


像素混合



在这里混合是线性混合,跟之前的图像加法有一定的区别。

imageProcessor = Operator.addWeight(imageProcessor1,2.0f,imageProcessor2,1.0f,4);
        if (imageProcessor!=null) {
            CV4JImage resultCV4JImage = new CV4JImage(imageProcessor.getWidth(), imageProcessor.getHeight(), imageProcessor.getPixels());
            result.setImageBitmap(resultCV4JImage.getProcessor().getImage().toBitmap());
        }

image.png

像素混合.png


Operator的addWeight方法表示像素混合。


image.png

addWeight.png

public static ImageProcessor addWeight(ImageProcessor image1, float w1, ImageProcessor image2, float w2, int gamma) {
        if(!checkParams(image1, image2)) {
            return null;
        }
        int channels = image1.getChannels();
        int w = image1.getWidth();
        int h = image1.getHeight();
        ImageProcessor dst = (channels == 3) ? new ColorProcessor(w, h) : new ByteProcessor(w, h);
        int size = w*h;
        int a=0, b=0;
        int c=0;
        for(int i=0; i<size; i++) {
            for(int n=0; n<channels; n++) {
                a = image1.toByte(n)[i]&0xff;
                b = image2.toByte(n)[i]&0xff;
                c = (int)(a*w1 + b*w2 + gamma);
                dst.toByte(n)[i] = (byte)Tools.clamp(c);
            }
        }
        return dst;
    }


提取图像中的ROI



ROI(region of interest),表示图像中感兴趣的区域。对于一张图像,可能我们只对图像中某部分感兴趣,或者要对目标进行跟踪时,需要选取目标特征,所以要提取图像的感兴趣区域。

Resources res = getResources();
        final Bitmap bitmap = BitmapFactory.decodeResource(res, R.drawable.pixel_test_3);
        image.setImageBitmap(bitmap);
        CV4JImage cv4jImage = new CV4JImage(bitmap);
        ImageProcessor imageProcessor = cv4jImage.getProcessor();
        Rect rect = new Rect();
        rect.x = 300;
        rect.y = 200;
        rect.width = 300;
        rect.height = 450;
        ImageProcessor resultImageProcessor = null;
        try {
            resultImageProcessor = Operator.subImage(imageProcessor,rect);
        } catch (CV4JException e) {
        }
        if (resultImageProcessor!=null) {
            CV4JImage resultCV4JImage = new CV4JImage(resultImageProcessor.getWidth(), resultImageProcessor.getHeight(), resultImageProcessor.getPixels());
            result.setImageBitmap(resultCV4JImage.getProcessor().getImage().toBitmap());
        }


image.png

提取图像中的ROI.png


其中,rect.x和rect.y表示ROI的起始点,rect.width和rect.height表示ROI的宽和高。Operator的subImage()表示从原图中提取ROI,之所以在这里还用到了try catch,是为了防止出现ROI的宽度或者高度过大,从而导致数组越界。


subImage方法的代码也很简单

/**
     * ROI sub image by rect.x, rect.y, rect.width, rect.height
     * @param image
     * @param rect
     * @return
     * @throws CV4JException
     */
    public static ImageProcessor subImage(ImageProcessor image, Rect rect) throws CV4JException{
        int channels = image.getChannels();
        int w = rect.width;
        int h = rect.height;
        ImageProcessor dst = (channels == 3) ? new ColorProcessor(w, h) : new ByteProcessor(w, h);
        int a=0;
        int index = 0;
        try {
            for(int n=0; n<channels; n++) {
                for(int row=rect.y; row < (rect.y+rect.height); row++) {
                    for(int col=rect.x; col < (rect.x+rect.width); col++) {
                        index = row*image.getWidth() + col;
                        a = image.toByte(n)[index]&0xff;
                        index = (row - rect.y)*w + (col - rect.x);
                        dst.toByte(n)[index] = (byte)a;
                    }
                }
            }
        } catch (ArrayIndexOutOfBoundsException e) {
            throw new CV4JException("数组越界了");
        }
        return dst;
    }


总结



cv4jgloomyfish和我一起开发的图像处理库,纯java实现,目前还处于早期的版本。

像素操作是 cv4j 的基本功能之一,所有的像素操作算法都在Operator类中。除了本文介绍的三个算法之外,还有substract表示矩阵减法、multiple表示矩阵逐元素乘法、division表示矩阵逐元素除法以及bitwise_and、bitwise_not、bitwise_or、bitwise_xor表示每个元素进行位运算分别是和、非、或、异或。

相关文章
|
1月前
|
机器学习/深度学习 算法 数据库
KNN和SVM实现对LFW人像图像数据集的分类应用
KNN和SVM实现对LFW人像图像数据集的分类应用
34 0
|
4月前
|
算法 数据挖掘 计算机视觉
Python利用K-Means算法进行图像聚类分割实战(超详细 附源码)
Python利用K-Means算法进行图像聚类分割实战(超详细 附源码)
142 0
|
4月前
|
算法 计算机视觉 异构计算
基于FPGA的图像形态学腐蚀算法实现,包括tb测试文件和MATLAB辅助验证
基于FPGA的图像形态学腐蚀算法实现,包括tb测试文件和MATLAB辅助验证
|
10天前
|
文字识别 算法 计算机视觉
图像倾斜校正算法的MATLAB实现:图像倾斜角检测及校正
图像倾斜校正算法的MATLAB实现:图像倾斜角检测及校正
15 0
|
4月前
|
机器学习/深度学习 文字识别 算法
[Halcon&图像] 缺陷检测的一些思路、常规检测算法
[Halcon&图像] 缺陷检测的一些思路、常规检测算法
324 1
|
1月前
|
机器学习/深度学习 算法 计算机视觉
利用深度学习算法实现图像风格转换技术探究
本文将通过深入分析深度学习算法在图像处理领域的应用,探讨如何利用神经网络实现图像风格转换技术。通过研究不同风格迁移算法的原理和实现方式,揭示其在艺术创作、图像编辑等领域的潜在应用和挑战。
|
1月前
|
编解码 算法 计算机视觉
基于FPGA的图像最近邻插值算法verilog实现,包括tb测试文件和MATLAB辅助验证
基于FPGA的图像最近邻插值算法verilog实现,包括tb测试文件和MATLAB辅助验证
|
2月前
|
机器学习/深度学习 算法 搜索推荐
【实操】数据扩增:Retinex算法用于图像颜色恢复和对比度增强
【实操】数据扩增:Retinex算法用于图像颜色恢复和对比度增强
33 0
【实操】数据扩增:Retinex算法用于图像颜色恢复和对比度增强
|
2月前
|
算法 测试技术 C++
【数论】【分类讨论】【C++算法】1611使整数变为 0 的最少操作次数
【数论】【分类讨论】【C++算法】1611使整数变为 0 的最少操作次数
|
2月前
|
算法 计算机视觉
基于Harris角点的多视角图像全景拼接算法matlab仿真
基于Harris角点的多视角图像全景拼接算法matlab仿真