如何从图像中取出平面,对其进行修改并重新插入?

时间:2019-06-07 07:04:53

标签: c++ opencv homography

我正在尝试取出地面并在其上制作网格以进行路径映射,然后将其插入到图像中。在这里,我正在使用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;
}

1 个答案:

答案 0 :(得分:1)

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;
}