opencv读取二进制灰度图并显示

cpp 复制代码
#include <iostream>
#include <fstream>
#include <vector>
#include <stdint.h>


#include <opencv2/opencv.hpp> // 包含OpenCV头文件

using namespace std;

// 注意:确保这些值与Python脚本中生成数据的值匹配。
const int WIDTH = 1920;
const int HEIGHT = 1080;
const int CHANNELS = 1;

int main() {
    // 用以存储从文件中读取的数据的缓冲区
    std::vector<uint8_t> buffer(HEIGHT * WIDTH * CHANNELS);

    // 打开文件
    std::ifstream file("hwc_1080_1920_1_uint8.bin", std::ios::binary);

    if (file.is_open()) {
        // 从文件读取数据到缓冲区中
        file.read(reinterpret_cast<char*>(buffer.data()), buffer.size() * sizeof(uint8_t));

        if (file) {
            std::cout << "All data read successfully." << std::endl;
        }
        else {
            std::cerr << "Error occurred while reading from the file." << std::endl;
            file.close();
            return 1;
        }

        file.close();

        std::cout << "x position: " << std::endl;
        cv::namedWindow("Canvas", cv::WINDOW_NORMAL);
        cv::Mat canvas(HEIGHT, WIDTH, CV_8UC3, cv::Scalar(255, 255, 255)); // 创建一个300x300像素的画布

        for(int i=0;i< WIDTH;i++){
            for (int j = 0; j < HEIGHT; j++) {
                float x_w = buffer[(j * WIDTH + i) * CHANNELS + 0]; // x_w值
                //std::cout << x_w << " ";
                cv::circle(canvas, cv::Point(i, j), 1, cv::Scalar(x_w, x_w, x_w), -1);
            }
            //std::cout << std::endl;
        }
        cv::resizeWindow("Canvas", 600, 400);
        cv::imshow("Canvas", canvas);
        cv::waitKey(0); // 等待10秒

        std::cout << "completed." << std::endl;

         处理读取到的数据。此处你可以添加自己的逻辑代码来处理buffer中的数据。
         例如,以下为访问某个特定像素的通道值的方式:
        //int y = 1079; // 第10行
        //int x = 960; // 第20列
        //float x_w = buffer[(y * WIDTH + x) * CHANNELS + 0]; // x_w值
        //float y_w = buffer[(y * WIDTH + x) * CHANNELS + 1]; // y_w值
        //float yaw = buffer[(y * WIDTH + x) * CHANNELS + 2]; // yaw值
        //float distance = buffer[(y * WIDTH + x) * CHANNELS + 3]; // distance值

        //std::cout << "Pixel at position (" << y << ", " << x << "): "
        //    << "x_w = " << x_w << ", "
        //    << "y_w = " << y_w << ", "
        //    << "yaw = " << yaw << ", "
        //    << "distance = " << distance << std::endl;

    }
    else {
        std::cerr << "Could not open the file." << std::endl;
        return 1;
    }

    return 0;
}
相关推荐
BulingQAQ1 小时前
论文阅读:PET/CT Cross-modal medical image fusion of lung tumors based on DCIF-GAN
论文阅读·深度学习·生成对抗网络·计算机视觉·gan
hsling松子1 小时前
使用PaddleHub智能生成,献上浓情国庆福
人工智能·算法·机器学习·语言模型·paddlepaddle
正在走向自律2 小时前
机器学习框架
人工智能·机器学习
好吃番茄2 小时前
U mamba配置问题;‘KeyError: ‘file_ending‘
人工智能·机器学习
CV-King3 小时前
opencv实战项目(三十):使用傅里叶变换进行图像边缘检测
人工智能·opencv·算法·计算机视觉
禁默3 小时前
2024年计算机视觉与艺术研讨会(CVA 2024)
人工智能·计算机视觉
whaosoft-1434 小时前
大模型~合集3
人工智能
Dream-Y.ocean4 小时前
文心智能体平台AgenBuilder | 搭建智能体:情感顾问叶晴
人工智能·智能体
丶21364 小时前
【CUDA】【PyTorch】安装 PyTorch 与 CUDA 11.7 的详细步骤
人工智能·pytorch·python
春末的南方城市5 小时前
FLUX的ID保持项目也来了! 字节开源PuLID-FLUX-v0.9.0,开启一致性风格写真新纪元!
人工智能·计算机视觉·stable diffusion·aigc·图像生成