文章目录
-
- 运行结果
- 编译文件
- 源码解读
- 多通道图像混合
- 图像混合和同时显示多张图像
- 图像混合
- 两张图像叠加控件调整透明度
运行结果
编译文件
cmake_minimum_required(VERSION 2.8)
project(imageBasics)
set(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_CXX_FLAGS "-std=c++11 -O2")
find_package(OpenCV REQUIRED)
add_executable(c_basis c_basis.cpp)
target_link_libraries(c_basis ${OpenCV_LIBS})
源码解读
#include
#include
#include
#include
#include
int main(int argc, char **argv) {
cv::Mat image;
image = cv::imread("../ubuntu.png");
if (image.data == nullptr) {
std::cerr << "文件不存在" << std::endl;
return 0;
}
cv::imshow("image", image);
cv::waitKey(0);
std::cout << "image.size: " << image.size << std::endl;
std::cout << "image.rows: " << image.rows << std::endl;
std::cout << "image.cols: " << image.cols << std::endl;
std::cout << "image.rows/2: " << image.rows/2 << std::endl;
std::cout << "image.cols/2: " << image.cols/2 << std::endl;
cv::Mat resize_image;
cv::resize(image, resize_image, cv::Size(0,0),0.5,0.5,CV_INTER_LINEAR);
cv::imshow("resize_image", resize_image);
cv::waitKey(0);
resize_image.copyTo(image(cv::Rect(0, 0, image.cols/2, image.rows/2)));
resize_image.copyTo(image(cv::Rect(0, resize_image.rows, image.cols/2, image.rows/2)));
resize_image.copyTo(image(cv::Rect(resize_image.cols, 0, image.cols/2, image.rows/2)));
resize_image.copyTo(image(cv::Rect(resize_image.cols, resize_image.rows, image.cols/2, image.rows/2)));
cv::imshow("image", image);
cv::waitKey(0);
cv::Mat mask;
cv::resize(resize_image, mask, cv::Size(0,0),0.5,0.5,CV_INTER_LINEAR);
cv::Mat mask_gray;
cv::cvtColor(mask,mask_gray,CV_RGB2GRAY);
mask.copyTo(resize_image(cv::Rect(0,0,mask.cols,mask.rows)),mask_gray);
cv::imshow("resize_image", resize_image);
cv::waitKey(0);
cv::rectangle(mask, cv::Rect(mask.cols-60, mask.rows-60, 60, 60), cv::Scalar(0, 0, 0), -1, 8);
cv::Mat imageROI= resize_image(cv::Rect(0,0,mask.cols,mask.rows));
cv::addWeighted(imageROI,0.5,mask,0.3,0.,imageROI);
imshow("resize_image",resize_image);
cv::waitKey(0);
cv::destroyAllWindows();
return 0;
}
多通道图像混合
#include
#include
using namespace cv;
using namespace std;
bool MultiChannelBlending();
int main(){
MultiChannelBlending();
return 0;
}
bool MultiChannelBlending(){
Mat srcImage;
Mat logoImage;
vector<Mat> channels;
Mat imageBlueChannel;
logoImage = imread("../dota_logo.jpg",0);
imshow("dota_logo",logoImage);
srcImage = imread("../dota_jugg.jpg");
imshow("dota_jugg",srcImage);
split(srcImage,channels);
imageBlueChannel= channels.at(0);
addWeighted(imageBlueChannel(Rect(500,250,logoImage.cols,logoImage.rows)),1.0,
logoImage,0.5,0,imageBlueChannel(Rect(500,250,logoImage.cols,logoImage.rows)));
merge(channels,srcImage);
namedWindow("srcImage");
imshow("srcImage",srcImage);
imwrite("../srcImage.png", srcImage);
cvWaitKey(0);
Mat imageGreenChannel;
logoImage= imread("../dota_logo.jpg",0);
imshow("dota_logo",logoImage);
srcImage= imread("../dota_jugg.jpg");
imshow("dota_jugg",srcImage);
split(srcImage,channels);
imageGreenChannel= channels.at(1);
addWeighted(imageGreenChannel(Rect(500,250,logoImage.cols,logoImage.rows)),1.0,
logoImage,0.5,0.,imageGreenChannel(Rect(500,250,logoImage.cols,logoImage.rows)));
merge(channels,srcImage);
namedWindow("srcImage");
imshow("srcImage",srcImage);
imwrite("../srcImage.png", srcImage);
cvWaitKey(0);
Mat imageRedChannel;
logoImage= imread("../dota_logo.jpg",0);
imshow("dota_logo",logoImage);
srcImage= imread("../dota_jugg.jpg");
imshow("dota_jugg",srcImage);
split(srcImage,channels);
imageRedChannel= channels.at(2);
addWeighted(imageRedChannel(Rect(500,250,logoImage.cols,logoImage.rows)),1.0,
logoImage,0.5,0.,imageRedChannel(Rect(500,250,logoImage.cols,logoImage.rows)));
merge(channels,srcImage);
namedWindow("srcImage");
imshow("srcImage",srcImage);
imwrite("../srcImage.png", srcImage);
waitKey(0);
return true;
}
图像混合和同时显示多张图像
#include
#include
#include
#include
#include
int main(int argc, char **argv) {
cv::Mat image;
image = cv::imread("../ubuntu.png");
if (image.data == nullptr) {
std::cerr << "文件不存在" << std::endl;
return 0;
}
cv::imshow("image", image);
cv::waitKey(0);
std::cout << "image.size: " << image.size << std::endl;
std::cout << "image.rows: " << image.rows << std::endl;
std::cout << "image.cols: " << image.cols << std::endl;
std::cout << "image.rows/2: " << image.rows/2 << std::endl;
std::cout << "image.cols/2: " << image.cols/2 << std::endl;
cv::Mat resize_image;
cv::resize(image, resize_image, cv::Size(0,0),0.5,0.5,CV_INTER_LINEAR);
cv::imshow("resize_image", resize_image);
cv::waitKey(0);
resize_image.copyTo(image(cv::Rect(0, 0, image.cols/2, image.rows/2)));
resize_image.copyTo(image(cv::Rect(0, resize_image.rows, image.cols/2, image.rows/2)));
resize_image.copyTo(image(cv::Rect(resize_image.cols, 0, image.cols/2, image.rows/2)));
resize_image.copyTo(image(cv::Rect(resize_image.cols, resize_image.rows, image.cols/2, image.rows/2)));
cv::imshow("image", image);
cv::waitKey(0);
cv::Mat mask;
cv::resize(resize_image, mask, cv::Size(0,0),0.5,0.5,CV_INTER_LINEAR);
cv::Mat mask_gray;
cv::cvtColor(mask,mask_gray,CV_RGB2GRAY);
mask.copyTo(resize_image(cv::Rect(0,0,mask.cols,mask.rows)),mask_gray);
cv::imshow("resize_image", resize_image);
cv::waitKey(0);
cv::rectangle(mask, cv::Rect(mask.cols-60, mask.rows-60, 60, 60), cv::Scalar(0, 0, 0), -1, 8);
cv::Mat imageROI= resize_image(cv::Rect(0,0,mask.cols,mask.rows));
cv::addWeighted(imageROI,0.5,mask,0.3,0.,imageROI);
imshow("resize_image",resize_image);
cv::waitKey(0);
cv::destroyAllWindows();
return 0;
}
图像混合
#include
#include
using namespace cv;
int main( ){
Mat image= imread("../dota.jpg");
namedWindow("【2】原画图");
imshow("【2】原画图",image);
waitKey(0);
Mat logo= imread("../dota_logo.jpg");
namedWindow("【3】logo图");
imshow("【3】logo图",logo);
waitKey(0);
Mat imageROI_same_size_logo, imageROI_add;
imageROI_same_size_logo= image(Rect(80,35,logo.cols,logo.rows));
addWeighted(imageROI_same_size_logo,0.5,logo,0.3,0.,imageROI_same_size_logo);
namedWindow("【4】原画+logo图");
imshow("【4】原画+logo图",image);
imwrite("../由imwrite生成的图片.jpg",image);
waitKey(0);
return 0;
}
两张图像叠加控件调整透明度
#include
#include
using namespace cv;
#define WINDOW_NAME "【滑动条的创建&线性混合示例】"
const int g_nMaxAlphaValue = 100;
int g_nAlphaValueSlider;
double g_dAlphaValue;
double g_dBetaValue;
Mat g_srcImage1;
Mat g_srcImage2;
Mat g_dstImage;
void on_Trackbar( int, void* )
{
g_dAlphaValue = (double) g_nAlphaValueSlider/g_nMaxAlphaValue ;
g_dBetaValue = ( 1.0 - g_dAlphaValue );
addWeighted( g_srcImage1, g_dAlphaValue, g_srcImage2, g_dBetaValue, 0.0, g_dstImage);
imshow( WINDOW_NAME, g_dstImage );
}
int main( int argc, char** argv )
{
g_srcImage1 = imread("../11.jpg");
g_srcImage2 = imread("../22.jpg");
g_nAlphaValueSlider = 70;
namedWindow(WINDOW_NAME, 1);
char TrackbarName[50];
sprintf( TrackbarName, "透明值 %d", g_nMaxAlphaValue );
createTrackbar( TrackbarName, WINDOW_NAME, &g_nAlphaValueSlider, g_nMaxAlphaValue, on_Trackbar );
on_Trackbar( g_nAlphaValueSlider, 0 );
waitKey(0);
return 0;
}