-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathimageacquisition.cpp
More file actions
416 lines (347 loc) · 13.7 KB
/
imageacquisition.cpp
File metadata and controls
416 lines (347 loc) · 13.7 KB
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#include "imageacquisition.h"
#include <QDebug>
#include <QWidget>
#include <QVBoxLayout>
#include <QLabel>
#include <QCoreApplication>
#include "defaults.h"
#include <exception>
#include <usoundutils.h>
// Setters and Getters
QString ImageAcquisition::getDeviceName() const
{
return deviceName;
}
void ImageAcquisition::setDeviceName(const QString &value)
{
deviceName = value;
}
int ImageAcquisition::getCounter() const
{
return counter;
}
void ImageAcquisition::setCounter(int value)
{
counter = value;
}
HalconCpp::HFramegrabber ImageAcquisition::getImageAcquisitionHandle() const
{
return imageAcquisitionHandle;
}
void ImageAcquisition::setImageAcquisitionHandle(const HalconCpp::HFramegrabber &value)
{
imageAcquisitionHandle = value;
}
bool ImageAcquisition::getStopAcquisition() const
{
return stopAcquisition;
}
void ImageAcquisition::setStopAcquisition(bool value)
{
stopAcquisition = value;
}
void ImageAcquisition::startAquisition(){
ImageAcquisition::run();
}
bool ImageAcquisition::getSupplyHistogramData() const
{
return supplyHistogramData;
}
void ImageAcquisition::setSupplyHistogramData(bool value)
{
supplyHistogramData = value;
}
QString ImageAcquisition::getDeviceType() const
{
return deviceType;
}
void ImageAcquisition::setDeviceType(const QString &value)
{
deviceType = value;
}
QString ImageAcquisition::getDeviceMake() const
{
return deviceMake;
}
void ImageAcquisition::setDeviceMake(const QString &value)
{
deviceMake = value;
}
// Constructor
bool ImageAcquisition::getSupplyHistogramDataForLine() const
{
return supplyHistogramDataForLine;
}
void ImageAcquisition::setSupplyHistogramDataForLine(bool value)
{
supplyHistogramDataForLine = value;
}
ImageAcquisition::ImageAcquisition(QString deviceType,QString deviceMake, QString deviceName, QObject *parent): QThread(parent)
{
HalconCpp::HFramegrabber imageAcquisitionHandle(HalconCpp::HString(deviceType.toUtf8().constData()).Text(),
0, 0, 0, 0, 0, 0,
"progressive", -1, "default",
-1, "false", "default",
HalconCpp::HString(
deviceName.toUtf8().constData()).Text(), 0, -1);
this->imageAcquisitionHandle = imageAcquisitionHandle;
this->deviceName = deviceName;
this->deviceType = deviceType;
this->deviceMake = deviceMake;
}
QList<QPair<int, int> > ImageAcquisition::getCoordinates() const
{
return coordinates;
}
void ImageAcquisition::setCoordinates(const QList<QPair<int, int> > &value)
{
coordinates = value;
}
void ImageAcquisition::run()
{
QImage qImage;
using namespace HalconCpp;
Hlong width,height;
QList<QLineSeries*> absoluteHistFrequencies;
QList<QString> colors = {"red","green","blue"};
this->imageAcquisitionHandle.GrabImageStart(0);
try {
while(!stopAcquisition)
{
currentImage = this->imageAcquisitionHandle.GrabImageAsync(0);
currentImage = currentImage.ZoomImageSize(IMAGE_CONFIGURATION::IMAGE_RESOLUTION_WIDTH, IMAGE_CONFIGURATION::IMAGE_RESOLUTION_HEIGHT, "constant");
width = currentImage.Width(); height = currentImage.Height();
if (imageRotation > 0.0){
currentImage = currentImage.RotateImage(imageRotation, "constant");
}
if (mirrorImageHorizontal){
currentImage = currentImage.MirrorImage("column");
}
if (mirrorImageVertical){
currentImage = currentImage.MirrorImage("row");
}
if (enableGrid){
HalconCpp::HRegion *grid = new HalconCpp::HRegion();
HalconCpp::HTuple *tuple= new HalconCpp::HTuple;
tuple->Append(255.0);
tuple->Append(255.0);
tuple->Append(255.0);
HalconCpp::HString *str = new HalconCpp::HString("fill");
HalconCpp::GenGridRegion(grid, IMAGE_CONFIGURATION::IMAGE_GRID_ROWS, IMAGE_CONFIGURATION::IMAGE_GRID_COLUMNS, "lines", currentImage.Width(), currentImage.Height());
currentImage = currentImage.PaintRegion(*grid, *tuple, *str);
}
auto conversionStatus = HImage2QImage(currentImage, qImage);
if (!conversionStatus)
{
// failed to convert himage to qimage. Handle it here
QCoreApplication::quit();
}
emit renderImageSignal(qImage);
if(getRecording()){
imageBuffer.enqueue(RecordingBuffer(currentImage, currentRecordSaveDir+QString::number(currentBufferImageCounter)+"."+IMAGE_CONFIGURATION::IMAGE_FORMAT));
emit updateStatusBarSignal(QString("Images in buffer %1").arg(currentBufferImageCounter));
currentBufferImageCounter+=1;
}
if(getSupplyHistogramData()){
int max=0;
int channels=currentImage.CountChannels().L();
if(channels == 3)
{
for(int c=1;c<=channels;c++)
{
QLineSeries *series = new QLineSeries();
HTuple relativeHisto, absoluteHisto;
HalconCpp::GrayHisto(HalconCpp::HRegion(0.0,0.0,double(width-1), double(height-1)), currentImage.AccessChannel(c), &absoluteHisto,&relativeHisto);
for(int l=0; l<absoluteHisto.Length()-1;l++)
{
long currentVal = absoluteHisto[l].L();
if(max<currentVal)
max = currentVal;
series->append(l, currentVal);
}
QPen pen = series->pen();
pen.setBrush(QBrush(colors.at(c-1).toUtf8().constData()));
series->setPen(pen);
absoluteHistFrequencies.append(series);
}
}
else{
QLineSeries *series = new QLineSeries();
HTuple relativeHisto, absoluteHisto;
HalconCpp::GrayHisto(HalconCpp::HRegion(0.0,0.0,double(width-1), double(height-1)), currentImage, &absoluteHisto,&relativeHisto);
for(int l=0; l<absoluteHisto.Length()-1;l++)
{
long currentVal = absoluteHisto[l].L();
if(max<currentVal)
max = currentVal;
series->append(l, currentVal);
}
absoluteHistFrequencies.append(series);
}
emit renderHistogramSignal(absoluteHistFrequencies, max);
absoluteHistFrequencies.clear();
}
if(getSupplyHistogramDataForLine())
{
// Data for this histogram is calculated in ImageStreamWindow.cpp createHistogramWindow function
int channels=currentImage.CountChannels().L();
if(channels == 3)
{
for(int c=1;c<=channels;c++)
{
QLineSeries *series = new QLineSeries();
QPen pen = series->pen();
pen.setBrush(QBrush(colors.at(c-1).toUtf8().constData()));
series->setPen(pen);
absoluteHistFrequencies.append(series);
}
for(int l=0; l<coordinates.size();l++)
{
int row = coordinates.at(l).second;
int column = coordinates.at(l).first;
HalconCpp::HTuple pixelIntensity = currentImage.GetGrayval(row,column);
absoluteHistFrequencies.at(0)->append(l,pixelIntensity.LArr()[0]);
absoluteHistFrequencies.at(1)->append(l,pixelIntensity.LArr()[1]);
absoluteHistFrequencies.at(2)->append(l,pixelIntensity.LArr()[2]);
}
}
else{
QLineSeries *series = new QLineSeries();
for(int l=0; l<coordinates.size();l++)
{
int pixelIntensity = currentImage.GetGrayval(coordinates.at(l).second, coordinates.at(l).first);
series->append(l, pixelIntensity);
}
absoluteHistFrequencies.append(series);
}
emit renderHistogramSignalForLine(absoluteHistFrequencies, coordinates.size());
absoluteHistFrequencies.clear();
}
counter++;
}
} catch (HalconCpp::HOperatorException &e) {
qDebug() << e.ErrorMessage().Text();
}
catch (HalconCpp::HException &e) {
qDebug() << e.ErrorMessage().Text();
}
catch (std::exception &e) {
qDebug() << e.what();
}
}
//*
// * @brief HImage2QImage Converts Halcon's HImage to Qt's QImage
// * @param from HImage , currently only supports 8bits grayscale image and 8bits 3 channel color image
// * @param to QImage , where from and to do not share memory. If the memory size of to is appropriate, then there is no need to redistribute memory. So you can speed up.
// * @return true means the conversion was successful, false means the conversion failed
bool ImageAcquisition::HImage2QImage(HalconCpp::HImage &from, QImage &to)
{
using namespace HalconCpp;
Hlong width;
Hlong height;
from.GetImageSize(&width, &height);
HTuple channels = from.CountChannels();
HTuple type = from.GetImageType();
if( strcmp(type[0].S(), "byte" )) // if it is not a byte type, it fails
{
return false;
}
QImage::Format format;
switch(channels[0].I())
{
case 1:
format = QImage::Format_Grayscale8;
break;
case 3:
format = QImage::Format_RGB32;
break;
default:
return false;
}
if(to.width() != width || to.height() != height || to.format() != format)
{
to = QImage(static_cast<int>(width),
static_cast<int>(height),
format);
}
HString Type;
if(channels[0].I() == 1)
{
unsigned char * pSrc = reinterpret_cast<unsigned char *>( from.GetImagePointer1(&Type, &width, &height) );
memcpy( to.bits(), pSrc, static_cast<size_t>(width) * static_cast<size_t>(height) );
return true;
}
else if(channels[0].I() == 3)
{
uchar *R, *G, *B;
from.GetImagePointer3(reinterpret_cast<void **>(&R),
reinterpret_cast<void **>(&G),
reinterpret_cast<void **>(&B), &Type, &width, &height);
for(int row = 0; row < height; row ++)
{
QRgb* line = reinterpret_cast<QRgb*>(to.scanLine(row));
for(int col = 0; col < width; col ++)
{
line[col] = qRgb(*R++, *G++, *B++);
}
}
return true;
}
return false;
}
HalconCpp::HTuple ImageAcquisition::getValueForParam(std::string paramString)
{
HalconCpp::HTuple paramValue;
try {
qDebug() << "Param string" << paramString.c_str();
paramValue = imageAcquisitionHandle.GetFramegrabberParam(paramString.c_str());
} catch (std::exception &e) {
qDebug() << e.what();
}
return paramValue;
}
//void ImageAcquisition::setValueForParam(std::string paramString, int paramValue)
//{
// try {
// qDebug() << "in int set param"<< paramString.c_str() << paramValue;
// imageAcquisitionHandle.SetFramegrabberParam(paramString.c_str(), paramValue);
// } catch (HalconCpp::HException &e) {
// qDebug() << "Exception in setting param value:int message"<<e.ErrorMessage().Text() << e.ErrorCode();
// }
//}
void ImageAcquisition::setValueForParam(std::string paramString, double paramValue)
{
try {
qDebug() << "double set param"<< paramString.c_str() << paramValue;
imageAcquisitionHandle.SetFramegrabberParam(paramString.c_str(), paramValue);
} catch (HalconCpp::HException &e) {
qDebug() << "Exception in setting param value:double message"<<e.ErrorMessage().Text() << e.ErrorCode();
}
}
void ImageAcquisition::setValueForParam(std::string paramString, bool paramState)
{
try {
qDebug() << "bool set param"<< paramString.c_str() << paramState;
imageAcquisitionHandle.SetFramegrabberParam(paramString.c_str(), paramState);
} catch (HalconCpp::HException &e) {
qDebug() << "Exception in setting param value:double message"<<e.ErrorMessage().Text() << e.ErrorCode();
}
}
//void ImageAcquisition::setValueForParam(std::string paramString, long paramValue)
//{
// try {
// qDebug() << "long set param"<< paramString.c_str() << paramValue;
// imageAcquisitionHandle.SetFramegrabberParam(paramString.c_str(), (Hlong)paramValue);
// } catch (HalconCpp::HException &e) {
// qDebug() << "Exception in setting param value:double message"<<e.ErrorMessage().Text() << e.ErrorCode();
// }
//}
void ImageAcquisition::setValueForParam(std::string paramString, std::string paramValue)
{
try {
qDebug() << "string set param"<< paramString.c_str() << paramValue.c_str();
imageAcquisitionHandle.SetFramegrabberParam(paramString.c_str(), paramValue.c_str());
} catch (HalconCpp::HException &e) {
qDebug() << "Exception in setting param value:string message"<<e.ErrorMessage().Text() << e.ErrorCode();
}
}