Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何从图像中取出一个平面,对其进行修改并将其插入?_C++_Opencv_Homography - Fatal编程技术网

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

C++ 如何从图像中取出一个平面,对其进行修改并将其插入?,c++,opencv,homography,C++,Opencv,Homography,我试图取出地面,在其上制作栅格,用于路径映射,并将其插入到图像中。在这里,我使用findhomography和warpPerspective函数来实现这一点。但当我切换点以插入修改后的平面时,图像中除平面外的所有内容都变为黑色 我试着用中间图像来做,但结果是一样的 #包括“pch.h” #包括 使用名称空间cv; 使用名称空间std; 结构用户数据{ Mat im; 矢量点; }; void鼠标句柄(int事件、int x、int y、int标志、void*数据\u ptr) { if(e

我试图取出地面,在其上制作栅格,用于路径映射,并将其插入到图像中。在这里,我使用findhomography和warpPerspective函数来实现这一点。但当我切换点以插入修改后的平面时,图像中除平面外的所有内容都变为黑色

我试着用中间图像来做,但结果是一样的



#包括“pch.h”
#包括
使用名称空间cv;
使用名称空间std;
结构用户数据{
Mat im;
矢量点;
};
void鼠标句柄(int事件、int x、int y、int标志、void*数据\u ptr)
{
if(event==event_LBUTTONDOWN){
用户数据*数据=((用户数据*)数据_ptr);
圆(数据->im,点(x,y),3,标量(0,0,255),5,线(AA);
imshow(“图像”,数据->即时消息);
如果(数据->点.size()<4){
数据->点。向后推(点2f(x,y));
}
}
}
int main(int argc,字符**argv)
{
//读取源图像。
Mat im_src=imread(“imagesindoor.jpg”);
//目标图像。书的纵横比为3/4
尺寸(400300);
尺寸2(im_src.cols,im_src.rows);
Mat im_dst=Mat::零(尺寸,CV_8UC3);
//创建目标点的向量。
向量ptsdst;
pts_dst.推回(点2f(0,0));
pts_dst.推回(点2F(尺寸宽度-1,0));
pts_dst.推回(点2F(尺寸.宽度-1,尺寸.高度-1));
pts_dst.推回(点2F(0,尺寸高度-1));
//为鼠标事件设置数据
Mat im_temp=im_src.clone();
用户数据;
data.im=im_温度;
cout可用于将当前结果与源图像混合以获得预期结果

#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;
}
#包括“opencv2/highgui/highgui.hpp”
#包括“opencv2/imgproc/imgproc.hpp”
#包括
#包括
#包括
使用名称空间cv;
使用名称空间std;
结构用户数据{
Mat im;
矢量点;
};
void鼠标句柄(int事件、int x、int y、int标志、void*数据\u ptr)
{
if(event==event_LBUTTONDOWN){
用户数据*数据=((用户数据*)数据_ptr);
圆(数据->im,点(x,y),3,标量(0,0,255),5,线(AA);
imshow(“图像”,数据->即时消息);
如果(数据->点.size()<4){
数据->点。向后推(点2f(x,y));
}
}
}
int main(int argc,字符**argv)
{
//读取源图像。
Mat im_src=imread(“test.png”);
//目标图像。书的纵横比为3/4
尺寸(400300);
尺寸2(im_src.cols,im_src.rows);
Mat im_dst=Mat::零(尺寸,CV_8UC3);
//创建目标点的向量。
向量ptsdst;
pts_dst.推回(点2f(0,0));
pts_dst.推回(点2F(尺寸宽度-1,0));
pts_dst.推回(点2F(尺寸.宽度-1,尺寸.高度-1));
pts_dst.推回(点2F(0,尺寸高度-1));
//为鼠标事件设置数据
Mat im_temp=im_src.clone();
用户数据;
data.im=im_温度;

请不要使用名称空间
,尤其是如果您使用多个名称空间,而使用
std:
cv::
。如果我理解正确,您可以使用鼠标收集平面的4个点,然后将该平面转换为矩形图像。在这里,您将丢失所有关于背景的信息。您需要做的是请告诉我如何组合这两幅图像?你能发布源图像和当前结果吗?背景值为o(黑色背景),前景(平面)设置为255(白色前景)。如果您有一个8位值的图像,您可以将其添加。如果要添加网格,只需将选定像素设置为255,将其余像素设置为0。
#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;
}