一、前言
Qt的QGraphicsView类具有强大的视图功能,与其一起使用的还有QGraphicsScene类和QGraphicsItem类。大体思路就是通过构建场景类,然后向场景对象中增加各种图元,最后利用QGraphicsView类对场景进行显示。
二、绘制光谱曲线
实现功能主要有:
1 传递点击图像行列号,然后绘制曲线,代码如下:
void SpectralProfileDialog::AddPressSlot()
{
//setModal(0);
HyperProcess *ptr = (HyperProcess*)parentWidget();
curFileName = ptr->curFileName;
currentRow = ptr->curRow;
currentCol = ptr->curCol; if(curFileName == "")
{
QMessageBox::information(this,"Message Error","Current Scene IS NULL!");
return;
} if(currentCol == - || currentRow == -)
{
QMessageBox::information(this,"Message Error","please Select A Pixel");
return;
} cv::Mat curImg = GDALOpenCV::GDAL2Mat(curFileName);
if(currentCol > curImg.cols || currentRow > curImg.rows)
return; int count = curImg.channels(); std::vector<cv::Mat> curImgMat(count);
cv::split(curImg,curImgMat); QVector<QPointF> pixSpectralData(count);
double tmpMaxValue;
double tmpMinValue;
double maxValue = ;
for(int c = ;c<count;c++)
{
cv::minMaxIdx(curImgMat[c],&tmpMinValue,&tmpMaxValue);
if(maxValue < tmpMaxValue)
maxValue = tmpMaxValue;
pixSpectralData[c].setX(curImgMat[c].at<float>(currentCol,currentRow));
pixSpectralData[c].setY(c+);
} float x_scale = 1.0*(ui->spectralGraphicsView->height()-)/maxValue;
float y_scale = 1.0*(ui->spectralGraphicsView->width()-)/count; QPen pen;
pen.setWidth();
QColor qc=QColor::fromHsl(rand()%,rand()%,rand()%);
pen.setColor(QColor(qc.red(),qc.green(),qc.blue())); for(int c = ;c<curImg.channels()-;c++)
{
myScene->addLine(pixSpectralData[c].y()*y_scale,-1.0*(pixSpectralData[c].x()*x_scale),
pixSpectralData[c+].y()*y_scale,-1.0*(pixSpectralData[c+].x()*x_scale),pen);
}
ui->spectralGraphicsView->setScene(myScene);
}
三、效果图