You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VSCode界面及输出半中英混合,如何切换为全英文?

解决VSCode中OpenCV输出半中英混合的问题

问题说明

VSCode界面及终端输出呈现半中英混合状态,查阅英文错误提示更高效,此前尝试调整VSCode终端语言的方案无效。使用OpenCV相机校准代码时,中文错误提示造成困扰。

可行解决方案

1. 终端临时设置环境变量

运行程序前,在VSCode终端中设置环境变量强制英文输出:

  • Windows CMD
    set LC_ALL=C
    set LANG=C
    
  • Windows PowerShell
    $env:LC_ALL="C"
    $env:LANG="C"
    
  • Linux/macOS
    export LC_ALL=C
    export LANG=C
    

2. 配置VSCode终端默认环境变量

打开VSCode设置(快捷键Ctrl+,),搜索对应系统的终端环境变量配置(如terminal.integrated.env.windows),添加以下配置:

"terminal.integrated.env.windows": {
    "LC_ALL": "C",
    "LANG": "C"
}

这样每次打开终端都会自动应用该设置,无需手动输入。

3. 在代码中强制指定语言

在代码开头添加本地化设置,强制OpenCV输出英文:

#include <iostream>
#include <locale.h> // 添加头文件
#include <opencv2/calib3d.hpp>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>

int main(int argc, char **argv) {
    setlocale(LC_ALL, "C"); // 添加这行
    (void)argc;
    (void)argv;

    // 其余代码保持不变...
}

也可以使用OpenCV自带的本地化函数:

cv::setLocale(cv::LOCALE_ENGLISH);

附用户提供的相机校准代码

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

int main(int argc, char **argv) {

  (void)argc;
  (void)argv;

  std::vector<cv::String> fileNames;
  cv::glob("../calibration/Image*.png", fileNames, false);
  cv::Size patternSize(25 - 1, 18 - 1);
  std::vector<std::vector<cv::Point2f>> q(fileNames.size());

  std::vector<std::vector<cv::Point3f>> Q;
  // 1. Generate checkerboard (world) coordinates Q. The board has 25 x 18
  // fields with a size of 15x15mm

  int checkerBoard[2] = {25,18};
  // Defining the world coordinates for 3D points
    std::vector<cv::Point3f> objp;
    for(int i = 1; i<checkerBoard[1]; i++){
      for(int j = 1; j<checkerBoard[0]; j++){
        objp.push_back(cv::Point3f(j,i,0));
      }
    }

  std::vector<cv::Point2f> imgPoint;
  // Detect feature points
  std::size_t i = 0;
  for (auto const &f : fileNames) {
    std::cout << std::string(f) << std::endl;

    // 2. Read in the image an call cv::findChessboardCorners()
    cv::Mat img = cv::imread(fileNames[i]);
    cv::Mat gray;

    cv::cvtColor(img, gray, cv::COLOR_RGB2GRAY);

    bool patternFound = cv::findChessboardCorners(gray, patternSize, q[i], cv::CALIB_CB_ADAPTIVE_THRESH + cv::CALIB_CB_NORMALIZE_IMAGE + cv::CALIB_CB_FAST_CHECK);

    // 2. Use cv::cornerSubPix() to refine the found corner detections
    if(patternFound){
        cv::cornerSubPix(gray, q[i],cv::Size(11,11), cv::Size(-1,-1), cv::TermCriteria(cv::TermCriteria::EPS + cv::TermCriteria::MAX_ITER, 30, 0.1));
        Q.push_back(objp);
    }

    // Display
    cv::drawChessboardCorners(img, patternSize, q[i], patternFound);
    cv::imshow("chessboard detection", img);
    cv::waitKey(0);

    i++;
  }


  cv::Matx33f K(cv::Matx33f::eye());  // intrinsic camera matrix
  cv::Vec<float, 5> k(0, 0, 0, 0, 0); // distortion coefficients

  std::vector<cv::Mat> rvecs, tvecs;
  std::vector<double> stdIntrinsics, stdExtrinsics, perViewErrors;
  int flags = cv::CALIB_FIX_ASPECT_RATIO + cv::CALIB_FIX_K3 +
              cv::CALIB_ZERO_TANGENT_DIST + cv::CALIB_FIX_PRINCIPAL_POINT;
  cv::Size frameSize(1440, 1080);

  std::cout << "Calibrating..." << std::endl;
  // 4. Call "float error = cv::calibrateCamera()" with the input coordinates
  // and output parameters as declared above...

  float error = cv::calibrateCamera(Q, q, frameSize, K, k, rvecs, tvecs, flags);

  std::cout << "Reprojection error = " << error << "\nK =\n"
            << K << "\nk=\n"
            << k << std::endl;

  // Precompute lens correction interpolation
  cv::Mat mapX, mapY;
  cv::initUndistortRectifyMap(K, k, cv::Matx33f::eye(), K, frameSize, CV_32FC1,
                              mapX, mapY);

  // Show lens corrected images
  for (auto const &f : fileNames) {
    std::cout << std::string(f) << std::endl;

    cv::Mat img = cv::imread(f, cv::IMREAD_COLOR);

    cv::Mat imgUndistorted;
    // 5. Remap the image using the precomputed interpolation maps.
    cv::remap(img, imgUndistorted, mapX, mapY, cv::INTER_LINEAR);

    // Display
    cv::imshow("undistorted image", imgUndistorted);
    cv::waitKey(0);
  }

  return 0;
}

内容的提问来源于stack exchange,提问作者brian2lee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 13:24:28