为什么使用Python和C++将图像归一化为0到1时图像数据不同.我使用opencv

Why is the image data different when using Python and C++to normalize the image to 0 to 1.I use opencv

提问人:kireirain 提问时间:11/8/2023 更新时间:11/8/2023 访问量:35

问:

这是我的 C++ 代码:


void writeImageDataToFile(const cv::Mat& image, const std::string& filePath) {
    std::ofstream outputFile(filePath);
    if (!outputFile.is_open()) {
        std::cout << "Failed to open file for writing: " << filePath << std::endl;
        return;
    }

    for (int channel = 0; channel < image.channels(); ++channel) {
        outputFile << "Channel " << channel << ":" << std::endl;
        for (int row = 0; row < image.rows; ++row) {
            for (int col = 0; col < image.cols; ++col) {
                outputFile << std::fixed << std::setprecision(6) << image.at<cv::Vec3f>(row, col)[channel] << " ";
            }
            outputFile << std::endl;
        }
        outputFile << std::endl;
    }

    outputFile.close();
}

int cntt = -1;
for (const auto& directory : directories) {
    std::vector<std::string> imagePaths = GetImagePaths(directory);

    
    for (const auto& imagePath : imagePaths) {
        cv::Mat image = cv::imread(imagePath);
        if (image.empty()) {
            std::cout << "Failed to read image: " << imagePath << std::endl;
            continue;
        }

        cntt++;
        cv::cvtColor(image, image, cv::COLOR_BGR2RGB); 
    

        cv::Mat resizedImage;
        cv::resize(image, resizedImage, cv::Size(32, 32));
        
        cv::Mat normalizedImage;
        resizedImage.convertTo(normalizedImage, CV_32F);

        cv::Scalar new_mean(0.9966, 0.9973, 0.9974);
        cv::Scalar new_std(0.0102, 0.0078, 0.0074);

    
        normalizedImage /= 255.0;
        for (int i = 0; i < 3; i++) {
            normalizedImage.row(i) = (normalizedImage.row(i) - new_mean[i]) / new_std[i];
        }
        
    
        std::string outputFilePath = "normalized_image_data" + std::to_string(cntt) + ".txt";
        writeImageDataToFile(normalizedImage, outputFilePath);
        
        
    }
}

这是我的python代码:

import os
import cv2
import numpy as np

def write_image_data_to_file(image, file_path):
    with open(file_path, 'w') as output_file:
        for channel in range(image.shape[0]):
            output_file.write(f"Channel {channel}:\n")
            for row in range(image.shape[1]):
                for col in range(image.shape[2]):
                    output_file.write(f"{image[channel, row, col]:.6f} ")
                output_file.write("\n")
            output_file.write("\n")


input_folder = 'input0'  # Change to your input folder name
output_folder = 'output_folder3'  # Change to your desired output folder name
new_mean = [0.9966, 0.9973, 0.9974]
new_std = [0.0102, 0.0078, 0.0074]


if not os.path.exists(output_folder):
    os.makedirs(output_folder)


for filename in os.listdir(input_folder):
    if filename.endswith(".jpg"):
        image_path = os.path.join(input_folder, filename)

        
        image = cv2.imread(image_path)

        
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        image = cv2.resize(image, (32, 32))
        image = np.transpose(image, (2, 0, 1))

        
        image = (image / 255.0).astype(np.float32)
        for i in range(3):
            image[i] = (image[i] - new_mean[i]) / new_std[i]

        
        output_file_path = os.path.join(output_folder, filename.replace(".jpg", ".txt"))

        
        write_image_data_to_file(image, output_file_path)

但我可以看到,使用 C++ 代码打印图像数据的 Txt 文件: normalized_image_data0.txt:(“......“代表省略相同) 图片 : 3x32x32

Channel 0:
0.333336 0.333336 0.333336 0.333336 0.333336 0.333336 0.333336 0.333336 0.333336 0.333336 0.333336 0.333336 ...(32 count)
0.346153 0.346153  ...
0.351349 0.351349 ...
1.000000 1.000000 ...
1.000000 1.000000 ...
1.000000 1.000000 ...
1.000000 1.000000 ...
1.000000 1.000000 ...
...

Channel 1:
0.333336 0.333336 0.333336 0.333336 0.333336 0.333336 0.333336 0.333336 0.333336 0.333336 0.333336 ...
0.346153 0.346153 ...
0.351349 0.351349 ...
1.000000 1.000000 ...
1.000000 1.000000 ...
1.000000 1.000000 ...
...
1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 0.980392 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 
1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 0.956863 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 0.992157 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 
1.000000 1.000000 ...
1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 0.992157 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 
1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 0.996078 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 
...

Channel 2:
0.333336 0.333336 ...
0.346153 0.346153 ...
0.351349 0.351349...
1.000000 1.000000 ...
1.000000 1.000000 ...
...
1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 0.992157 0.988235 0.980392 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 
1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 0.792157 0.988235 1.000000 1.000000 1.000000 0.984314 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 
...


使用 Python 代码打印图像数据的 txt 文件:

Channel 0:
0.333336 0.333336 ...
0.333336 0.333336 ...
0.333336 0.333336 ...
...

Channel 1:
0.346150 0.346150 ...
0.346150 0.346150 ...
...
Channel 2:
0.351353 0.351353 ...
0.351353 0.351353 ...
...

图像数据完全不同。为什么? 我现在需要确保 C++ opencv 中预处理的图像与 Python opencv 中的图像相同。这是我的最终目标。这是对应于 0.txt 的原始图像 0.jgp

enter image description here

蟒蛇 C++ OpenCV

评论

0赞 OM222O 11/8/2023
您是否尝试过减去从每种方法生成的数组?由于浮点数与双精度,可能会出现舍入错误(我找不到您在 C++ 中使用的类型,但 numpy 是浮点数)。尝试收集有关平均像素增量的统计信息,以查看误差是否可接受
1赞 463035818_is_not_an_ai 11/8/2023
看起来你的尺寸有些移位。python 输出中的第一个通道似乎是 c++ 输出中第一个通道的第一行
0赞 pptaszni 11/8/2023
你能用 C++ 代码展示一下你的是什么吗?看起来与这种情况类似image.type()

答: 暂无答案