前言
這是我《OpenCV:從零到一》專欄的第五篇博客,想看跟多請戳這。
本文概要
multiply
addWeighted
Rect以及常用的矩形類
copyTo的另一種用法
案例代碼
大概內容:圖片線性混合、乘積混合、疊加。
#include <opencv2/opencv.hpp>
#include <iostream>
using std::cout;
using std::endl;
using namespace cv;
int main(int argc, char** argv) {
Mat linux, windows, dst1, dst2;
linux = imread("D:\\86186\\Documents\\opencv\\linux.jpg");
windows = imread("D:\\86186\\Documents\\opencv\\windows.jpg");
if (!linux.data) {
cout << "could not load image Linux Logo..." << endl;
return -1;
}
if (!windows.data) {
cout << "could not load image WIN7 Logo..." << endl;
return -1;
}
if (linux.rows == windows.rows && linux.cols == windows.cols && linux.type() == windows.type()) {
//混合圖像(無論是乘還是加)的大小和類型都要一樣
double alpha = 0.5;//設置混合權重
addWeighted(linux, alpha, windows, (1.0 - alpha), 0.0, dst1);
imshow("addWeightedDst", dst1);
imshow("linux", linux);
imshow("windows", windows);
multiply(linux, windows, dst2, 1.0);//Calculates the per-element scaled product of two arrays.
imshow("multiplyDst", dst2);
Mat smallLinux = imread("D:\\86186\\Documents\\opencv\\smallLinux.jpg");
Mat imageROI = windows(Rect(20, 20, smallLinux.cols, smallLinux.rows));//ROI region of image
Mat mask= imread("D:\\86186\\Documents\\opencv\\smallLinux.jpg", IMREAD_GRAYSCALE);//IMREAD_GRAYSCALE==0,mask必須是灰度圖像
smallLinux.copyTo(imageROI, mask);//copyTo的另一種用法是用來疊加圖片
imshow("smallLinux", windows);
}
else {
printf("could not blend images , the size of images is not same...\n");
return -1;
}
waitKey(0);
return 0;
}
運行效果:
解析及注意事項
- 混合的圖像要大小一樣,疊加的圖像要比原圖像小,並且位置要不超出原圖像
- 線性混合公式:
- ROI區域創建好之後可以利用copyTo將和ROI一樣大小的圖片疊加到ROI所在的區域上面
全註釋代碼
#include <opencv2/opencv.hpp>
#include <iostream>
using std::cout;
using std::endl;
using namespace cv;
int main(int argc, char** argv) {
Mat linux, windows, dst1, dst2;
linux = imread("D:\\86186\\Documents\\opencv\\linux.jpg");
windows = imread("D:\\86186\\Documents\\opencv\\windows.jpg");
if (!linux.data) {
cout << "could not load image Linux Logo..." << endl;
return -1;
}
if (!windows.data) {
cout << "could not load image WIN7 Logo..." << endl;
return -1;
}
if (linux.rows == windows.rows && linux.cols == windows.cols && linux.type() == windows.type()) {
//混合圖像的大小和類型都要一樣(無論是乘還是加)
double alpha = 0.5;//設置混合權重
addWeighted(linux, alpha, windows, (1.0 - alpha), 0.0, dst1);
/*
InputArray src1,//first input array.
double alpha,//weight of the first array elements.
InputArray src2,//second input array of the same size and channel number as src1.大小和通道要和圖一相同
double beta,//weight of the second array elements.
double gamma,//scalar added to each sum.
OutputArray dst,//scalar added to each sum.
int dtype = -1 //optional depth of the output array;
*/
imshow("addWeightedDst", dst1);
imshow("linux", linux);
imshow("windows", windows);
multiply(linux, windows, dst2, 1.0);//Calculates the per-element scaled product of two arrays.
/*
計算兩個數組的每個元素縮放後的乘積。
參數:
InputArray src1,
InputArray src2,//second input array of the same size and the same type as src1
OutputArray dst,
double scale = 1,
int dtype = -1
dst(I)=saturate(scale * src1(I) * src2(I))
*/
imshow("multiplyDst", dst2);
Mat smallLinux = imread("D:\\86186\\Documents\\opencv\\smallLinux.jpg");
Mat imageROI = windows(Rect(20, 20, smallLinux.cols, smallLinux.rows));//ROI region of image
Mat mask = imread("D:\\86186\\Documents\\opencv\\smallLinux.jpg", IMREAD_GRAYSCALE);//IMREAD_GRAYSCALE==0,mask必須是灰度圖像
smallLinux.copyTo(imageROI, mask);//copyTo的另一種用法是用來疊加圖片
imshow("smallLinux", windows);
/*
rect是一個矩形類,參數有四個(當然還有其他重載),建議前兩個參數設置小一些,並且區域要小於原圖,否則會越界
_Tp x,//x coordinate of the top-left corner 矩形左上角的x座標
_Tp y,//y coordinate of the top-left corner 矩形左上角的y座標
_Tp height,//height of the rectangle 矩形高度
_Tp width,//width of the rectangle 矩形寬度
宏定義關係如下
typedef Rect_<int> cv::Rect2i
typedef Rect_<float> cv::Rect2f
typedef Rect_<double> cv::Rect2d
typedef Rect2i cv::Rect
*/
}
else {
printf("could not blend images , the size of images is not same...\n");
return -1;
}
waitKey(0);
return 0;
}
翻譯筆記
ROI region of interest 感興趣區域
coordinate v. 調節,配合;使動作協調;(衣服、傢俱等)搭配;與……形成共價鍵
adj. 同等的,並列的;配位的;座標的
n. 座標;配套服裝;同等的人或物