gpt4 book ai didi

c++ - 如何从图像中取出一个平面,对其进行修改并将其插入回去?

转载 作者:行者123 更新时间:2023-11-28 04:16:28 25 4
gpt4 key购买 nike

我正在尝试取出地面并在其上制作网格以进行路径映射并将其插入回图像中。在这里,我使用 findhomography 和 warpPerspective 函数来执行此操作。但是当我切换点以插入修改后的平面时,图像中除平面外的所有内容都变成黑色。

我尝试使用中间图像来完成,但结果是一样的。


enter image description here


#include "pch.h"

#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

struct userdata {
Mat im;
vector<Point2f> points;
};

void mouseHandler(int event, int x, int y, int flags, void* data_ptr)
{
if (event == EVENT_LBUTTONDOWN) {
userdata* data = ((userdata*)data_ptr);
circle(data - > im, Point(x, y), 3, Scalar(0, 0, 255), 5, LINE_AA);
imshow("Image", data - > im);
if (data - > points.size() < 4) {
data - > points.push_back(Point2f(x, y));
}
}
}

int main(int argc, char** argv)
{

// Read source image.
Mat im_src = imread("imagesindoor.jpg");

// Destination image. The aspect ratio of the book is 3/4
Size size(400, 300);
Size size2(im_src.cols, im_src.rows);
Mat im_dst = Mat::zeros(size, CV_8UC3);

// Create a vector of destination points.
vector<Point2f> pts_dst;

pts_dst.push_back(Point2f(0, 0));
pts_dst.push_back(Point2f(size.width - 1, 0));
pts_dst.push_back(Point2f(size.width - 1, size.height - 1));
pts_dst.push_back(Point2f(0, size.height - 1));

// Set data for mouse event
Mat im_temp = im_src.clone();
userdata data;
data.im = im_temp;

cout << "Click on the four corners of the book -- top left first and" <<
endl
<< "bottom left last -- and then hit ENTER" << endl;

// Show image and wait for 4 clicks.
imshow("Image", im_temp);
// Set the callback function for any mouse event
setMouseCallback("Image", mouseHandler, &data);
waitKey(0);

// Calculate the homography
Mat h = getPerspectiveTransform(data.points, pts_dst);

// Warp source image to destination
warpPerspective(im_src, im_dst, h, size);

// changing clor of im_dst
for (int i = 0; i < im_dst.rows; i++) {
for (int j = 0; j < im_dst.cols; j++) {
//apply condition here

im_dst.at<cv::Vec3b>(i, j) = 255;
}
}
Mat p = getPerspectiveTransform(pts_dst, data.points);
warpPerspective(im_dst, im_src, p, size2);

// Show image
//imshow("Image", im_dst);
imshow("Image2", im_src);

waitKey(0);

return 0;
}

最佳答案

addWeighted可用于将当前结果与源图像混合以获得预期结果。

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <opencv2/opencv.hpp>
#include <iostream>
#include <stdio.h>

using namespace cv;
using namespace std;

struct userdata {
Mat im;
vector<Point2f> points;
};

void mouseHandler(int event, int x, int y, int flags, void* data_ptr)
{
if (event == EVENT_LBUTTONDOWN) {
userdata* data = ((userdata*)data_ptr);
circle(data-> im, Point(x, y), 3, Scalar(0, 0, 255), 5, LINE_AA);
imshow("Image", data->im);
if (data-> points.size() < 4) {
data-> points.push_back(Point2f(x, y));
}
}
}

int main(int argc, char** argv)
{

// Read source image.
Mat im_src = imread("test.png");

// Destination image. The aspect ratio of the book is 3/4
Size size(400, 300);
Size size2(im_src.cols, im_src.rows);
Mat im_dst = Mat::zeros(size, CV_8UC3);

// Create a vector of destination points.
vector<Point2f> pts_dst;

pts_dst.push_back(Point2f(0, 0));
pts_dst.push_back(Point2f(size.width - 1, 0));
pts_dst.push_back(Point2f(size.width - 1, size.height - 1));
pts_dst.push_back(Point2f(0, size.height - 1));

// Set data for mouse event
Mat im_temp = im_src.clone();
userdata data;
data.im = im_temp;

cout << "Click on the four corners of the book -- top left first and" <<
endl
<< "bottom left last -- and then hit ENTER" << endl;

// Show image and wait for 4 clicks.
imshow("Image", im_temp);
// Set the callback function for any mouse event
setMouseCallback("Image", mouseHandler, &data);
waitKey(0);

// Calculate the homography
Mat h = getPerspectiveTransform(data.points, pts_dst);

// Warp source image to destination
warpPerspective(im_src, im_dst, h, size);

// changing clor of im_dst
for (int i = 0; i < im_dst.rows; i++) {
for (int j = 0; j < im_dst.cols; j++) {
//apply condition here

im_dst.at<cv::Vec3b>(i, j) = 255;
}
}
Mat t;
Mat p = getPerspectiveTransform(pts_dst, data.points);

warpPerspective(im_dst, t, p, size2);

// Show image
//imshow("Image", im_dst);
std::cout << "t :" <<t.cols << ", " <<t.rows <<std::endl;

Mat final;
addWeighted(im_src, 0.5, t, 0.5, 0, final);
imshow("Image2", final);



waitKey(0);

return 0;
}

关于c++ - 如何从图像中取出一个平面,对其进行修改并将其插入回去?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56489476/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com