c++ - 在使用 opencv 的 C++ 中,我有一个 uint 数组转换为灰度图像,只显示灰色屏幕没有图像

标签 c++ arrays opencv grayscale

我想知道是否有人可以帮助我解决这个问题。我有一个数组(单暗淡),它包含足够的 uint 值来填充尺寸为 120x160 的屏幕。我想将数组加载到垫子中并将其显示为灰度图像。我没看到有人可以帮忙吗??提前致谢。

myarray[39360];
//..
//Code populates values between 0 and 255 to the array
// I have printed the array and the values are all 0 to 255
//..
Mat img,img1;

// load the array into a mat
img(120,160,CV_8UC1,myarray);

// convert the mat to a grayscale image, 3 channel instead of the current 1
img.convertTo(img1,CV_8UC3);
namedWindow("test",WINDOW_AUTOSIZE);
imshow("test",img1);
waitKey(0);

// windows pops up but just shows a gray blank page about mid tone :-(

最佳答案

不确定为什么要使用 3 channel 的 Mat(CV_8UC3 表示每个像素 8 个字节,无符号,3 channel )如果您想要灰度图像,这里是您尝试执行的操作的完整示例:

#include "opencv2/highgui.hpp"
#include <vector>
#include <iostream>
#include <ctype.h>
#include <cstdlib>

int main()
{
    //  create a uint8_t array, can be unsigned char too
    uint8_t myArray[120*160];

    //  fill values
    srand(time(0));
    for (int i = 0; i < 120*160; ++i)
    {
        myArray[i] = (rand() % 255) + 1;
    }

    //  create grayscale image
    cv::Mat imgGray(120, 160, CV_8UC1, myArray);

    cv::namedWindow("test", cv::WINDOW_AUTOSIZE);
    cv::imshow("test", imgGray);
    cv::waitKey(0);

    return 0;
}

示例输出图像: enter image description here

关于c++ - 在使用 opencv 的 C++ 中,我有一个 uint 数组转换为灰度图像,只显示灰色屏幕没有图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52760484/

相关文章:

c++ - 检测音频流中的匹配声音

arrays - Solr 查询过滤文档,数组中至少有一个值(指定值除外)

java - 将具有相同值的数组添加到 HashSet 会导致重复项

opencv - 如何通过OpenCV用序列图像制作视频

c++ - 访问 typedef 映射结构中的成员?

c++ - 为 VTK 非结构化网格声明类型为 vtkSmartPointer<vtkDataArray> 的变量的问题

javascript - 将 ruby​​ 方法移植到 javascript

java - 如何将 BufferedImage 转换为 Mat (OpenCV)

c++ - 使用透明层 opencv 读取和显示 PNG 时出现问题

c++ - 在 C++ 中使用 shared_ptr 从公共(public)静态成员函数访问私有(private)构造函数