图像去畸变

cmake_minimum_required(VERSION 3.10)
project(undistort)
 
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
 
# 生成 compile_commands.json(添加这行)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
 
# 明确设置调试配置
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Debug)
endif()
 
# 强制禁用所有优化
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0 -fno-optimize-sibling-calls")

find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
 

add_executable(main undistort_image.cpp)
 
# 使用传统链接方式
target_link_libraries(main 
    ${OpenCV_LIBS}

)
 
 

#include <opencv2/opencv.hpp>
#include <string>
#include <iostream>
#include <fstream>
#include <unistd.h>

using namespace std;
using namespace cv;

int main(int argc, char **argv) {
    // 调试信息
    char cwd[1024];
    getcwd(cwd, sizeof(cwd));
    cout << "Current working directory: " << cwd << endl;

    // 尝试不同的路径
    string image_file = "./test.png";  
    // string image_file = "../../Data/test.png";  // 或者相对路径
    
    cout << "Looking for image at: " << image_file << endl;

    // 检查文件是否存在
    ifstream file_test(image_file);
    if (!file_test.good()) {
        cerr << "Error: File does not exist: " << image_file << endl;
        cerr << "Please ensure the image file is in the correct location." << endl;
        return -1;
    }
    file_test.close();

    // 畸变参数
    double k1 = -0.28340811, k2 = 0.07395907, p1 = 0.00019359, p2 = 1.76187114e-05;
    double fx = 458.654, fy = 457.296, cx = 367.215, cy = 248.375;

    Mat image = imread(image_file, 0);
    
    if (image.empty()) {
        cerr << "Error: Could not load image! File may be corrupted or wrong format." << endl;
        return -1;
    }
    
    cout << "Image loaded successfully. Size: " << image.cols << "x" << image.rows << endl;

    // ... 其余代码保持不变
    int rows = image.rows, cols = image.cols;
    Mat image_undistort = Mat(rows, cols, CV_8UC1);

    for (int v = 0; v < rows; v++) {
        for (int u = 0; u < cols; u++) {
            double u_distorted = 0, v_distorted = 0;
            double x = (u - cx) / fx;
            double y = (v - cy) / fy;
            double r = sqrt(x * x + y * y);
            double x_distorted = x * (1 + k1 * r * r + k2 * r * r * r * r) + 2 * p1 * x * y + p2 * (r * r + 2 * x * x);
            double y_distorted = y * (1 + k1 * r * r + k2 * r * r * r * r) + p1 * (r * r + 2 * y * y) + 2 * p2 * x * y;
            u_distorted = fx * x_distorted + cx;
            v_distorted = fy * y_distorted + cy;
            
            if (u_distorted >= 0 && v_distorted >= 0 && u_distorted < cols && v_distorted < rows) {
                image_undistort.at<uchar>(v, u) = image.at<uchar>((int)v_distorted, (int)u_distorted);
            } else {
                image_undistort.at<uchar>(v, u) = 0;
            }
        }
    }

    imwrite("./undistorted_image.png", image_undistort);
    cout << "Undistorted image saved as 'undistorted_image.png'" << endl;

    return 0;
}

Logo

火山引擎开发者社区是火山引擎打造的AI技术生态平台,聚焦Agent与大模型开发,提供豆包系列模型(图像/视频/视觉)、智能分析与会话工具,并配套评测集、动手实验室及行业案例库。社区通过技术沙龙、挑战赛等活动促进开发者成长,新用户可领50万Tokens权益,助力构建智能应用。

更多推荐