欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用OpneCV去畸变

程序员文章站 2024-03-25 10:14:28
...
// 不多哔哔,直接上代码片
/**
 *      @time: 2020.10,20
 * 
 */

#include <iostream>

// OpenCV
// 其实用不到这么多
#include <opencv2/core/core.hpp>
#include <opencv2/imgcodecs/imgcodecs.hpp>

// 文件输入输出流
#include <fstream>
#include <sstream>

// boost format格式控制
#include <boost/format.hpp>

// setw() setfill
// 格式化输出
#include <iomanip> 

#include <string.h>


using namespace std;
using namespace cv;

string dataset_path = "/home/wang/Ginger2020/Dataset/Bumblebee/01/image_0";

void ReadImg(int &index, 
             string &DatasetPath,
             cv::Mat &dataset);


int main ( int argc, char** argv )
{
    int img_file_index = 0;
    int filecount = 0;
    
    while (1)
    {
        cv::Mat dataset;

        ReadImg(img_file_index, dataset_path, dataset);

        if(dataset.data == 0) { cout<<"图像读取完毕"<<endl; break; }

        cout << " img_file_index " << img_file_index << endl;
        img_file_index++;

        // 去畸变
        cv::Mat K_img(3, 3, CV_64F);
        cv::Mat coeff(5, 1, CV_64F);

        K_img.at<double>(0, 0) = 1644.7156613466043;   // fx
        K_img.at<double>(0, 1) = 0;
        K_img.at<double>(0, 2) = 640.0;                // cx

        K_img.at<double>(1, 0) = 0;
        K_img.at<double>(1, 1) = 1644.7156613466043;   // fy
        K_img.at<double>(1, 2) = 480.0;                // cy\

        K_img.at<double>(2, 0) = 0;
        K_img.at<double>(2, 1) = 0;
        K_img.at<double>(2, 2) = 1;

        coeff.at<double>(0,0) = -0.54184704404293005; // k1 
        coeff.at<double>(1,0) = -1.1038602217192159;  // k2
        coeff.at<double>(2,0) = 0.0;                  // p1
        coeff.at<double>(3,0) = 0.0;                  // p2
        coeff.at<double>(4,0) = 7.8578495299742874;   // p3

        cv::Mat newimage;
        cv::Mat new_matrix;  // ???

        cv::undistort(dataset, newimage, K_img, coeff, new_matrix);

        // 保存去畸变后的图像
        string imgfilename;
        std::stringstream StrStm;     
    
        //从右往左  剩下的用0填满   一共填充5位   0 — ....      
        StrStm << setfill('0') << setw(5) << filecount;
        
        filecount++;
        
        StrStm >> imgfilename;

        imgfilename += ".jpg";

        imwrite(imgfilename, newimage);

        StrStm.clear(); // 循环,前边写入的会在流里,clear一下
        imgfilename.clear();

        // 先拿10幅图像试试
        if(index==10) { break; } 
    }
    
    return 0;

}


void ReadImg(int &index, 
             string &DatasetPath,
             cv::Mat &dataset)
{

        // format格式控制 %s路径: "/home/ginger/NCUTDataset/917/3/3"
        boost::format fmt("%s/my_video-3_%03d.jpg"); // my_video-3_000.jpg 000是变化的用%03d代替, 03是说从三个零开始:000 
        

        cv::Mat Lab;

        Lab = cv::imread((fmt % DatasetPath % index).str(), cv::IMREAD_GRAYSCALE);

        // 判定图像是否为空
        if ( Lab.data == 0 )
        {
            cout << " 图像读取完毕! " << endl;
            break;
        }

        dataset = Lab;
}
相关标签: Camera opencv