使用OpenCV实现两张图像融合在一起
opencv
OpenCV: 开源计算机视觉库
项目地址:https://gitcode.com/gh_mirrors/opencv31/opencv
免费下载资源
·
简单介绍
图像融合技术是一种结合多个不同来源或不同传感器捕获的同一场景的图像数据,以生成一幅更全面、更高质量的单一图像的过程。这种技术广泛应用于遥感、医学影像分析、计算机视觉等多个领域。常见的图像融合技术包括基于像素级、特征级和决策级的融合方法,以及基于多尺度分解如图像金字塔的方法。
OpenCV + Python实现
OpenCV 中实现图像融合的一个常见方法是使用 addWeighted()
函数。这个函数可以用来对两张图像按照指定的权重进行线性组合,从而达到融合的效果。以下是一个基本的示例代码片段:
import cv2
# 读取两张图像
img1 = cv2.imread('image1.jpg')
img2 = cv2.imread('image2.jpg')
# 确保两张图像具有相同的尺寸,如果不相同,可以先调整到同一尺寸
if img1.shape != img2.shape:
# 调整图像大小
img1_resized = cv2.resize(img1, img2.shape[:2][::-1], interpolation=cv2.INTER_LINEAR)
img2_resized = img2
else:
img1_resized = img1
img2_resized = img2
# 定义权重
alpha = 0.7 # 第一张图像的权重
beta = 0.3 # 第二张图像的权重
gamma = 0 # 常数值(可选,通常设为0)
# 使用 addWeighted() 进行图像融合
blended_img = cv2.addWeighted(img1_resized, alpha, img2_resized, beta, gamma)
# 显示或保存融合后的图像
cv2.imshow('Blended Image', blended_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 或者直接保存
cv2.imwrite('blended_image.jpg', blended_img)
在这个例子中,addWeighted()
函数接收五个参数:
src1
(这里是img1_resized
):第一个输入图像矩阵。alpha
:第一个图像的权重,取值范围从0到1之间。src2
(这里是img2_resized
):第二个输入图像矩阵。beta
:第二个图像的权重。gamma
:常数,添加到权重和之后。
通过调整 alpha
和 beta
参数,你可以控制两张图像在融合结果中的相对强度或透明度。如果希望得到的是简单的加权平均效果,那么通常会将 gamma
设为0。如果需要做亮度调节或者其它非线性混合,可以根据实际情况调整这些参数。
OpenCV + C++实现
在C++中使用OpenCV实现图像融合的方式与Python类似,主要区别在于语法和API调用方式。以下是使用C++实现图像融合的示例代码:
#include <opencv2/opencv.hpp>
int main() {
// 读取两张图像
cv::Mat img1 = cv::imread("image1.jpg");
cv::Mat img2 = cv::imread("image2.jpg");
// 检查图像是否成功读取
if (img1.empty() || img2.empty()) {
std::cout << "Error: Could not open or find the images!" << std::endl;
return -1;
}
// 确保两张图像具有相同的尺寸,如果不相同,可以先调整到同一尺寸
cv::Mat img1_resized, img2_resized;
if (img1.size() != img2.size()) {
cv::resize(img1, img1_resized, img2.size(), 0, 0, cv::INTER_LINEAR);
img2_resized = img2.clone();
} else {
img1_resized = img1.clone();
img2_resized = img2.clone();
}
// 定义权重
double alpha = 0.7; // 第一张图像的权重
double beta = 0.3; // 第二张图像的权重
double gamma = 0; // 常数值(可选,通常设为0)
// 使用 addWeighted() 进行图像融合
cv::Mat blended_img;
cv::addWeighted(img1_resized, alpha, img2_resized, beta, gamma, blended_img);
// 显示或保存融合后的图像
cv::imshow("Blended Image", blended_img);
cv::waitKey(0);
// 或者直接保存
cv::imwrite("blended_image.jpg", blended_img);
return 0;
}
请注意,在C++版本中,你需要包含必要的头文件,并且在显示图像后使用waitKey(0)
来暂停程序执行,等待用户按键,然后关闭所有打开的窗口。同时,使用clone()
函数复制图像以避免原始图像被修改。
效果展示
GitHub 加速计划 / opencv31 / opencv
142
15
下载
OpenCV: 开源计算机视觉库
最近提交(Master分支:3 个月前 )
d9a139f9
Animated WebP Support #25608
related issues #24855 #22569
### Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
1 天前
09030615
V4l default image size #25500
Added ability to set default image width and height for V4L capture. This is required for cameras that does not support 640x480 resolution because otherwise V4L capture cannot be opened and failed with "Pixel format of incoming image is unsupported by OpenCV" and then with "can't open camera by index" message. Because of the videoio architecture it is not possible to insert actions between CvCaptureCAM_V4L::CvCaptureCAM_V4L and CvCaptureCAM_V4L::open so the only way I found is to use environment variables to preselect the resolution.
Related bug report is [#25499](https://github.com/opencv/opencv/issues/25499)
Maybe (but not confirmed) this is also related to [#24551](https://github.com/opencv/opencv/issues/24551)
This fix was made and verified in my local environment: capture board AVMATRIX VC42, Ubuntu 20, NVidia Jetson Orin.
### Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
- [X] I agree to contribute to the project under Apache 2 License.
- [X] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [X] The PR is proposed to the proper branch
- [X] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
1 天前
更多推荐
已为社区贡献6条内容
所有评论(0)