基于坐标的立绘合成工具
-
usage:img-cov.exe backgroundfile foregroundfile outputfile x y
img-cov.7z
源代码:#include<bits/stdc++.h> #include <io.h> #include <windows.h> #include <direct.h> #include <opencv2/opencv.hpp> using namespace cv; using namespace std; void overlayImages(const cv::Mat& background, const cv::Mat& foreground, cv::Mat& output, int x, int y) { output = background.clone(); cv::Rect roi(x, y, foreground.cols, foreground.rows); roi &= cv::Rect(0, 0, background.cols, background.rows); cv::Mat roi_output = output(roi); cv::Mat roi_foreground = foreground(cv::Rect(0, 0, roi.width, roi.height)); for (int i = 0; i < roi.height; ++i) { for (int j = 0; j < roi.width; ++j) { cv::Vec4b pixel_foreground = roi_foreground.at<cv::Vec4b>(i, j); cv::Vec4b& pixel_output = roi_output.at<cv::Vec4b>(i, j); double alpha = pixel_foreground[3] / 255.0; double beta = 1.0 - alpha; for (int k = 0; k < 3; ++k) { pixel_output[k] = static_cast<uchar>(alpha * pixel_foreground[k] + beta * pixel_output[k]); } } } } void imgset(string path1, string path2,string outpath,int x,int y){ Mat img1 = cv::imread(path2, cv::IMREAD_UNCHANGED); Mat img2 = cv::imread(path1, cv::IMREAD_UNCHANGED); Mat mergedImg; overlayImages(img1,img2,mergedImg,x,y); imwrite(outpath, mergedImg); return ; } int main(int argc,char *argv[]){ if(argc!=6){ printf("usage:img-cov.exe backgroundfile foregroundfile outputfile x y"); return 0; } else { imgset(argv[2],argv[1],argv[3],atoi(argv[4]),atoi(argv[5])); return 0; } }