问题描述
我读bufferedImages从PNG图像,并将其转换为int使用PixelGrabber都会阵列。我的问题是那么:怎么我用整型数组来做出相应的OpenCV垫?其中数组是一维,每个值重新presenting一个像素的组合RGB值。
I am reading bufferedImages in from PNGs, and converting them to int arrays using the PixelGrabber. My question is then: how to I use the integer array to make the corresponding OpenCV Mat? Where the array is 1D, with each value representing a pixels combined RGB value.
使用字节数组我已经试过了,但我真的开始时,我使用它们混淆自己。
I have already tried using byte arrays, but I really start confusing myself when I use them.
推荐答案
只是间preT 32位的int值作为一个32位的RGBA值。我不知道为什么你没有改变通道的顺序,但使用int数组作为输入你的 CV ::垫
您自动获得一个BGRA排序。那么你只需要删除的alpha通道,如果需要的话。
just interpret the 32 bit int value as a 32 bit RGBA value. 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. Then you only have to remove the alpha channel, if needed.
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;
}
给我的输出:
values: int: 8453889 R = 128 G = 255 B = 1
BGRA ordering: [1, 255, 128, 0]
BGR ordering: [1, 255, 128]
这篇关于如何为int []转换为OpenCV的垫? (反之亦然)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!