您的代码太初始了。你的图像也不适合完美的结果。猜猜你在图像上执行了真正的代码,你会得到谁穿衬衫的图像。
Mat4b src= imread("path");// to load 4 channel image : imread("path",IMREAD_UNCHANGED);
Mat3b dest = imread("path");
Rect roi = Rect(179,539,src.cols,src.rows); //src.cols = 1186 and src.rows= 1134 after scaling.
Mat destinationROI = dest(roi);
src.copyTo(destinationROI);
imwrite("destinationROI.png", destinationROI);
我可以编写代码来获取以下图像(它只是为了显示结果而进行了 photoshop 处理),但它对您的图像毫无意义。
编辑 1:
我在这里稍微编辑了您的代码。看看 //edit 1,2,3 里面的代码。
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <stdio.h>
using namespace cv;
using namespace std;
float sx, sy;
int tx, ty;
struct TaggingPoint{
int rshx, rshy;
int lshx, lshy;
int topx, topy;
int botx, boty;
}body_points, garment_points;
int main(int argc, char** argv)
{
Mat shirt;
Mat body;
shirt = imread(argv[1], IMREAD_COLOR);
if (shirt.empty())
return -1;
body = imread(argv[2], IMREAD_COLOR);
if (body.empty())
return -1;
body_points.rshx = 418;
body_points.rshy = 706;
body_points.lshx = 1234;
body_points.lshy = 706;
body_points.topx = 838;
body_points.topy = 510;
body_points.botx = 838;
body_points.boty = 1534;
garment_points.rshx = 239;
garment_points.rshy = 147;
garment_points.lshx = 755;
garment_points.lshy = 147;
garment_points.topx = 505;
garment_points.topy = 50;
garment_points.botx = 505;
garment_points.boty = 953;
// edit 1 : when you calculate by this values your shirt image's width will be greater that body image's width
// so to reduce it
sx = (float)(body_points.lshx - body_points.rshx) / (garment_points.lshx - garment_points.rshx)*0.995;
sy = (float)(body_points.boty - body_points.topy) / (garment_points.boty - garment_points.topy);
//scale the image
resize(shirt, shirt, Size(), sx, sy, INTER_LINEAR);
imwrite("shirt.png", shirt);
//translation happened
// tx = body_points.rshx - (sx * garment_points.rshx);
tx = body_points.rshx - (garment_points.rshx);
ty = body_points.rshy - (sy * garment_points.rshy);
//draw one image over another image
//src.copyTo(dst.rowRange(1, 6).colRange(3, 10));
// shirt.copyTo(body.rowRange(tx, shirt.rows).colRange(ty, shirt.cols));
// shirt.copyTo(body.rowRange(100, 1000).colRange(100, 500));
// cvtColor(shirt, shirt, CV_BGRA2BGR);
// cvtColor(body, body, CV_BGRA2BGR);
namedWindow("body");
//Rect roi(cv::Point(tx, ty), Size(shirt.size()));
// Edit 2 : Rect.x = 0
Rect roi = Rect(0,ty,shirt.cols,shirt.rows);
Mat destinationROI = body(roi);
// cvtColor(destinationROI, destinationROI, CV_BGRA2BGR);
// Edit 3 : Create a mask ( it is show purpose only, need improvement )
Mat mask;
cvtColor( shirt, mask, COLOR_BGR2GRAY );
mask = mask < 250;
shirt.copyTo(destinationROI,mask);
imwrite("destinationROI.png", destinationROI);
imshow("body", body);
imwrite("body.png", body);
waitKey();
return 0;
}