当前位置: 首页 > article >正文

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

#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;
}

http://www.kler.cn/a/152489.html

相关文章:

  • 15 网关实战: 微服务集成Swagger实现在线文档
  • RPC与HTTP的详细比较
  • maven 基础
  • 大屏适配方案(vw、vh)
  • 抑制过拟合——从梯度的角度看LayerNorm的作用
  • Vue3 配置自动导入的步骤
  • Android 11.0 软硬键盘同时使用的兼容(软键盘与内置物理键盘共存)
  • CSS新手入门笔记整理:CSS列表样式
  • 1.自动化运维工具Ansible的安装
  • Linux CentOS7 安装Docker
  • Day51:503.下一个更大元素II、42. 接雨水
  • 全栈软件开发工程师需要具备哪些技能
  • ElementPlus自定义表单验证
  • 删除容器挂载卷打包容器镜像并传到阿里云
  • c++--类型的基础
  • 43.0BaseDao抽取dao公共父类
  • 大数据(十一):概率统计基础
  • STM32/GD32_分散加载
  • Paraformer 语音识别原理
  • Android Audio实战——音频焦点监听(十)