当前位置:网站首页>Image mosaic camera mosaic notes

Image mosaic camera mosaic notes

2022-06-10 22:47:00 AI vision netqi

1、 be based on opencv3.4.1 Video mosaic algorithm developed , Integrated feature extraction 、 Two channel video automatic splicing algorithm ;

2、 Need to use vs2015, The graphics card runtime has been copied to the execution file , It can run directly , If further optimization is needed , You need to continue to improve yourself ;

3、 Fully open source , Due to the large project , So upload to the network disk , If you need it, you can download it ;

4、 Multithreading is used in the algorithm , If the master makes further improvement , Welcome to share .

SkyDrive address : link :https://pan.baidu.com/s/1TfB6ZWPFaWfn18MiXRyuvg password :ltct.
Link to the original text :https://blog.csdn.net/zhulong1984/article/details/80748202

Homography transformation is compared with translation transformation , It has wider scene adaptability , But at the same time, the stability will decline to a certain extent .

The technical details of the design are :

Feature detection and description
Feature matching and homography matrix estimation
opencv Video capture
Fade in fade out image fusion
The hardware requirements for this solution include : There are two USB Interface computer , Two reasonably placed USB camera .

Reasonable placement means : The two cameras are separated by a certain angle , The camera centers are close to each other , There is enough overlap in the scene . The above ensures the availability of homography transformation .

Code implementation :

#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
# include "opencv2/features2d/features2d.hpp"
#include"opencv2/nonfree/nonfree.hpp"
#include"opencv2/calib3d/calib3d.hpp"
#include<iostream>

using namespace cv;
using namespace std;

int main()
{
    VideoCapture cap1(0);
    VideoCapture cap2(1);

    double rate = 60;
    int delay = 1000 / rate;
    bool stop(false);
    Mat img1;
    Mat img2;
    Mat result;
    int d = 200;// Fade in fade out fusion width 
    Mat homography;
    int k = 0;

    namedWindow("cam1", CV_WINDOW_AUTOSIZE);
    namedWindow("cam2", CV_WINDOW_AUTOSIZE);
    namedWindow("stitch", CV_WINDOW_AUTOSIZE);

    if (cap1.isOpened() && cap2.isOpened())
    {
        cout << "*** ***" << endl;
        cout << " Camera activated !" << endl;
    }
    else
    {
        cout << "*** ***" << endl;
        cout << " Warning : Please check whether the camera is installed properly !" << endl;
        cout << " Program end !" << endl << "*** ***" << endl;
        return -1;
    }

    cap1.set(CV_CAP_PROP_FOCUS, 0);
    cap2.set(CV_CAP_PROP_FOCUS, 0);

    while (!stop)
    {
        if (cap1.read(img1) && cap2.read(img2))
        {
            imshow("cam1", img1);
            imshow("cam2", img2);

            // Color frame to grayscale 
            //cvtColor(img1, img1, CV_RGB2GRAY);
            //cvtColor(img2, img2, CV_RGB2GRAY);

            // Calculate the homography matrix 
            if (k < 1 || waitKey(delay) == 13)
            {
                cout << " Matching ..." << endl;
                
                vector<KeyPoint> keypoints1, keypoints2;
                // Construction detector 
                //Ptr<FeatureDetector> detector = new ORB(120);
                Ptr<FeatureDetector> detector = new SIFT(80);
                detector->detect(img1, keypoints1);
                detector->detect(img2, keypoints2);
                // Construct descriptor extractor 
                Ptr<DescriptorExtractor> descriptor = detector;
                // Extract descriptors 
                Mat descriptors1, descriptors2;

                descriptor->compute(img1, keypoints1, descriptors1);
                descriptor->compute(img2, keypoints2, descriptors2);
                // Construct matcher 
                BFMatcher matcher(NORM_L2, true);
                // Match descriptor 
                vector<DMatch> matches;
                matcher.match(descriptors1, descriptors2, matches);

                vector<Point2f> selPoints1, selPoints2;
                vector<int> pointIndexes1, pointIndexes2;
                for (vector<DMatch>::const_iterator it = matches.begin(); it != matches.end(); ++it)
                {
                    selPoints1.push_back(keypoints1.at(it->queryIdx).pt);
                    selPoints2.push_back(keypoints2.at(it->trainIdx).pt);
                }

                vector<uchar> inliers(selPoints1.size(), 0);
                homography = findHomography(selPoints1, selPoints2, inliers, CV_FM_RANSAC, 1.0);

                // according to RANSAC Re filter matches 
                vector<DMatch> outMatches;
                vector<uchar>::const_iterator itIn = inliers.begin();
                vector<DMatch>::const_iterator itM = matches.begin();
                for (; itIn != inliers.end(); ++itIn, ++itM)
                {
                    if (*itIn)
                    {
                        outMatches.push_back(*itM);
                    }

                }
                k++;

                // Draw the matching result 
                //Mat matchImage;
                //drawMatches(img1, keypoints1, img2, keypoints2, outMatches, matchImage, 255, 255);
                //imshow("match", matchImage);
                ///
                
            }
            // Splicing 
            double t = getTickCount();
            warpPerspective(img1, result, homography, Size(2 * img1.cols-d, img1.rows));//Size Set the width of the resulting image , Cut a part of the width ,d Adjustable 

            Mat half(result, Rect(0, 0, img2.cols - d, img2.rows));
            img2(Range::all(), Range(0, img2.cols - d)).copyTo(half);
            for (int i = 0; i < d; i++)
            {
                result.col(img2.cols - d + i) = (d - i) / (float)d*img2.col(img2.cols - d + i) + i / (float)d*result.col(img2.cols - d + i);
            }

            imshow("stitch", result);
            t = ((double)getTickCount() - t) / getTickFrequency();
            //cout << t << endl;

        }
        else
        {
            cout << "----------------------" << endl;
            cout << "waitting..." << endl;
        }

        if (waitKey(1) == 27)
        {
            stop = true;
            cout << " Program end !" << endl;
            cout << "*** ***" << endl;
        }
    }
    return 0;
}



Experimental results :

The above video was recorded with screen recording software , The resolution will decrease . In practice , Direct observation shows good . Both input source images are 640*480 The resolution of the , It can be realized in real time . In my own i3 The processor is configured to run on a laptop , The display interval of the mosaic image is 0.10″~0.12″.
————————————————
Copyright notice : This paper is about CSDN Blogger 「czl389」 The original article of , follow CC 4.0 BY-SA Copyright agreement , For reprint, please attach the original source link and this statement .
Link to the original text :https://blog.csdn.net/czl389/article/details/60757000

原网站

版权声明
本文为[AI vision netqi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206102133035760.html