How to convert infrared to mat format?

Mat colorframe2mat(astra::ColorFrame colorframe)
{
int width = colorframe.width();
int height = colorframe.height();

Mat image(height, width, CV_8UC3);

const astra::RgbPixel* colorData = colorframe.data();

for (int y = 0; y < height; y++)
{
	for (int x = 0; x < width; x++)
	{
		image.at<Vec3b>(y, x)[0] = colorData[y * width + x].b;
		image.at<Vec3b>(y, x)[1] = colorData[y * width + x].g;
		image.at<Vec3b>(y, x)[2] = colorData[y * width + x].r;
	}
}
return image;

}

Mat depthframe2mat(astra::DepthFrame depthframe)
{
int width = depthframe.width();
int height = depthframe.height();

int grayval = 0;

Mat image(height, width, CV_8UC1);

for (int y = 0; y < height; y++)
{
	for (int x = 0; x < width; x++)
	{
		grayval = depthframe.data()[x + (y*width)] / (3500 / 256);
		image.at<uchar>(y, x) = grayval;
	}
}
return image;

}

Mat inframe2mat(astra::InfraredFrame16 irFrame)
{
int width = irFrame.width();
int height = irFrame.height();

Mat image(height, width, CV_8UC1);

int grayval = 0;
for (int y = 0; y < height; y++)
{
	for (int x = 0; x < width; x++)
	{
		grayval = irFrame.data()[x + y*width] / (3500 / 256);
		image.at<uchar>(y, x) = grayval;
	}
}

}

int main()
{
astra::initialize();

Mat depthimage;
Mat color;

astra::StreamSet streamSet;
astra::StreamSet streamSetC;
astra::StreamSet streamSetIR;


astra::ImageStreamMode irMode;

irMode.set_width(640);
irMode.set_height(480);
irMode.set_pixel_format(astra_pixel_formats::ASTRA_PIXEL_FORMAT_GRAY16);
irMode.set_fps(30);

astra::StreamReader readerD = streamSet.create_reader();
astra::StreamReader readerC = streamSetC.create_reader();
astra::StreamReader readerIR = streamSetIR.create_reader();

readerIR.stream<astra::InfraredStream>().set_mode(irMode);

readerD.stream<astra::DepthStream>().start();
readerC.stream<astra::ColorStream>().start();
readerIR.stream<astra::InfraredStream>().start();

do
{
	astra::Frame cFrame = readerC.get_latest_frame();
	const auto colorFrame = cFrame.get<astra::ColorFrame>();

	astra::Frame frame = readerD.get_latest_frame();
	const auto depthFrame = frame.get<astra::DepthFrame>();

	astra::Frame ir = readerIR.get_latest_frame();
	const auto irFrame = ir.get<astra::InfraredFrame16>();

	Mat rgbMat = colorframe2mat(colorFrame);
	depthimage = depthframe2mat(depthFrame);
	Mat irMat = inframe2mat(irFrame);

	//imshow("RGB", rgbMat);
	//imshow("Depth", depthimage);
	imshow("IR", irMat);

	if (waitKey(30) >= 0) break;



} while (1);

}

There is an error outputting the infrared image.

How should I fix it? I used only realsense and the aster device was very strange

HI… I’m not very much sure but you can not use depth and IR simultaneously. Try to use IR only…

Let me know if that helps…

You can use RGB+depth or IR+depth simultaneously but not RGB+IR(+depth).