我想提供小方 block 的中心坐标,即 (5,5)[小方 block 是 10x10] 放置在 20x20 大方 block 的 (8,8) 坐标上。 得到这个图像结果:
最佳答案
试试这个,虽然我没有测试它,所以如果它不起作用请评论,我会尝试修复它 =)
cv::Mat placeImageInImage(cv::Point center, cv::Mat src, cv::Mat dst_orig)
{
cv::Mat dst = dst_orig.clone();
cv::Rect targetRect = cv::Rect(center.x-src.cols/2, center.y-src.rows/2, src.cols, src.rows);
cv::Rect srcRect = cv::Rect(0,0,src.cols, src.rows);
cv::Rect dstRect = cv::Rect(0,0,dst.cols, dst.rows);
cv::Rect roi = targetRect & dstRect;
srcRect.width = roi.width;
srcRect.height = roi.height;
// didnt test, maybe swap the order:
srcRect.x += roi.x - targetRect.x;
srcRect.y += roi.y - targetRect.y;
src(srcRect).copyTo(dst(roi));
return dst;
}
使用此代码进行测试:
int main(int argc, char* argv[])
{
cv::Mat input = cv::imread("C:/StackOverflow/Input/Lenna.png");
cv::Mat input2;
cv::resize(input, input2, cv::Size(256, 256));
cv::Mat result = placeImageInImage(cv::Point(256, 256), input2, input);
cv::imshow("input", input);
cv::imshow("result", result);
cv::waitKey(0);
return 0;
}
我得到:
关于opencv - C++ opencv图像编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38873357/