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;
}
相关推荐
Work(沉淀版)1 小时前
DAY 40
人工智能·深度学习·机器学习
蓦然回首却已人去楼空2 小时前
Build a Large Language Model (From Scratch) 序章
人工智能·语言模型·自然语言处理
CM莫问2 小时前
<论文>(微软)WINA:用于加速大语言模型推理的权重感知神经元激活
人工智能·算法·语言模型·自然语言处理·大模型·推理加速
拾忆-eleven2 小时前
NLP学习路线图(二十六):自注意力机制
人工智能·深度学习
MYH5163 小时前
在NLP文本处理中,将字符映射到阿拉伯数字(构建词汇表vocab)的核心目的和意义
人工智能·深度学习·自然语言处理
要努力啊啊啊3 小时前
KV Cache:大语言模型推理加速的核心机制详解
人工智能·语言模型·自然语言处理
mzlogin5 小时前
DIY|Mac 搭建 ESP-IDF 开发环境及编译小智 AI
人工智能
jndingxin5 小时前
OpenCV CUDA模块图像处理-----对图像执行 均值漂移过程(Mean Shift Procedure)函数meanShiftProc()
图像处理·opencv
归去_来兮5 小时前
知识图谱技术概述
大数据·人工智能·知识图谱
就是有点傻6 小时前
VM图像处理之图像二值化
图像处理·人工智能·计算机视觉