有两种方法
第一种是
Qtemp = QImage((const unsigned char*)(img.data), img.cols, img.rows, img.step, QImage::Format_Indexed8);
第二种是
Qtemp = QImage((const unsigned char*)(img.data), img.cols, img.rows, img.step, QImage::Format_RGB16);
void MainWindow::on_pushButton_clicked()
{
IKinectSensor * mySensor = nullptr;
GetDefaultKinectSensor(&mySensor); //获取感应器
mySensor->Open();//打开感应器
IDepthFrameSource * mySource = nullptr; //取得深度数据
mySensor->get_DepthFrameSource(&mySource);
int height = 0, width = 0; //取得宽和高
IFrameDescription * myDescription = nullptr;
mySource->get_FrameDescription(&myDescription);
myDescription->get_Height(&height);
myDescription->get_Width(&width);
IDepthFrameReader * myReader = nullptr;
mySource->OpenReader(&myReader); //打开深度数据的Reader
IDepthFrame * myFrame = nullptr;
Mat temp(height, width, CV_16UC1); //建立临时图像矩阵
Mat img(height, width, CV_8UC1);
QImage Qtemp;
namedWindow("test", WINDOW_AUTOSIZE);
while (1)
{
if (myReader->AcquireLatestFrame(&myFrame) == S_OK) //通过Reader尝试获取最新的一帧深度数据,放入深度帧中,并判断是否成功获取
{
myFrame->CopyFrameDataToArray(height * width, (UINT16 *)temp.data); //先把数据存入16位的图像矩阵中
temp.convertTo(img, CV_8UC1); //再把16位转换为8位
imshow("test", img);
/*显示图片再在Label上*/
Qtemp = QImage((const unsigned char*)(img.data), img.cols, img.rows, img.step, QImage::Format_Indexed8);
//设置图片大小和label的长宽一致
Qtemp = Qtemp.scaled(ui->label->width(), ui->label->height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
//将图像显示在label上
ui->label->setPixmap(QPixmap::fromImage(Qtemp));
myFrame->Release();
}
if (waitKey(30) == VK_ESCAPE){break;}
//释放资源
}
myReader->Release();
myDescription->Release();
mySource->Release();
mySensor->Close();
mySensor->Release();
}
效果图如下
第二种方式
void MainWindow::on_pushButton_clicked()
{
IKinectSensor * mySensor = nullptr;
GetDefaultKinectSensor(&mySensor); //获取感应器
mySensor->Open();//打开感应器
IDepthFrameSource * mySource = nullptr; //取得深度数据
mySensor->get_DepthFrameSource(&mySource);
int height = 0, width = 0; //取得宽和高等下用
IFrameDescription * myDescription = nullptr;
mySource->get_FrameDescription(&myDescription);
myDescription->get_Height(&height);
myDescription->get_Width(&width);
IDepthFrameReader * myReader = nullptr;
mySource->OpenReader(&myReader); //打开深度数据的Reader
IDepthFrame * myFrame = nullptr;
Mat img(height, width, CV_16UC1);
QImage Qtemp;
namedWindow("test", WINDOW_AUTOSIZE);
while (1)
{
if (myReader->AcquireLatestFrame(&myFrame) == S_OK) //通过Reader尝试获取最新的一帧深度数据,放入深度帧中,并判断是否成功获取
{
myFrame->CopyFrameDataToArray(height * width, (UINT16 *)img.data); //先把数据存入16位的图像矩阵中
/*显示图片再QLabel上*/
Qtemp = QImage((const unsigned char*)(img.data), img.cols, img.rows, img.step, QImage::Format_RGB16);
//设置图片大小和label的长宽一致
Qtemp = Qtemp.scaled(ui->label->width(), ui->label->height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
//将图像显示在label上
ui->label->setPixmap(QPixmap::fromImage(Qtemp));
myFrame->Release();
}
if (waitKey(30) == VK_ESCAPE){break;}
//释放资源
}
myReader->Release();
myDescription->Release();
mySource->Release();
mySensor->Close();
mySensor->Release();
}