2019-06-21 04:24:47 +08:00
|
|
|
/** \example paramscatterplot_image.cpp
|
2019-01-20 23:15:10 +08:00
|
|
|
* JKQTPlotter: Examples: Draw an Artistic Image with a Parametrized Scatter Graph
|
2019-01-13 01:53:16 +08:00
|
|
|
*
|
2019-01-20 23:15:10 +08:00
|
|
|
* \ref JKQTPlotterParamScatterImage
|
2019-01-13 01:53:16 +08:00
|
|
|
*/
|
|
|
|
|
2022-08-27 04:32:48 +08:00
|
|
|
#include "jkqtpexampleapplication.h"
|
2018-12-25 04:39:37 +08:00
|
|
|
#include <QApplication>
|
|
|
|
#include "jkqtplotter/jkqtplotter.h"
|
2019-06-20 22:06:31 +08:00
|
|
|
#include "jkqtplotter/graphs/jkqtpscatter.h"
|
|
|
|
#include "jkqtplotter/graphs/jkqtpimage.h"
|
2018-12-25 04:39:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2022-04-16 05:01:09 +08:00
|
|
|
|
2022-08-27 04:32:48 +08:00
|
|
|
JKQTPAppSettingController highDPIController(argc, argv);
|
|
|
|
JKQTPExampleApplication app(argc, argv);
|
2018-12-25 04:39:37 +08:00
|
|
|
|
2022-04-16 05:01:09 +08:00
|
|
|
|
2018-12-25 04:39:37 +08:00
|
|
|
// 1. create a plotter window and get a pointer to the internal datastore (for convenience)
|
2019-01-20 23:15:10 +08:00
|
|
|
JKQTPlotter plot;
|
2019-01-20 17:49:29 +08:00
|
|
|
JKQTPDatastore* ds=plot.getDatastore();
|
2018-12-25 04:39:37 +08:00
|
|
|
|
|
|
|
// 2. Prepare Data
|
|
|
|
// 2.1 load image
|
|
|
|
QImage image(":/example.bmp");
|
|
|
|
QVector<double> imageVector, pointSizes;
|
|
|
|
QVector<double> X,Y;
|
|
|
|
|
|
|
|
// 2.2 convert image to greyscale, stored in a vector in row-major order
|
|
|
|
double maxSymbolSize=30; // maximal diameter of symbols in pt
|
|
|
|
for (int y=0; y<image.height(); y++) {
|
|
|
|
for (int x=0; x<image.width(); x++) {
|
|
|
|
// calculate grey-value image vector
|
|
|
|
imageVector.push_back(qGray(image.pixel(x,y)));
|
|
|
|
// calculate point sizes from inverse grey value and scaling between 0 and maxSymbolSize
|
|
|
|
pointSizes.push_back(static_cast<double>(255-qGray(image.pixel(x,y)))/255.0*maxSymbolSize);
|
|
|
|
// calculate X/Y-coordinates (y mirrored, so image is upright)
|
|
|
|
X.push_back(x);
|
|
|
|
Y.push_back(image.height()-1-y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2.3 and copy it to the datastore
|
|
|
|
size_t columnX=ds->addCopiedColumn(X, "x");
|
|
|
|
size_t columnY=ds->addCopiedColumn(Y, "y");
|
|
|
|
size_t columnG=ds->addCopiedColumn(imageVector, "greyscaleImageData");
|
|
|
|
size_t columnS=ds->addCopiedColumn(pointSizes, "pointSizes");
|
|
|
|
|
|
|
|
// 3. add graphs to plot
|
|
|
|
// 3.1 Now add a parametrized scatter graph with columnX, columnY for the positions of the
|
|
|
|
// scatter points, where the symbol size is given by column columnS and the color of
|
|
|
|
// each symbol is set from column columnG, via a color palette JKQTPMathImageMATLAB
|
2019-01-20 17:49:29 +08:00
|
|
|
JKQTPXYParametrizedScatterGraph* graph1=new JKQTPXYParametrizedScatterGraph(&plot);
|
2019-01-26 20:00:40 +08:00
|
|
|
graph1->setXColumn(columnX);
|
|
|
|
graph1->setYColumn(columnY);
|
|
|
|
graph1->setSizeColumn(columnS);
|
|
|
|
graph1->setSymbolColumn(columnS);
|
2019-04-22 19:27:50 +08:00
|
|
|
graph1->setSymbolType(JKQTPFilledTriangle);
|
2019-01-26 20:00:40 +08:00
|
|
|
graph1->setColorColumn(columnG);
|
2020-09-11 18:14:51 +08:00
|
|
|
graph1->setColorPalette(JKQTPMathImageOCEAN);
|
2019-01-26 20:00:40 +08:00
|
|
|
graph1->setDrawLine(false);
|
|
|
|
graph1->setTitle("");
|
2018-12-25 04:39:37 +08:00
|
|
|
plot.addGraph(graph1);
|
|
|
|
|
|
|
|
// 3.2 add an image to display the original graphics
|
|
|
|
JKQTPImage* graph2=new JKQTPImage(&plot);
|
2019-01-26 20:00:40 +08:00
|
|
|
graph2->setImage(image);
|
|
|
|
graph2->setX(0);
|
|
|
|
graph2->setY(0);
|
|
|
|
graph2->setWidth(10);
|
|
|
|
graph2->setHeight(10);
|
2018-12-25 04:39:37 +08:00
|
|
|
plot.addGraph(graph2);
|
|
|
|
|
|
|
|
|
|
|
|
// 4. scale the plot so the graph is contained and format the coordinate system
|
2019-01-26 03:16:04 +08:00
|
|
|
plot.getXAxis()->setAxisLabel("x-axis");
|
|
|
|
plot.getYAxis()->setAxisLabel("y-axis");
|
2019-01-26 20:00:40 +08:00
|
|
|
plot.getXAxis()->setDrawGrid(false);
|
|
|
|
plot.getYAxis()->setDrawGrid(false);
|
2018-12-25 04:39:37 +08:00
|
|
|
// max. size is the size of the image
|
|
|
|
plot.setXY(0,image.width()-1,0,image.height()-1);
|
|
|
|
plot.setAbsoluteXY(0,image.width()-1,0,image.height()-1);
|
2019-02-08 00:24:46 +08:00
|
|
|
// ensure that axis aspect ratio and coordinate system aspect ratio are maintained
|
2019-01-26 03:16:04 +08:00
|
|
|
plot.getPlotter()->setMaintainAspectRatio(true);
|
|
|
|
plot.getPlotter()->setAspectRatio(1);
|
|
|
|
plot.getPlotter()->setMaintainAxisAspectRatio(true);
|
|
|
|
plot.getPlotter()->setAxisAspectRatio(1);
|
2018-12-25 04:39:37 +08:00
|
|
|
|
|
|
|
// 5. show plotter and make it a decent size
|
|
|
|
plot.show();
|
|
|
|
plot.resize(800,800);
|
|
|
|
|
|
|
|
return app.exec();
|
|
|
|
}
|