Files
HPPA/HPPA/image2display.cpp
tangchao0503 d326dabff7 add,深度相机:
1、采集惯导数据并写入文件;
2、轮播看板添加深度图像;
3、植物表型场景控制深度相机看板;

fix:
优化调焦时,图像像素的填充方式;
2026-04-14 13:14:55 +08:00

191 lines
5.6 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "stdafx.h"
#include "image2display.h"
#include <iostream>
CImage::CImage(QWidget* pParent) :QObject(pParent)
{
m_QRgbImage = nullptr;
m_matRgbImage = nullptr;
m_matFocusGrayImage = nullptr;
m_qimageFocusGrayImage = nullptr;
}
void CImage::SetRgbImageWidthAndHeight(int BandCount, int Sample, int FrameNumber)
{
using namespace cv;
if (m_QRgbImage != nullptr)
{
delete m_QRgbImage;//有问题????????????????????????????????????????????????
}
//m_QRgbImage = new QImage(Sample, FrameNumber, QImage::Format_RGB888);
if (m_matRgbImage != nullptr)
{
delete m_matRgbImage;
}
m_matRgbImage = new Mat(FrameNumber, Sample, CV_16UC3, Scalar(0, 0, 0));
if (m_qimageFocusGrayImage == nullptr)
{
m_qimageFocusGrayImage = new QImage(Sample, BandCount, QImage::Format_RGB32);
}
if (m_matFocusGrayImage == nullptr)
{
m_matFocusGrayImage = new Mat(BandCount, Sample, CV_16U, Scalar(0));
//cv::Mat matAdjustPreview = Mat::zeros(BandCount, Sample, CV_16U);
}
//cv::Mat matAdjustPreview = Mat::zeros(BandCount, Sample, CV_16U);
//m_matFocusGrayImage = matAdjustPreview;
std::cout << "设置帧数:" << FrameNumber << std::endl;
m_iFrameCounter = 0;//每次都重置为0
m_iSampleNumber = Sample;
m_iBandNumber = BandCount;
m_iFrameNumber = FrameNumber;
//std::cout << "rgb影像内存地址为" << m_QRgbImage << std::endl;
}
void CImage::FillRgbImage(unsigned short *datacube)
{
//uchar==unsigned char内存大小1个字节范围为0-255
//uchar * imagebits24 = m_QRgbImage->bits();
//uchar * imagebits24 = m_QRgbImage->scanLine(m_iFrameCounter);//??????????????????????????????????????????????????????????????????????
unsigned short r, g, b;
for (int j = 0; j < m_iSampleNumber; j++)
{
//std::cout << "rgb图像写入数据帧数" << j << std::endl;
//取值一帧影像中从左到右的rgb像元值
r = *(datacube + 121 * m_iSampleNumber + j);
g = *(datacube + 79 * m_iSampleNumber + j);
b = *(datacube + 40 * m_iSampleNumber + j);
//将像元值赋值到cv::Mat中操作像元值https://zhuanlan.zhihu.com/p/51842288
//int dataType = m_matRgbImage->type();//当数据类型为CV_16UC3时返回18
//std::cout << "m_matRgbImage数据类型为" << dataType << std::endl;
if (m_matRgbImage->type() == CV_16UC3)
{
//std::cout << "操作像素值!" << std::endl;
m_matRgbImage->at<cv::Vec3w>(m_iFrameCounter, j)[2] = r;
m_matRgbImage->at<cv::Vec3w>(m_iFrameCounter, j)[1] = g;
m_matRgbImage->at<cv::Vec3w>(m_iFrameCounter, j)[0] = b;
}
////将像元值从4095归一化到255的范围并赋值到QImage中
//imagebits24[j * 3] = uchar(r * 255 / 4095);
//imagebits24[j * 3 + 1] = uchar(g * 255 / 4095);
//imagebits24[j * 3 + 2] = uchar(b * 255 / 4095);
}
m_iFrameCounter++;
//保存rgb图片
if (m_iFrameCounter % m_iFramerate == 0 || m_iFrameCounter == m_iFrameNumber - 1)
{
////保存文件
//FileOperation * fileOperation = new FileOperation();
//string directory = fileOperation->getDirectoryOfExe();
//string rgbFilePathStrech = directory + "\\tmp_image_strech.png";//没有拉伸图片
//string rgbFilePathNoStrech = directory + "\\tmp_image_no_strech.png";//拉伸图片
//m_QRgbImage->save(QString::fromStdString(rgbFilePathNoStrech), "PNG");
//ImageProcessor imageProcessor;
//cv::imwrite(rgbFilePathNoStrech, *m_matRgbImage);
//cv::imwrite(rgbFilePathStrech, imageProcessor.CStretch(*m_matRgbImage, 0.02));
}
}
void CImage::FillFocusGrayImage(unsigned short * datacube)
{
int rowCount = m_matFocusGrayImage->rows;
int colCount = m_matFocusGrayImage->cols;
for (unsigned short i = 0; i < m_matFocusGrayImage->rows; i++)
{
for (unsigned short j = 0; j < m_matFocusGrayImage->cols; j++)
{
//m_matFocusGrayImage->at<ushort>(i, j) = *(datacube + m_matFocusGrayImage->cols*i + j);
m_matFocusGrayImage->at<ushort>(i, j) = datacube[m_matFocusGrayImage->cols*i + j];
}
}
//int rowCount = m_matFocusGrayImage.rows;
//int colCount = m_matFocusGrayImage.cols;
////memcpy(m_matFocusGrayImage.data, datacube, rowCount*colCount);
//for (unsigned short i = 0; i < m_matFocusGrayImage.rows; i++)
//{
// for (unsigned short j = 0; j < m_matFocusGrayImage.cols; j++)
// {
// m_matFocusGrayImage.at<ushort>(i, j) = *(datacube + m_matFocusGrayImage.cols*i + j);
// //m_matFocusGrayImage.at<ushort>(i, j) = datacube[colCount*i + j];
// }
//}
//将mat保存成文件
//cv::imwrite("D:/delete/2222222222/test.bmp", m_matFocusGrayImage);
}
void CImage::FillFocusGrayQImage(unsigned short * datacube)
{
constexpr float scale = 256.0f / 4096.0f; // 2^8 / 2^12
int width = m_qimageFocusGrayImage->width();
int height = m_qimageFocusGrayImage->height();
for (int i = 0; i < height; i++)
{
QRgb* scanLine = reinterpret_cast<QRgb*>(m_qimageFocusGrayImage->scanLine(i));
const unsigned short* srcRow = datacube + width * i;
for (int j = 0; j < width; j++)
{
unsigned char gray = static_cast<unsigned char>(srcRow[j] * scale);
scanLine[j] = qRgb(gray, gray, gray);
}
}
//m_qimageFocusGrayImage->save("D:/delete/2222222222/test.bmp");
/*float two_eight = pow(2.0, 8);
float two_sixteen = pow(2.0, 16);
QImage *qi = new QImage(imwidth, imheight, QImage::Format_RGB32);
for (int i = 0; i < imheight; i++)
{
for (int j = 0; j < imwidth; j++)
{
floatData[i*imwidth + j] = (two_eight* floatData[i*imwidth + j]) / two_sixteen;
qi->setPixel(j, i, qRgb((unsigned char)floatData[i*imwidth + j], (unsigned char)floatData[i*imwidth + j], (unsigned char)floatData[i*imwidth + j]));
}
}*/
}