OpenCV CUDA模块图像处理------图像融合函数blendLinear()

  • 操作系统:ubuntu22.04
  • OpenCV版本:OpenCV4.9
  • IDE:Visual Studio Code
  • 编程语言:C++11

算法描述

该函数执行 线性融合(加权平均) 两个图像 img1 和 img2,使用对应的权重图 weights1 和 weights2。

融合公式如下:
r e s u l t ( x , y ) = i m g 1 ( x , y ) ⋅ w e i g h t s 1 ( x , y ) + i m g 2 ( x , y ) ⋅ w e i g h t s 2 ( x , y ) result(x,y)=img1(x,y)⋅weights1(x,y)+img2(x,y)⋅weights2(x,y) result(x,y)=img1(x,y)⋅weights1(x,y)+img2(x,y)⋅weights2(x,y)

每个像素点根据各自的权重进行线性组合。

函数原型

cpp 复制代码
void cv::cuda::blendLinear 	
(
 	InputArray  	img1,
	InputArray  	img2,
	InputArray  	weights1,
	InputArray  	weights2,
	OutputArray  	result,
	Stream &  	stream = Stream::Null() 
) 		

参数

参数名 说明
img1 输入图像 1 第一幅输入图像
img2 输入图像 2 第二幅输入图像,必须与 img1 尺寸和类型相同
weights1 权重图 1 与 img1 对应的权重图,单通道浮点型(CV_32F)
weights2 权重图 2 与 img2 对应的权重图,单通道浮点型(CV_32F)
result 输出图像 输出结果,与输入图像大小和类型一致
stream 流对象 用于异步执行的 CUDA 流。默认为 Stream::Null(),即同步执行

代码示例

cpp 复制代码
#include <opencv2/opencv.hpp>
#include <opencv2/cudaimgproc.hpp>

int main() {
    // Step 1: 加载两幅图像
    cv::Mat img1 = cv::imread("/media/dingxin/data/study/OpenCV/sources/images/stich1.png");
    cv::Mat img2 = cv::imread("/media/dingxin/data/study/OpenCV/sources/images/stich2.png");

    if (img1.empty() || img2.empty()) {
        std::cerr << "Failed to load images!" << std::endl;
        return -1;
    }

    // Step 2: 确保图像尺寸一致
    cv::resize(img2, img2, img1.size());

    // Step 3: 创建权重图(例如:渐变效果)
    cv::Mat weight1(img1.size(), CV_32F, 0.5f);  // 全图权重 0.5
    cv::Mat weight2 = 1.0f - weight1;             // 反向权重图

    // Step 4: 上传数据到 GPU
    cv::cuda::GpuMat d_img1, d_img2, d_weight1, d_weight2, d_result;
    d_img1.upload(img1);
    d_img2.upload(img2);
    d_weight1.upload(weight1);
    d_weight2.upload(weight2);
    d_result.create(img1.size(), img1.type());

    // Step 5: 执行线性融合
    cv::cuda::blendLinear(d_img1, d_img2, d_weight1, d_weight2, d_result);

    // Step 6: 下载并显示结果
    cv::Mat result;
    d_result.download(result);

    cv::imshow("Original Image 1", img1);
    cv::imshow("Original Image 2", img2);
    cv::imshow("Blended Image", result);
    cv::waitKey(0);

    return 0;
}

运行结果

相关推荐
爱笑的眼睛114 小时前
PyTorch Lightning:重新定义深度学习工程实践
java·人工智能·python·ai
做cv的小昊4 小时前
VLM经典论文阅读:【综述】An Introduction to Vision-Language Modeling
论文阅读·人工智能·计算机视觉·语言模型·自然语言处理·bert·transformer
开放知识图谱4 小时前
论文浅尝 | 利用条件语句激发和提升大语言模型的因果推理能力(CL2025)
人工智能·语言模型·自然语言处理
KG_LLM图谱增强大模型4 小时前
[经典之作]大语言模型与知识图谱的融合:通往智能未来的路线图
人工智能·大模型·知识图谱·graphrag·本体论·图谱增强大模型
YJlio4 小时前
「C++ 40 周年」:从“野蛮生长的指针地狱”到 AI 时代的系统底座
c++·人工智能·oracle
机器之心4 小时前
小米开源首个跨域具身基座模型MiMo-Embodied,29个榜单SOTA
人工智能·openai
六行神算API-天璇5 小时前
架构实战:打造基于大模型的“混合搜索”系统,兼顾关键词与语义
人工智能·架构
龙卷风04055 小时前
深入理解Spring AI Alibaba多Agent系统:图结构驱动的智能协作
人工智能·后端
mqiqe5 小时前
【Spring AI MCP】四、MCP 服务端
java·人工智能·spring
好奇龙猫5 小时前
【AI学习-lora-定义-comfyUI相关-相关学习-了解概念(1)】
人工智能·学习