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;
}
相关推荐
Amy187021118238 分钟前
数据中心电气接点测温:从“被动抢修”到“主动预警”的安全革命
人工智能·安全
jimmyleeee22 分钟前
GEN AI安全:威胁全景---从训练到运行的攻防实战
人工智能·安全
阿里云大数据AI技术36 分钟前
DataWorks Data Agent 实战课堂(八):数据质量巡检服务
人工智能·agent
昇腾知识体系42 分钟前
昇腾 A5 ISA 指令集:文档入口与 mem_bar 等关键指令
人工智能·华为·架构·知识图谱
ltqvibe1 小时前
让AI操作业务系统,误删数据谁来拦
人工智能·数字员工管理·高危操作拦截·ai审计追责
oscar9991 小时前
Ollie:Opik 内置的 AI 助手,让 Agent 调试从“看”变成“修”
人工智能·opik·ollie
wshzd1 小时前
LLM漫谈(十一)| 5 个 开源Agent 源码剖析
人工智能
酒旅Agent开发实战1 小时前
酒店供应链MCP实践分享
人工智能·大模型·酒店预订·ai agent·mcp
xsd202411181 小时前
篮球排球足球目标跟踪全解析:从多目标跟踪算法到球轨迹预测的技术
人工智能·目标跟踪