如何用C ++拼接图像? [英] How to stitch images in C++?

查看:126
本文介绍了如何用C ++拼接图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了很多代码甚至很多网站,但我只得到一个使用opencv的stitcher功能的代码。该功能没有问题,但主要问题是我只能将一个图像加载到拼接器中。因此,由于代码退出异常。请帮忙!



我尝试过:



下面的代码是几乎所有网站中给出的代码,但它无法正常工作。

线条Stitcher雕像会产生一个错误,即只加载了一个图像。所以,由于代码不起作用。请帮我正确的代码。



我使用的代码如下:

I have tried many codes and even many websites, but i get only one code which uses stitcher function of opencv. There is no problem in that function, but the major problem is i can only load one image into the stitcher. So, due to that the code exits with exception. Please help!

What I have tried:

The code below is the code given in almost all websites but it does'nt work correctly.
The line Stitcher statues generates an error that there is only one image loaded. So, due to that the code doesnt work. Please help me with the correct code.

The code which i have used is as follows:

#include <iostream>
#include <fstream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/stitching/stitcher.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"

using namespace std;
using namespace cv;

bool try_use_gpu = false;
bool divide_images = false;
// Define mode for stitching as panoroma 
// (One out of many functions of Stitcher)
//Stitcher:: mode = Stitcher::composePanorama;
string result_name = "result.jpg";
// Array for pictures
vector<mat> imgs;

	// methods

int main(int argc, char* argv[])
{
	// Get all the images that need to be 
	// stitched as arguments from command line 
	for (int i = 1; i < 3; ++i)
	{
		// Read the ith argument or image 
		// and push into the image array
		Mat img = imread("C:\\Users\\leno\\Documents\\640x480dove\\OCR_REF\\FONT-0.bmp",IMREAD_GRAYSCALE);
		Mat img1 = imread("C:\\Users\\leno\\Documents\\640x480dove\\OCR_REF\\FONT-1.bmp", IMREAD_GRAYSCALE);
		Mat img2 = imread("C:\\Users\\leno\\Documents\\640x480dove\\OCR_REF\\FONT-2.bmp", IMREAD_GRAYSCALE);
		if (img.empty())
		{
			// Exit if image is not present
			cout << "Can't read image '" << img << "'\n";
			return -1;
		}
		imgs.push_back(img);

		if (img1.empty())
		{
			// Exit if image is not present
			cout << "Can't read image '" << img << "'\n";
			return -1;
		}
		imgs.push_back(img1);

		if (img2.empty())
		{
			// Exit if image is not present
			cout << "Can't read image '" << img << "'\n";
			return -1;
		}
		imgs.push_back(img2);
	}

	// Define object to store the stitched image
	Mat pano;

	// Create a Stitcher class object with mode panoroma
	//Ptr<stitcher> stitcher = Stitcher::create( try_use_gpu);
	
	Stitcher stitcher = Stitcher::createDefault(false);
	// Command to stitch all the images present in the image array
	Stitcher::Status status = stitcher.stitch(imgs, pano);

	if (status != Stitcher::OK)
	{
		// Check if images could not be stiched
		// status is OK if images are stiched successfully
		cout << "Can't stitch images\n";
		return -1;
	}

	// Store a new image stiched from the given 
	//set of images as "result.jpg"
	imwrite("result.jpg", pano);

	// Show the result
	imshow("Result", pano);

	waitKey(0);
	return 0;
}

推荐答案

您必须知道为什么您的代码无效。也许文件不存在,或者你的循环没有意义。为什么要加载3个文件3次?



你应该学会使用 debugger 代码。
You must know WHY your code isnt working. Maybe the files arent there, or your loop is non sense. Why are you loading the 3 files 3 times?

You should learn to use the debugger for your code.


这篇关于如何用C ++拼接图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆