
2. 去掉多余的std::out,避免采集log过于杂乱; 3. 通过OpenCV从高光谱影像中提取rgb影像; 4. 在log中记录开始采集时间和停止采集时间; 5. 添加手动设置曝光时间的功能:127.0.0.1 7,2; 6. 头文件中写入仪器序列号;
254 lines
8.2 KiB
C++
254 lines
8.2 KiB
C++
//
|
||
// Created by tangchao on 2022/12/24.
|
||
//
|
||
|
||
#include "../Header_Files/rgbImage.h"
|
||
|
||
|
||
rgbImage::rgbImage(QWidget* pParent)
|
||
{
|
||
m_QRgbImage = nullptr;
|
||
m_matRgbImage = nullptr;
|
||
m_matFocusGrayImage = nullptr;
|
||
m_qimageFocusGrayImage = nullptr;
|
||
|
||
|
||
|
||
}
|
||
|
||
rgbImage::~rgbImage()
|
||
{
|
||
|
||
}
|
||
|
||
|
||
void rgbImage::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_8UC3, Scalar(0, 0, 0));
|
||
|
||
int codec = VideoWriter::fourcc('M', 'P', '4', '2'); // select desired codec (must be available at runtime)
|
||
double fps = 20.0; // framerate of the created video stream
|
||
std::string filename = "appsrc ! autovideoconvert ! filesink location=/media/nvme/delete/live.avi";//https://blog.csdn.net/ancientapesman/article/details/117324638
|
||
m_VideoWriter.open(filename, codec, fps, m_matRgbImage->size(), true);
|
||
|
||
// VideoWriter video("test.avi", CV_FOURCC('M', 'J', 'P', 'G'), 25.0, Size(640, 480));
|
||
|
||
// m_frame_writer = cvCreateVideoWriter("/media/nvme/delete/live.avi", cv::VideoWriter::fourcc('M', 'J', 'P', 'G'), 20.0, Size(688, 688), false);
|
||
|
||
|
||
|
||
|
||
|
||
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 rgbImage::FillOnerowofRgbImage(cv::Mat * matRgbImage, int rowNumber, unsigned short *datacube)
|
||
{
|
||
unsigned short r, g, b;
|
||
for (int j = 0; j < m_iSampleNumber; j++)
|
||
{
|
||
//取值:一帧影像中,从左到右的rgb像元值
|
||
r = *(datacube + 121 * m_iSampleNumber + j)*255/4096;
|
||
g = *(datacube + 79 * m_iSampleNumber + j)*255/4096;
|
||
b = *(datacube + 40 * m_iSampleNumber + j)*255/4096;
|
||
|
||
//将像元值赋值到cv::Mat中,操作像元值:https://zhuanlan.zhihu.com/p/51842288
|
||
//int dataType = m_matRgbImage->type();//当数据类型为CV_16UC3时,返回18
|
||
//std::cout << "m_matRgbImage数据类型为:" << dataType << std::endl;
|
||
if (matRgbImage->type() == CV_16UC3)
|
||
{
|
||
//std::cout << "操作像素值!" << std::endl;
|
||
matRgbImage->at<cv::Vec3w>(rowNumber, j)[2] = r;
|
||
matRgbImage->at<cv::Vec3w>(rowNumber, j)[1] = g;
|
||
matRgbImage->at<cv::Vec3w>(rowNumber, j)[0] = b;
|
||
}
|
||
}
|
||
}
|
||
|
||
void rgbImage::FillRgbImage(unsigned short *datacube)
|
||
{
|
||
unsigned short *r_row, *g_row, *b_row;
|
||
|
||
if(m_iFrameCounter<m_iFrameNumber)
|
||
{
|
||
FillOnerowofRgbImage(m_matRgbImage, m_iFrameCounter, datacube);
|
||
// std::cout << "小于:" << m_iFrameNumber << std::endl;
|
||
}
|
||
else
|
||
{
|
||
// std::cout << "大于:" << m_iFrameNumber << std::endl;
|
||
|
||
//通过行赋值将前m_iFrameNumber-1行向上移动一行,https://blog.csdn.net/u014686356/article/details/65937750
|
||
// m_matRgbImage->rowRange(1, m_matRgbImage->rows).copyTo(m_matRgbImage->rowRange(0, m_matRgbImage->rows-1));
|
||
for (int i = 1; i < m_matRgbImage->rows; ++i)
|
||
{
|
||
// std::cout << "大于:" << i << std::endl;
|
||
m_matRgbImage->col(i).copyTo(m_matRgbImage->col(i-1));
|
||
// std::cout << "--------------" << i << std::endl;
|
||
}
|
||
|
||
|
||
// std::cout << "1111111111111111111111111111"<< std::endl;
|
||
//通过FillOnerowofRgbImage为m_iFrameNumber行赋值
|
||
FillOnerowofRgbImage(m_matRgbImage, m_iFrameNumber-1, datacube);
|
||
// std::cout << "22222222222222222"<< std::endl;
|
||
|
||
|
||
|
||
|
||
|
||
// //循环给每行像素赋值
|
||
// r_row = datacube + 121 * m_iSampleNumber;
|
||
// g_row = datacube + 79 * m_iSampleNumber;
|
||
// b_row = datacube + 40 * m_iSampleNumber;
|
||
// for (int j = 0; j < m_iFrameNumber; j++)
|
||
// {
|
||
// p = m_matRgbImage.ptr<uchar>(j);
|
||
// for ( j = 0; j < nCols; ++j){
|
||
// p[j] = table[p[j]];
|
||
// }
|
||
//
|
||
// }
|
||
|
||
|
||
|
||
//保存rgb图片
|
||
if (m_iFrameCounter % m_iFramerate == 0 || m_iFrameCounter == m_iFrameNumber - 1)
|
||
{
|
||
////保存文件
|
||
//FileOperation * fileOperation = new FileOperation();
|
||
//string directory = fileOperation->getDirectoryOfExe();
|
||
//string rgbFilePathStrech = “/media/nvme/300TC/config/” + "\\tmp_image_strech.png";//没有拉伸图片
|
||
// std::string rgbFilePathNoStrech = "/media/nvme/300TC/config/" + std::to_string(m_iFrameCounter) + "ctmp_image_no_strech.png";
|
||
|
||
//m_QRgbImage->save(QString::fromStdString(rgbFilePathNoStrech), "PNG");
|
||
|
||
// cv::imwrite(rgbFilePathNoStrech, *m_matRgbImage);
|
||
//cv::imwrite(rgbFilePathStrech, CStretch(*m_matRgbImage, 0.01));
|
||
|
||
}
|
||
m_VideoWriter.write(*m_matRgbImage);
|
||
std::string rgbFilePathNoStrech = "/media/nvme/delete/" + std::to_string(m_iFrameCounter) + "ctmp_image_no_strech.png";
|
||
cv::imwrite(rgbFilePathNoStrech, *m_matRgbImage);
|
||
}
|
||
|
||
|
||
m_iFrameCounter++;
|
||
}
|
||
|
||
void rgbImage::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 rgbImage::FillFocusGrayQImage(unsigned short * datacube)
|
||
{
|
||
float two_eight = pow(2.0, 8);
|
||
float two_sixteen = pow(2.0, 12);
|
||
|
||
int width = m_qimageFocusGrayImage->width();
|
||
int height = m_qimageFocusGrayImage->height();
|
||
for (unsigned short i = 0; i < height; i++)
|
||
{
|
||
for (unsigned short j = 0; j < width; j++)
|
||
{
|
||
//uint tmp = (two_eight* *(datacube + width * i + j)) / two_sixteen;
|
||
uint tmp = (two_eight* datacube[width*i + j]) / two_sixteen;
|
||
//uint tmp = datacube[width*i + j];
|
||
|
||
|
||
//m_qimageFocusGrayImage->setPixel(j, i, tmp);
|
||
m_qimageFocusGrayImage->setPixel(j, i, qRgb((unsigned char)tmp, (unsigned char)tmp, (unsigned char)tmp));
|
||
}
|
||
}
|
||
|
||
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]));
|
||
|
||
|
||
}
|
||
}*/
|
||
|
||
}
|