#include "mainwindow.h" #include "ui_mainwindow.h" #include #include #include "stim/visualization/colormap.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); s = new SpectrumWindow(this); } void MainWindow::loadImage(QString fileName){ std::string file = fileName.toUtf8().constData(); //load a dummy hyperspectral image for debugging hsi.open(file, file + ".hdr"); //initialize the size of the main window resize(hsi.header.samples+2, hsi.header.lines + ui->menuBar->size().height()); //fill the x axis with wavelength numbers from the hsi x = QVector::fromStdVector(hsi.header.wavelength); //load the middle band as the default load_band(x.size()/2); //set the pick position to the center of the image p = QPoint((int)(image.width()/2), (int)(image.height()/2)); //create a graphics scene to store the image and any drawn UI elements scene = new QGraphicsScene(this); //send the scene to the QGraphicsView object for display ui->graphicsView->setScene(scene); //create and show the spectrum window load_spectrum(); s->initPlot(x.first(), x.last(), 0, 1, hsi.header.wavelength_units.c_str(), "Absorbance"); s->show(); //draw everything draw_viewport(); } /// Update the image viewing widget so that it always fills the main window void MainWindow::resizeEvent(QResizeEvent* event){ //get the current window size QSize winsize = event->size(); //get the current position of the QGraphicsView object QPoint imagepos = ui->graphicsView->pos(); //update the size of the QGraphicsView object // the QGraphicsView will be either the size of the image or // the size of the window (whichever is smaller) QSize viewsize = QSize( std::min(winsize.width() - imagepos.x(), image.width()+2), std::min(winsize.height() - imagepos.y(), image.height()+2) ); ui->graphicsView->setFixedSize(viewsize.width(), viewsize.height()); } /// Update everything associated with when the image is clicked void MainWindow::mousePressEvent(QMouseEvent * event){ //get the window position of the click QPoint winp = event->pos(); //get the position of the QGraphicsView object (upper left corner of the image) QPoint imagep = ui->graphicsView->pos(); //get the size of the menu bar int menu_height = ui->menuBar->size().height(); //calculate the position of the click in the image p = winp - imagep - QPoint(0, menu_height); //load the spectrum associated with p load_spectrum(); draw_viewport(); } void MainWindow::mousePressSpectrum(double px, double py){ //calculate the band to be loaded // this will be the closest band to x unsigned int c = 0; double diff = abs(px - x[0]); for(unsigned int i=1; i(band, buffer, N, stim::cmBrewer); //store the color data in the image member variable image = QPixmap::fromImage( QImage( buffer, hsi.header.samples, hsi.header.lines, QImage::Format_RGB888 ) ); } void MainWindow::draw_viewport(){ scene->clear(); //add the image to the scene scene->addPixmap(image); //I'm not sure what this does... scene->setSceneRect(image.rect()); //draw the overlay indicating the selected pixel draw_overlay(); //draw the spectrum draw_spectrum(); } //draws an overlay over the image showing details that include the selected pixel void MainWindow::draw_overlay(){ //get the size of the QGraphicsView object QSize image_size = ui->graphicsView->size(); scene->addLine(0, p.y(), image_size.width(), p.y()); scene->addLine(p.x(), 0, p.x(), image_size.height()); } /// Loads the spectrum from the picked point of the HSI and stores it in the member variable y void MainWindow::load_spectrum(){ //allocate the appropriate amount of memory for the spectrum float* temp_y = (float*) malloc(x.size() * sizeof(float)); //load the spectrum from the HSI hsi.spectrum(temp_y, p.x(), p.y()); //clear the previous spectrum y.clear(); //copy the values into the member variable y for(int i = 0; i < x.size(); i++) y.push_back(temp_y[i]); } void MainWindow::draw_spectrum(){ //plot the saved spectrum s->plotSpectrum(x, y); } MainWindow::~MainWindow(){ delete ui; } void MainWindow::on_actionLoad_triggered() { QString fileName = QFileDialog::getOpenFileName(0, "Open ENVI File"); loadImage(fileName); }