/
matrixofpixels.cpp
379 lines (327 loc) · 9.62 KB
/
matrixofpixels.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#include "matrixofpixels.h"
#include <iostream>
#include <stdio.h>
#include <fstream>
//#include <iomanip>
#include <qwt_legend.h>
#include <QMessageBox>
//#include <QFile>
/*
matrix of 256x256 pixels
*/
class ColorMap: public QwtLinearColorMap
{
public:
ColorMap(int colormap);
};
ColorMap::ColorMap(int colormap)
{
// enum ColorMapForPlot {bluered = 1, blackwhite = 2};
switch (colormap)
{
case 1:
setColorInterval(Qt::darkBlue, Qt::darkRed);
addColorStop(0.2, Qt::blue);
addColorStop(0.4, Qt::cyan);
addColorStop(0.6, Qt::yellow);
addColorStop(0.8, Qt::red);
break;
case 2:
setColorInterval(Qt::black, Qt::white);
break;
case 3:
setColorInterval(QColor(0,0,0),QColor(0,255,255));
break;
default:
setColorInterval(Qt::darkBlue, Qt::darkRed);
}
}
matrixofpixels::matrixofpixels(QWidget *parent):
QwtPlot( parent )
{
MapPlot = new QwtPlotSpectrogram();
his = new Histogram("Hist",Qt::red);
//###################ZoomInd = false;
if (maskdatavector.size()>0) maskdatavector.clear();
for (int i=0;i<Npix;i++)
{
for (int j=0;j<Npix;j++)
{
masked[i][j] = 1.;
maskdatavector.push_back(1.);
}
}
numofnotmasked = Npixels;
}
matrixofpixels::~matrixofpixels()
{
std::cout << " DESTRUCTOR blyaaaa " << std::endl;
delete MapPlot;
delete his;
delete grid;
//##############delete myzoom;
// delete MapPlot;
//delete rasterpixdata;
//delete MainPlot;
//HistogramData();
}
bool matrixofpixels::ifmatrixpixfile(QString namefilewithpixels)
{
FILE *c;
int i=0;
float tmp;
int ifnum;
const char* charnamefilewithpixels = namefilewithpixels.toLocal8Bit().constData();
c = fopen(charnamefilewithpixels,"r");
while(feof(c) == 0 )
{
ifnum = fscanf(c,"%e",&tmp);
if (!ifnum) {fclose(c); return false;}
i++;
}
fclose(c);
//std::cout << i << std::endl;
if (i==Npixels+1) return true;
else return false;
}
bool matrixofpixels::ifmatrixmaskfile(QString namefilewithmask)
{
FILE *c;
int i=0;
int tmp;
int ifnum;
const char* charnamefilewithmask = namefilewithmask.toLocal8Bit().constData();
c = fopen(charnamefilewithmask,"r");
while(feof(c) == 0 )
{
ifnum = fscanf(c,"%i",&tmp);
if (!ifnum) {fclose(c); return false;}
if (tmp!=0 && tmp!=1) {fclose(c); return false;}
i++;
}
fclose(c);
if (i==Npixels+1) return true;
else return false;
}
int matrixofpixels::getmatrixofpixels(QString namefilewithpixels)
{
if (!ifmatrixpixfile(namefilewithpixels))
{
QMessageBox::information(0,"Information","File does not contain information \nabout the pixels or is empty");
return 0;
}
const char* charnamefilewithpixels = namefilewithpixels.toLocal8Bit().constData();
float tmpfloat;
std::ifstream inmat(charnamefilewithpixels);
if (matdatavector.size()>0) matdatavector.clear();
for (int i=0;i<Npix;i++)
{
for (int j=0;j<Npix;j++)
{
inmat >> tmpfloat;
matrix[i][j] = tmpfloat;
if (i==j && i==0) MaxElOfMat = tmpfloat;
if (tmpfloat>MaxElOfMat) MaxElOfMat = tmpfloat;
matdatavector.push_back((double)tmpfloat);
}
}
inmat.close();
return 1;
}
int matrixofpixels::getmaskedofpixels(QString namefilewithmask)
{
if (!ifmatrixmaskfile(namefilewithmask))
{
QMessageBox::information(0,"Information","File does not contain information \nabout the masked pixels or is empty");
return 0;
}
const char* charnamefilewithmask = namefilewithmask.toLocal8Bit().constData();
float tmpint;
int tmpperc=0;
std::ifstream inmask(charnamefilewithmask);
if (maskdatavector.size()>0) maskdatavector.clear();
for (int i=0;i<Npix;i++)
{
for (int j=0;j<Npix;j++)
{
inmask >> tmpint;
masked[i][j] = tmpint;
if (tmpint) tmpperc++;
maskdatavector.push_back((double)tmpint);
}
}
//####### if (ZoomInd) {delete myzoom; ZoomInd = false;}
numofnotmasked = tmpperc;
inmask.close();
return 1;
}
float matrixofpixels::MaxElementOfMatrix()
{
float tmpmax;
for (int i=0;i<Npix;i++)
{
for (int j=0;j<Npix;j++)
{
if (i==j && i==0) tmpmax = matrix[i][j];
if ((matrix[i][j]>tmpmax)&&(masked[i][j])) tmpmax = matrix[i][j];
}
}
return tmpmax;
}
void matrixofpixels::flipudmasked()
{
// flip matrix of mask
int tmp;
if (maskdatavector.size()!=Npixels) return;
for (int i=0;i<Npix/2;i++)
{
for (int j=0;j<Npix;j++)
{
tmp = masked[i][j];
masked[i][j] = masked[(Npix-1)-i][j];
maskdatavector[Npix*i+j] = masked[(Npix-1)-i][j];
masked[(Npix-1)-i][j] = tmp;
maskdatavector[Npix*((Npix-1)-i)+j] = tmp;
}
}
}
void matrixofpixels::PixPlotSpect(int TypeOfSpec,QwtMatrixRasterData *rasterpixdata)
{
changeitems();
MapPlot->setData(rasterpixdata);
MapPlot->setRenderThreadCount(0);
setAxisAutoScale(this->xTop);
setAxisAutoScale(this->xBottom);
switch (TypeOfSpec)
{
case 1:
MapPlot->setColorMap(new ColorMap(BLUERED));
break;
case 2:
MapPlot->setColorMap(new ColorMap(BLACKWHITE));
break;
default:
MapPlot->setColorMap(new ColorMap(BLUERED));
}
MapPlot->attach(/*MainPlot*/this);
const QwtInterval zInterval = MapPlot->data()->interval(Qt::ZAxis);
setAxisScale(QwtPlot::xBottom, 0, Npix);
setAxisMaxMinor(QwtPlot::xBottom, 0);
setAxisScale(QwtPlot::yLeft, 0, Npix);
setAxisMaxMinor(QwtPlot::yLeft, 0);
//rightAxis = new QwtScaleWidget();
QwtScaleWidget *rightAxis = axisWidget(QwtPlot::yRight);
//rightAxis = axisWidget(QwtPlot::yRight);
rightAxis->setColorBarEnabled(true);
rightAxis->setColorBarWidth(20);
rightAxis->setColorMap(zInterval, new ColorMap(TypeOfSpec) );
//ColorMap(TypeOfSpec);
plotLayout()->setAlignCanvasToScales(true);
setAxisScale(QwtPlot::yRight,zInterval.minValue(),zInterval.maxValue());
enableAxis(QwtPlot::yRight);
replot();
setAutoFillBackground(true);
/**
setAutoFillBackground(true); - autozapolnenie, ctobi isklyuchit'
nalozjeniya graphikov
*/
}
void matrixofpixels::createdatavector()
{
if(datavector.size()) datavector.clear();
int k=0;
for (int i = 0;i<Npix;i++)
{
for (int j = 0;j<Npix;j++)
{
if (masked[i][j]) datavector.push_back(matrix[i][j]);
else {if (masked[i][j] != matrix[i][j]) k++;}
}
}
//std::cout << " datavector.size() " << datavector.size() << " k = " << k <<std::endl;
}
void matrixofpixels::createdatavector(double Xmin, double Xmax)
{
if(datavector.size()) datavector.clear();
int k=0;
float NM,NON; // NON - number of noise NM - number of not masked
NM = NON = 0;
for (int i = 0;i<Npix;i++)
{
for (int j = 0;j<Npix;j++)
{
if (masked[i][j])
{
if(matrix[i][j]>=Xmin && matrix[i][j]<=Xmax)
{
datavector.push_back(matrix[i][j]);
}
else
{
if(matrix[i][j]<Xmin) {datavector.push_back((float)Xmin);/*std::cout<<"float xmin " << (float)Xmin;*/}
if(matrix[i][j]>Xmax) {datavector.push_back((float)Xmax);NON++;}
}
NM++;
}
else {if (masked[i][j] != matrix[i][j]) k++;}
}
}
// 0 - number of noise 1 - number of not masked 2 - percent of noise
numofnoise[0] = NON;
numofnoise[1] = NM;
numofnoise[2] = (NON/NM)*100.;
}
void matrixofpixels::createhistogram(double *limitsofvalues)
{
//createdatavector();
//HistogramData(limitsofvalues);
createdatavector();
double maxx = (double)MaxElementOfMatrix();
double Ymax = his->createDataforHist(datavector,0,maxx,100);
limitsofvalues[0]=0;
limitsofvalues[1]=maxx;
limitsofvalues[2]=100;
HistogramPlotProperty(0,maxx,(Ymax+Ymax*0.1));
}
void matrixofpixels::createhistogram(double Xmin, double Xmax, int nbins)
{
createdatavector(Xmin,Xmax);
double Ymax = his->createDataforHist(datavector,Xmin,Xmax,nbins);
HistogramPlotProperty(Xmin,Xmax,(Ymax+Ymax*0.1));
}
void matrixofpixels::HistogramPlotProperty(double Xmin, double Xmax, double Ymax)
{
changeitems();
setAxisAutoScale(this->xTop);
setAxisAutoScale(this->xBottom);
//setAxisFont(QwtPlot::xBottom,QFont("Helvetica",15,1));
QwtText xlabel("Value");
QwtText ylabel("Events");
QColor col(Qt::red);
xlabel.setColor(col);
ylabel.setColor(col);
xlabel.setFont(QFont("Helvetica",15,1));
ylabel.setFont(QFont("Helvetica",15,1));
setAxisTitle(QwtPlot::xBottom,xlabel);
setAxisTitle(QwtPlot::yLeft,ylabel);
setCanvasBackground(QColor(Qt::white));
setAxisScale(QwtPlot::xBottom, Xmin, Xmax);
setAxisMaxMinor(QwtPlot::xBottom, 0);
setAxisScale(QwtPlot::yLeft, 0, Ymax);
setAxisMaxMinor(QwtPlot::yLeft, 0);
plotLayout()->setAlignCanvasToScales(true);
his->attach(this);
/*QwtPlotGrid **/grid = new QwtPlotGrid;
grid->setMajPen(QPen(Qt::gray, 0, Qt::DotLine));
grid->attach(this);
replot();
}
void matrixofpixels::changeitems()
{
QwtPlotItemList items = itemList(QwtPlotItem::Rtti_PlotItem);
for ( int i = 0; i < items.size(); i++ )
{
items[i]->detach();
}
this->replot();
}