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;
}
相关推荐
mys55184 分钟前
杨建允:AI搜索时代文旅品牌的GEO优化营销策略
人工智能·ai搜索优化·geo优化·文旅geo·文旅品牌geo
hsg778 分钟前
简述:torchgeo
人工智能·深度学习
ForDreamMusk11 分钟前
PyTorch编程基础
人工智能·pytorch
喵喵侠w12 分钟前
Rokid 智能眼镜开发实战:制作一款花草树木识别智能体
人工智能
小程故事多_8012 分钟前
从推理到智能体,大模型强化学习中信用分配机制的演进与突破
人工智能·prompt·aigc·ai编程
人工智能研究所16 分钟前
Claude + HyperFrames:用 HTML 方式制作视频,AI 时代一切皆可 HTML?
人工智能·html·音视频·ai 视频·hyperframes·claude-
是娇娇公主~24 分钟前
AI Agent详解
人工智能·ai agent
CV-杨帆26 分钟前
RAG 与记忆机制本质辨析及研究路径评估
人工智能
AI医影跨模态组学33 分钟前
如何通过MRI识别的系膜筋膜侵犯类型关联局部晚期直肠癌的免疫微环境及肿瘤增殖活性,并进一步解释其与预后不良的机制联系
人工智能·论文·医学·医学影像
人机与认知实验室36 分钟前
如何用四维矩阵建模计算性的态势感知与算计性的势态知感?
人工智能·线性代数·矩阵