简体   繁体   English

如何将int []转换为OpenCV Mat? (反之亦然)

[英]How to convert an int[] to an OpenCV Mat? (and vice-versa)

I am reading bufferedImages in from PNGs, and converting them to int arrays using the PixelGrabber. 我正在从PNG中读取bufferedImages,然后使用PixelGrabber将它们转换为int数组。 My question is then: how do I use the integer array to make the corresponding OpenCV Mat? 然后我的问题是:如何使用整数数组制作相应的OpenCV Mat? Where the array is 1D, with each value representing a pixel's combined RGB value. 其中数组是1D,每个值代表一个像素的组合RGB值。

I have already tried using byte arrays. 我已经尝试过使用字节数组。

just interpret the 32 bit int value as a 32 bit RGBA value. 只需将32位int值解释为32位RGBA值。 I dont know why you dont have to change the order of the channels, but using the int array as input for your cv::Mat you automatically get an BGRA ordering. 我不知道为什么您不必更改通道顺序,但是使用int数组作为cv::Mat输入,您会自动获得BGRA排序。 Then you only have to remove the alpha channel, if needed. 然后,您只需要删除Alpha通道即可。

int main()
{
    // the idea is that each int is 32 bit which is 4 channels of 8 bit color values instead of 3 channels, so assume a 4th channel.

    // first I create fake intArray which should be replaced by your input...
    const int imgWidth = 320;
    const int imgHeight = 210;
    int intArray[imgWidth*imgHeight]; // int array

    // fill the array with some test values:
    for(unsigned int pos = 0; pos < imgWidth*imgHeight; ++pos)
        intArray[pos] = 8453889; // 00000000 10000000 11111111 00000001 => R = 128, G = 255, B = 1
        //intArray[pos] = 65280;    // green
        //intArray[pos] = 16711680; // red
        //intArray[pos] = 255;  // blue

    // test:
    int firstVal = intArray[0];
    std::cout << "values: " << " int: " << firstVal << " R = " <<  ((firstVal >> 16) & 0xff) << " G = " << ((firstVal >> 8) & 0xff) << " B = " << (firstVal  & 0xff) << std::endl;

    // here you create the Mat and use your int array as input
    cv::Mat intMat_BGRA = cv::Mat(imgHeight,imgWidth,CV_8UC4, intArray); 
    // now you have a 4 channel mat with each pixel is one of your int, but with wrong order...
    std::cout << "BGRA ordering: " << intMat_BGRA.at<cv::Vec4b>(0,0) << std::endl;
    // this is in fact the BGRA ordering but you have to remove the alpha channel to get BGR values:
    // (unless you can live with BGRA values => you have to check whether there is garbage or 0s/255s in the byte area

    // so split the channels...
    std::vector<cv::Mat> BGRA_channels;
    cv::split(intMat_BGRA, BGRA_channels);

    // remove the alpha channel:
    BGRA_channels.pop_back();

    // and merge back to image:
    cv::Mat intMat_BGR;
    cv::merge(BGRA_channels, intMat_BGR);

    std::cout << "BGR ordering: " << intMat_BGR.at<cv::Vec3b>(0,0) << std::endl;

    cv::imshow("ordereed", intMat_BGR);

    cv::waitKey(0);
    return 0;
}

give me output: 给我输出:

values:  int: 8453889 R = 128 G = 255 B = 1
BGRA ordering: [1, 255, 128, 0]
BGR ordering: [1, 255, 128]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将byte转换为int,反之亦然 - Convert byte to int and vice-versa Java:如何将二进制值的字符串转换为Float,反之亦然? - Java: How to convert a String of Binary values to a Float and vice-versa? 有多少种方法可以将位图转换为字符串,反之亦然? - How many ways to convert bitmap to string and vice-versa? 如何将缓冲的图像转换为图像,反之亦然? - How to convert buffered image to image and vice-versa? 将xml转换为hashmap,反之亦然 - Convert xml to hashmap and vice-versa 将参数变量从int转换为char,反之亦然 - Casting Parameter Variables from int to char and vice-versa 如何编写 Apache 骆驼路线来检查 XML 内容并将其转换为 JSON 和 Z3418008FDD81C28 引导服务反之亦然 - How to write a Apache Camel route to check and convert XML contents to JSON and vice-versa for a Spring boot service 编年史队列:将循环整数转换为时间戳,反之亦然 - Chronicle Queue: Convert cycle integer to timestamp and vice-versa 如何在 JavaFX 中将数据从 WebView 传递到控制器,反之亦然 - How to pass data from WebView to controller and vice-versa in JavaFX 在 java 中从十六进制转换为整数,反之亦然 - Convert from hex to int and vice versa in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM