Files
HPPA/HPPA/image2display.cpp
tangchao0503 1a64fb32e3 fix
1、默认渲染波段:可见光、近红外,采集中、打开文件;
2026-09-04 10:27:57 +08:00

191 lines
5.7 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, int rBandNumber, int gBandNumber, int bBandNumber)
{
//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 + rBandNumber * m_iSampleNumber + j);
g = *(datacube + gBandNumber * m_iSampleNumber + j);
b = *(datacube + bBandNumber * 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]));
}
}*/
}