最近准备开始着手学习计算机视觉,首先还是从opencv开始学起。
在opencv平台首先实现摄像头实时采集图像,按下空格拍照,拍完四张图片后实现图像的简单拼接。
1.图像的采集
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/stitching.hpp>
using namespace std;
using namespace cv;
//程序说明:1.按下ESC程序退出,2.按下空格键截取图像
int main()
{
Mat srcImage;
char c;
int temp=0;//计数器
VideoCapture cam(0);
if (!cam.isOpened()) exit(0);
namedWindow("camera");
while (true) {
cam >> srcImage;
imshow("camera", srcImage);
c = waitKey(30);
if (c == ' ')
{
imwrite(FileLocation("e:\hello\ ",temp,".jpg"),srcImage);
temp++;
}
if (c == 27)
break;
}
return 0;
}
2.图像的拼接
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/stitching.hpp>
using namespace std;
using namespace cv;
bool try_use_gpu = false;
vector<Mat> imgs;
string result_name = "dst1.jpg";
int main(int argc, char * argv[])
{
Mat img1 = imread("3.jpg");
Mat img2 = imread("2.jpg");
Mat img3 = imread("4.jpg");
Mat img4 = imread("1.jpg");
if (img1.empty() || img2.empty())
{
cout << "Can't read image" << endl;
return -1;
}
imgs.push_back(img1);
imgs.push_back(img2);
imgs.push_back(img3);
imgs.push_back(img4);
Stitcher stitcher = Stitcher::createDefault(try_use_gpu);
// 使用stitch函数进行拼接
Mat pano;
Stitcher::Status status = stitcher.stitch(imgs, pano);
if (status != Stitcher::OK)
{
cout << "Can't stitch images, error code = " << int(status) << endl;
return -1;
}
imwrite(result_name, pano);
Mat pano2 = pano.clone();
// 显示源图像,和结果图像
imshow("FULL", pano);
if (waitKey() == 27)
return 0;
}