-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProcessingCore.cpp
769 lines (684 loc) · 25.9 KB
/
ProcessingCore.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
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
#include "StdAfx.h"
#include "ProcessingCore.h"
#include <opencv2/core.hpp>
#include "opencv2/core/types_c.h"
#include <cmath>
using namespace std;
ProcessingCore::ProcessingCore(void)
{
}
std::string ProcessingCore::convertToStdString( QString & s )
{
std::string stds = s.toLocal8Bit().constData();
return stds;
}
cv::Mat ProcessingCore::readImage(QString & s)
{
cv::Mat image = cv::imread(ProcessingCore::convertToStdString(s), -1);
int channelsNumber = image.channels();
switch(channelsNumber)
{
case 1:
break;
case 3:
cvtColor(image, image, cv::COLOR_BGR2RGB);
break;
case 4:
cvtColor(image, image, cv::COLOR_BGRA2RGBA);
break;
}
return image;
}
cv::Mat ProcessingCore::RGB2BGR(cv::Mat image)
{
cv::Mat newImage;
int channelsNumber = image.channels();
switch(channelsNumber)
{
case 1:
newImage = image;
break;
case 3:
cvtColor(image, newImage, cv::COLOR_RGB2BGR);
break;
case 4:
cvtColor(image, newImage, cv::COLOR_RGBA2BGRA);
break;
}
return newImage;
}
bool ProcessingCore::writeImage(QString fileName, cv::Mat image)
{
bool success = false;
cv::Mat newImage = ProcessingCore::RGB2BGR(image);
success = cv::imwrite(ProcessingCore::convertToStdString(fileName), newImage);
return success;
}
QImage* ProcessingCore::convertToQImage( cv::Mat& matrix)
{
QImage* img;
int channelsNumber;
//Mat resized;
//cv::resize(matrix, resized, Size(width, height), 0, 0);
channelsNumber = matrix.channels();
switch(channelsNumber)
{
case 1:
//resized = matrix;
img = new QImage((uchar*)matrix.data, matrix.cols, matrix.rows, matrix.step, QImage::Format_Indexed8);
break;
case 3:
// cvtColor(matrix, matrix, CV_BGR2RGB);
img = new QImage((uchar*)matrix.data, matrix.cols, matrix.rows, matrix.step, QImage::Format_RGB888);
break;
case 4:
// cvtColor(matrix, matrix, CV_BGRA2RGBA);
img = new QImage((uchar*)matrix.data, matrix.cols, matrix.rows, matrix.step, QImage::Format_ARGB32);
break;
}
return img;
}
vector<QImage*> ProcessingCore::splitToQImage(cv::Mat matrix)
{
vector<QImage*> result;
vector<cv::Mat> channels;
int channelsNumber;
channelsNumber = matrix.channels();
cv::split(matrix, channels);
for (int i = channelsNumber-1; i>=0; i--)
{
cv::Mat* ch = new cv::Mat(channels[i]);
result.push_back(new QImage((uchar*)ch->data, ch->cols, ch->rows, ch->step, QImage::Format_Indexed8));
}
return result;
}
cv::Mat ProcessingCore::getChannel(Channels ch, cv::Mat & matrix)
{
cv::Mat result;
int channelsNumber = matrix.channels();
if (channelsNumber>ch && ch>=0)
{
vector<cv::Mat> channels;
cv::split(matrix, channels);
result = channels[ch];
}
return result;
}
QRresult ProcessingCore::mgs_qr(cv::Mat& A)
{
QRresult result;
result.Q = A;
result.Q.convertTo(result.Q, CV_32F);
int Width = result.Q.cols;
result.R = cv::Mat::zeros(Width, Width, CV_32F);
for (int j = 0; j<Width; j++)
{
result.R.at<float>(j ,j) = norm(result.Q.col(j));
result.Q.col(j) = result.Q.col(j)/result.R.at<float>(j ,j);
for (int k=j+1; k<Width;k++)
{
cv::Mat temp = result.Q.col(j).t()*result.Q.col(k);
int rows = temp.rows; int cols = temp.cols;
result.R.at<float>(j ,k)= temp.at<float>(0, 0);
result.Q.col(k) = result.Q.col(k)-result.R.at<float>(j ,k)*result.Q.col(j);
}
}
return result;
}
template<class T>
void ProcessingCore::matIteration(cv::Mat M, void (*callback)(T Value))
{
for(int i = 0; i < M.rows; i++)
{
const T* Mi = M.ptr<T>(i);
for(int j = 0; j < M.cols; j++)
{
callback(Mi[j]);
}
}
}
cv::Mat ProcessingCore::FFT(cv::Mat image)
{
if (image.channels() > 1)
{
vector<cv::Mat> ch;
cv::split(image, ch);
image = ch[0];
}
cv::Size dftSize;
dftSize.width = cv::getOptimalDFTSize(image.cols);
dftSize.height = cv::getOptimalDFTSize(image.rows);
cv::Mat temp=cv::Mat::zeros(dftSize, CV_64FC1), result;
cv::Mat roi_temp(temp, cv::Rect(0, 0, image.cols, image.rows));
image.convertTo(roi_temp, CV_64F);
cv::dft(temp, result, cv::DFT_COMPLEX_OUTPUT );
return result;
}
cv::Mat ProcessingCore::IFT(cv::Mat transform, int origType)
{
cv::Mat iftm;
cv::dft(transform, iftm, cv::DFT_INVERSE + cv::DFT_SCALE + cv::DFT_REAL_OUTPUT );
iftm.convertTo(iftm, origType);
return iftm;
}
//void Core::getBasicCharacteristics(Mat image)
//{
/*int maxValue;
switch (image.depth())
{
case CV_16U:
maxValue=65535;
break;
case CV_8U:
maxValue = 255;
break;
}*/
/*Mat p = Mat(1, maxValue+1, image.type());
MatConstIterator_<short> it, it_end = image.end<short>(); int count=0;
for(int i=0; i<maxValue; ++i)
{
it = image.begin<short>();
for (; it != it_end; ++it)
{
if(*it == i)
{
++count;
}
}
p.push_back(count);
count=0;
}
p= p*(1/cv::sum(p)[0]);
*p*(cv::log() p))/log(2));
return result;
}
bool MainImagesList::push_back(string Key)
{
bool exist=false;
for (int i = 0; i<_Images.size(); i++)
{
if(_Images[i].Key == Key)
{
exist=true;
break;
}
}
if (!exist)
{
Mat Image = imread(Key);
MainImagesListItem item = MainImagesListItem(Image, Key);
this->_Images.push_back(item);
}
return !exist;*/
// }
cv::Scalar ProcessingCore::getMSSIM( const cv::Mat& i1, const cv::Mat& i2, cv::Mat& ssim_map)
{
const double C1 = 6.5025, C2 = 58.5225;
/***************************** INITS **********************************/
int d = CV_32F;
cv::Mat I1, I2;
i1.convertTo(I1, d); // cannot calculate on one byte large values
i2.convertTo(I2, d);
cv::Mat I2_2 = I2.mul(I2); // I2^2
cv::Mat I1_2 = I1.mul(I1); // I1^2
cv::Mat I1_I2 = I1.mul(I2); // I1 * I2
/***********************PRELIMINARY COMPUTING ******************************/
cv::Mat mu1, mu2; //
cv::GaussianBlur(I1, mu1, cv::Size(11, 11), 1.5);
cv::GaussianBlur(I2, mu2, cv::Size(11, 11), 1.5);
cv::Mat mu1_2 = mu1.mul(mu1);
cv::Mat mu2_2 = mu2.mul(mu2);
cv::Mat mu1_mu2 = mu1.mul(mu2);
cv::Mat sigma1_2, sigma2_2, sigma12;
cv::GaussianBlur(I1_2, sigma1_2, cv::Size(11, 11), 1.5);
sigma1_2 -= mu1_2;
cv::GaussianBlur(I2_2, sigma2_2, cv::Size(11, 11), 1.5);
sigma2_2 -= mu2_2;
GaussianBlur(I1_I2, sigma12, cv::Size(11, 11), 1.5);
sigma12 -= mu1_mu2;
///////////////////////////////// FORMULA ////////////////////////////////
cv::Mat t1, t2, t3;
t1 = 2 * mu1_mu2 + C1;
t2 = 2 * sigma12 + C2;
t3 = t1.mul(t2); // t3 = ((2*mu1_mu2 + C1).*(2*sigma12 + C2))
t1 = mu1_2 + mu2_2 + C1;
t2 = sigma1_2 + sigma2_2 + C2;
t1 = t1.mul(t2); // t1 =((mu1_2 + mu2_2 + C1).*(sigma1_2 + sigma2_2 + C2))
divide(t3, t1, ssim_map); // ssim_map = t3./t1;
cv::Scalar mssim = mean( ssim_map ); // mssim = average of ssim map
ssim_map = ssim_map*255;
ssim_map.convertTo(ssim_map, CV_8U);
return mssim;
}
QIcon ProcessingCore::convertToQIcon(cv::Mat image, int width, int height)
{
QImage* qimage = convertToQImage(image);
QPixmap pixmap = QPixmap::fromImage(*qimage);
QIcon ic = QIcon(pixmap.scaled(width, height));
return ic;
}
//static double getSSIM(IplImage* src1, IplImage* src2, IplImage* mask,
// const double K1,
// const double K2,
// const int L,
// const int downsamplewidth,
// const int gaussian_window,
// const double gaussian_sigma,
// IplImage* dest)
//{
// // default settings
// const double C1 = (K1 * L) * (K1 * L); //6.5025 C1 = (K(1)*L)^2;
// const double C2 = (K2 * L) * (K2 * L); //58.5225 C2 = (K(2)*L)^2;
// //�@get width and height
// int x=src1->width, y=src1->height;
// //�@distance (down sampling) setting
// int rate_downsampling = std::max(1, int((std::min(x,y) / downsamplewidth) + 0.5));
// int nChan=1, d=IPL_DEPTH_32F;
// //�@size before down sampling
// CvSize size_L = cvSize(x, y);
// //�@size after down sampling
// CvSize size = cvSize(x / rate_downsampling, y / rate_downsampling);
// //�@image allocation
// cv::Ptr<IplImage> img1 = cvCreateImage( size, d, nChan);
// cv::Ptr<IplImage> img2 = cvCreateImage( size, d, nChan);
// //�@convert 8 bit to 32 bit float value
// cv::Ptr<IplImage> img1_L = cvCreateImage( size_L, d, nChan);
// cv::Ptr<IplImage> img2_L = cvCreateImage( size_L, d, nChan);
// cvConvert(src1, img1_L);
// cvConvert(src2, img2_L);
// //�@down sampling
// cvResize(img1_L, img1);
// cvResize(img2_L, img2);
// //�@buffer alocation
// cv::Ptr<IplImage> img1_sq = cvCreateImage( size, d, nChan);
// cv::Ptr<IplImage> img2_sq = cvCreateImage( size, d, nChan);
// cv::Ptr<IplImage> img1_img2 = cvCreateImage( size, d, nChan);
// //�@pow and mul
// cvPow( img1, img1_sq, 2 );
// cvPow( img2, img2_sq, 2 );
// cvMul( img1, img2, img1_img2, 1 );
// //�@get sigma
// cv::Ptr<IplImage> mu1 = cvCreateImage( size, d, nChan);
// cv::Ptr<IplImage> mu2 = cvCreateImage( size, d, nChan);
// cv::Ptr<IplImage> mu1_sq = cvCreateImage( size, d, nChan);
// cv::Ptr<IplImage> mu2_sq = cvCreateImage( size, d, nChan);
// cv::Ptr<IplImage> mu1_mu2 = cvCreateImage( size, d, nChan);
// cv::Ptr<IplImage> sigma1_sq = cvCreateImage( size, d, nChan);
// cv::Ptr<IplImage> sigma2_sq = cvCreateImage( size, d, nChan);
// cv::Ptr<IplImage> sigma12 = cvCreateImage( size, d, nChan);
// //�@allocate buffer
// cv::Ptr<IplImage> temp1 = cvCreateImage( size, d, nChan);
// cv::Ptr<IplImage> temp2 = cvCreateImage( size, d, nChan);
// cv::Ptr<IplImage> temp3 = cvCreateImage( size, d, nChan);
// //ssim map
// cv::Ptr<IplImage> ssim_map = cvCreateImage( size, d, nChan);
// //////////////////////////////////////////////////////////////////////////
// // // PRELIMINARY COMPUTING
// //�@gaussian smooth
// cvSmooth( img1, mu1, CV_GAUSSIAN, gaussian_window, gaussian_window, gaussian_sigma );
// cvSmooth( img2, mu2, CV_GAUSSIAN, gaussian_window, gaussian_window, gaussian_sigma );
// //�@get mu
// cvPow( mu1, mu1_sq, 2 );
// cvPow( mu2, mu2_sq, 2 );
// cvMul( mu1, mu2, mu1_mu2, 1 );
// //�@calc sigma
// cvSmooth( img1_sq, sigma1_sq, CV_GAUSSIAN, gaussian_window, gaussian_window, gaussian_sigma );
// cvAddWeighted( sigma1_sq, 1, mu1_sq, -1, 0, sigma1_sq );
// cvSmooth( img2_sq, sigma2_sq, CV_GAUSSIAN, gaussian_window, gaussian_window, gaussian_sigma);
// cvAddWeighted( sigma2_sq, 1, mu2_sq, -1, 0, sigma2_sq );
// cvSmooth( img1_img2, sigma12, CV_GAUSSIAN, gaussian_window, gaussian_window, gaussian_sigma );
// cvAddWeighted( sigma12, 1, mu1_mu2, -1, 0, sigma12 );
// //////////////////////////////////////////////////////////////////////////
// // FORMULA
// // (2*mu1_mu2 + C1)
// cvScale( mu1_mu2, temp1, 2 );
// cvAddS( temp1, cvScalarAll(C1), temp1 );
// // (2*sigma12 + C2)
// cvScale( sigma12, temp2, 2 );
// cvAddS( temp2, cvScalarAll(C2), temp2 );
// // ((2*mu1_mu2 + C1).*(2*sigma12 + C2))
// cvMul( temp1, temp2, temp3, 1 );
// // (mu1_sq + mu2_sq + C1)
// cvAdd( mu1_sq, mu2_sq, temp1 );
// cvAddS( temp1, cvScalarAll(C1), temp1 );
// // (sigma1_sq + sigma2_sq + C2)
// cvAdd( sigma1_sq, sigma2_sq, temp2 );
// cvAddS( temp2, cvScalarAll(C2), temp2 );
// // ((mu1_sq + mu2_sq + C1).*(sigma1_sq + sigma2_sq + C2))
// cvMul( temp1, temp2, temp1, 1 );
// // ((2*mu1_mu2 + C1).*(2*sigma12 + C2))./((mu1_sq + mu2_sq + C1).*(sigma1_sq + sigma2_sq + C2))
// cvDiv( temp3, temp1, ssim_map, 1 );
// cv::Ptr<IplImage> stemp = cvCreateImage( size, IPL_DEPTH_8U, 1);
// cv::Ptr<IplImage> mask2 = cvCreateImage( size, IPL_DEPTH_8U, 1);
// cvConvertScale(ssim_map, stemp, 255.0, 0.0);
// cvResize(stemp,dest);
// cvResize(mask,mask2);
// CvScalar index_scalar = cvAvg( ssim_map, mask2 );
// // through observation, there is approximately
// // 1% error max with the original matlab program
// return index_scalar.val[0];
//}
//double xcvCalcSSIM(IplImage* src, IplImage* dest, int channel, int method, IplImage* _mask,
// const double K1,
// const double K2,
// const int L,
// const int downsamplewidth,
// const int gaussian_window,
// const double gaussian_sigma,
// IplImage* ssim_map
// )
//{
// cv::IplImage* mask;
// cv::IplImage* __mask=cv::cvCreateImage(cv::cvGetSize(src),8,1);
// cv::IplImage* smap=cvCreateImage(cv::cvGetSize(src),8,1);
// cvSet(__mask,cvScalarAll(255));
// if(_mask==NULL)mask=__mask;
// else mask=_mask;
// IplImage* ssrc;
// IplImage* sdest;
// if(src->nChannels==1)
// {
// ssrc=cvCreateImage(cvGetSize(src),8,3);
// sdest=cvCreateImage(cvGetSize(src),8,3);
// cvCvtColor(src,ssrc,CV_GRAY2BGR);
// cvCvtColor(dest,sdest,CV_GRAY2BGR);
// }
// else
// {
// ssrc = cvCloneImage(src);
// sdest = cvCloneImage(dest);
// cvCvtColor(dest,sdest,method);
// cvCvtColor(src,ssrc,method);
// }
// IplImage* gray[4];
// IplImage* sgray[4];
// for(int i=0;i<4;i++)
// {
// gray[i] = cvCreateImage(cvGetSize(src),8,1);
// sgray[i] = cvCreateImage(cvGetSize(src),8,1);
// }
// cvSplit(sdest,gray[0],gray[1],gray[2],NULL);
// cvSplit(ssrc,sgray[0],sgray[1],sgray[2],NULL);
// double sn=0.0;
// if(channel==ALLCHANNEL)
// {
// for(int i=0;i<src->nChannels;i++)
// {
// sn+=getSSIM(sgray[i],gray[i],mask,K1,K2,L,downsamplewidth,gaussian_window,gaussian_sigma,smap);
// }
// sn/=(double)src->nChannels;
// }
// else
// {
// sn = getSSIM(sgray[channel],gray[channel],mask,K1,K2,L,downsamplewidth,gaussian_window,gaussian_sigma,smap);
// }
// for(int i=0;i<4;i++)
// {
// cvReleaseImage(&gray[i]);
// cvReleaseImage(&sgray[i]);
// }
// if(ssim_map!=NULL)cvCopy(smap,ssim_map);
// cvReleaseImage(&smap);
// cvReleaseImage(&__mask);
// cvReleaseImage(&ssrc);
// cvReleaseImage(&sdest);
// return sn;
//}
//double xcvCalcDSSIM(IplImage* src, IplImage* dest, int channel, int method, IplImage* _mask,
// const double K1,
// const double K2,
// const int L,
// const int downsamplewidth,
// const int gaussian_window,
// const double gaussian_sigma,
// IplImage* ssim_map
// )
//{
// double ret = xcvCalcSSIM(src, dest, channel, method, _mask, K1,K2,L,downsamplewidth,gaussian_window,gaussian_sigma,ssim_map);
// if(ret==0)ret=-1.0;
// else ret =(1.0 / ret) - 1.0;
// return ret;
//}
//double xcvCalcSSIMBB(IplImage* src, IplImage* dest, int channel, int method, int boundx,int boundy,const double K1, const double K2, const int L, const int downsamplewidth, const int gaussian_window, const double gaussian_sigma, IplImage* ssim_map)
//{
// IplImage* mask = cvCreateImage(cvGetSize(src),8,1);
// cvZero(mask);
// cvRectangle(mask,cvPoint(boundx,boundy),cvPoint(src->width-boundx,src->height-boundy),cvScalarAll(255),CV_FILLED);
// double ret = xcvCalcSSIM(src,dest,channel,method,mask,K1,K2,L,downsamplewidth,gaussian_window,gaussian_sigma,ssim_map);
// cvReleaseImage(&mask);
// return ret;
//}
//double xcvCalcDSSIMBB(IplImage* src, IplImage* dest, int channel, int method, int boundx,int boundy,const double K1, const double K2, const int L, const int downsamplewidth, const int gaussian_window, const double gaussian_sigma, IplImage* ssim_map)
//{
// IplImage* mask = cvCreateImage(cvGetSize(src),8,1);
// cvZero(mask);
// cvRectangle(mask,cvPoint(boundx,boundy),cvPoint(src->width-boundx,src->height-boundy),cvScalarAll(255),CV_FILLED);
// double ret = xcvCalcSSIM(src,dest,channel,method,mask,K1,K2,L,downsamplewidth,gaussian_window,gaussian_sigma,ssim_map);
// cvReleaseImage(&mask);
// if(ret==0)ret=-1.0;
// else ret = (1.0 / ret) - 1.0;
// return ret;
//}
double ProcessingCore::calcSSIM(cv::Mat& src1, cv::Mat& src2, int channel, int method, const double K1, const double K2, const int L, const int downsamplewidth, const int gaussian_window, const double gaussian_sigma)
{
cv::Mat mask; cv::Mat map;
return 0;
}
double ProcessingCore::calcSSIM(cv::Mat& src1, cv::Mat& src2, cv::Mat& mask, cv::Mat& ssim_map, int channel, int method, const double K1, const double K2, const int L, const int downsamplewidth, const int gaussian_window, const double gaussian_sigma)
{
return 0;
// if(ssim_map.empty())ssim_map.create(src1.size(),CV_8U);
// IplImage Src1 = IplImage(src1);
// IplImage Src2 = IplImage(src2);
// IplImage Ssim_map = IplImage(ssim_map);
// IplImage* iplSrc1 = &Src1;
// IplImage* iplSrc2 = &Src2;
// IplImage* iplSsim_map = &Ssim_map;
// if(mask.empty())
// {
// xcvCalcSSIM(iplSrc1,iplSrc2,channel,method,NULL,K1,K2,L,downsamplewidth,gaussian_window,gaussian_sigma, iplSsim_map);
// return xcvCalcSSIM(iplSrc1,iplSrc2,channel,method,NULL,K1,K2,L,downsamplewidth,gaussian_window,gaussian_sigma, iplSsim_map);
// }
// else
// {
// IplImage Mask = IplImage(mask);
// IplImage* iplMask = &Mask;
// return xcvCalcSSIM(iplSrc1,iplSrc2,channel,method,iplMask,K1,K2,L,downsamplewidth,gaussian_window,gaussian_sigma, iplSsim_map);
// }
}
//double ProcessingCore::calcSSIMBB(cv::Mat& src1, cv::Mat& src2, int channel, int method, int boundx, int boundy, const double K1, const double K2, const int L, const int downsamplewidth, const int gaussian_window, const double gaussian_sigma)
//{
// Mat ssim_map;
// return calcSSIMBB(src1, src2, ssim_map, channel, method, boundx, boundy, K1, K2, L, downsamplewidth, gaussian_window, gaussian_sigma);
//}
//double ProcessingCore::calcSSIMBB(cv::Mat& src1, cv::Mat& src2, cv::Mat& ssim_map, int channel, int method, int boundx, int boundy, const double K1, const double K2, const int L, const int downsamplewidth, const int gaussian_window, const double gaussian_sigma)
//{
// IplImage Src1 = IplImage(src1);
// IplImage Src2 = IplImage(src2);
// IplImage Ssim_map = IplImage(ssim_map);
// IplImage* iplSrc1 = &Src1;
// IplImage* iplSrc2 = &Src2;
// IplImage* iplSsim_map = &Ssim_map;
// if(ssim_map.empty())ssim_map.create(src1.size(),CV_8U);
// return xcvCalcSSIMBB(iplSrc1,iplSrc2,channel,method,boundx,boundy,K1,K2,L,downsamplewidth,gaussian_window,gaussian_sigma, iplSsim_map);
//}
//double calcDSSIM(cv::Mat& src1, cv::Mat& src2, int channel, int method, cv::Mat& mask, const double K1, const double K2, const int L, const int downsamplewidth, const int gaussian_window, const double gaussian_sigma, cv::Mat& ssim_map)
//{
// if(ssim_map.empty())ssim_map.create(src1.size(),CV_8U);
// IplImage* iplSrc1 = &IplImage(src1);
// IplImage* iplSrc2 = &IplImage(src2);
// IplImage* iplSsim_map = &IplImage(ssim_map);
// if(mask.empty())
// {
// xcvCalcSSIM(iplSrc1,iplSrc2,channel,method,NULL,K1,K2,L,downsamplewidth,gaussian_window,gaussian_sigma, iplSsim_map);
// return xcvCalcDSSIM(iplSrc1,iplSrc2,channel,method,NULL,K1,K2,L,downsamplewidth,gaussian_window,gaussian_sigma, iplSsim_map);
// }
// else
// {
// IplImage* iplMask = &IplImage(mask);
// return xcvCalcDSSIM(iplSrc1,iplSrc2,channel,method,iplMask,K1,K2,L,downsamplewidth,gaussian_window,gaussian_sigma, iplSsim_map);
// }
//}
//double calcDSSIMBB(cv::Mat& src1, cv::Mat& src2, int channel, int method, int boundx, int boundy, const double K1, const double K2, const int L, const int downsamplewidth, const int gaussian_window, const double gaussian_sigma, cv::Mat& ssim_map)
//{
// if(ssim_map.empty())ssim_map.create(src1.size(),CV_8U);
// IplImage* iplSrc1 = &IplImage(src1);
// IplImage* iplSrc2 = &IplImage(src2);
// IplImage* iplSsim_map = &IplImage(ssim_map);
// return xcvCalcDSSIMBB(iplSrc1,iplSrc2,channel,method,boundx,boundy,K1,K2,L,downsamplewidth,gaussian_window,gaussian_sigma, iplSsim_map);
//}
double ProcessingCore::meanDiff(cv::Mat img1, cv::Mat img2)
{
cv::Mat diff;
cv::absdiff(img1, img2, diff);
cv::Scalar result = cv::mean(diff);
return result.val[0];
}
double ProcessingCore::normCorrelation(cv::Mat img1, cv::Mat img2)
{
cv::Mat img1pow2, mult;
cv::pow(img1, 2.0, img1pow2);
cv::multiply(img1, img2, mult);
cv::Scalar result = cv::sum(mult)/cv::sum(img1pow2);
return result.val[0];
}
double ProcessingCore::correlationQuality(cv::Mat img1, cv::Mat img2)
{
cv::Mat mult;
cv::multiply(img1, img2, mult);
cv::Scalar result = cv::sum(mult)/cv::sum(img1);
return result.val[0];
}
double ProcessingCore::maxDiff(cv::Mat img1, cv::Mat img2)
{
cv::Mat diff;
cv::absdiff(img1, img2, diff);
double max;
cv::minMaxLoc(diff, NULL, &max);
return max;
}
double ProcessingCore::imageFidelity(cv::Mat img1, cv::Mat img2)
{
cv::Mat img1pow2, diff;
cv::pow(img1, 2.0, img1pow2);
cv::absdiff(img1, img2, diff);
cv::pow(diff, 2.0, diff);
cv::Scalar value = cv::sum(diff)/cv::sum(img1pow2);
return 1.0-value.val[0];
}
template<class T>
cv::Mat ProcessingCore::getO(cv::Mat img)
{
cv::Mat o = cv::Mat::zeros(img.rows, img.cols, CV_32F);
for(int i = 0; i < img.rows; i++)
{
for(int j = 0; j < img.cols; j++)
{
float value = -4*img.at<T>(i, j);
if(i)
{
value+=img.at<T>(i-1, j);
}
if(j)
{
value+=img.at<T>(i, j-1);
}
if(i != img.rows-1)
{
value+=img.at<T>(i+1, j);
}
if(j != img.cols-1)
{
value+=img.at<T>(i, j+1);
}
o.at<float>(i, j) = value;
}
}
return o;
}
double ProcessingCore::laplMeanSqError(cv::Mat img1, cv::Mat img2)
{
cv::Mat oImg1,oImg2;
switch(img1.depth())
{
case CV_8U:
oImg1 = ProcessingCore::getO<unsigned char>(img1);
break;
case CV_16U:
oImg1 = ProcessingCore::getO<unsigned int>(img1);
break;
case CV_32F:
oImg1 = ProcessingCore::getO<float>(img1);
case CV_64F:
oImg1 = ProcessingCore::getO<double>(img1);
break;
}
switch(img2.depth())
{
case CV_8U:
oImg2 = ProcessingCore::getO<unsigned char>(img2);
break;
case CV_16U:
oImg2 = ProcessingCore::getO<unsigned int>(img2);
break;
case CV_32F:
oImg2 = ProcessingCore::getO<float>(img2);
case CV_64F:
oImg2 = ProcessingCore::getO<double>(img2);
break;
}
cv::Mat oImg1Pow;
pow(oImg1, 2.0, oImg1Pow);
cv::Scalar value = cv::sum(oImg1-oImg2)/cv::sum(oImg1Pow);
return value.val[0];
}
double ProcessingCore::meanSqError(cv::Mat img1, cv::Mat img2)
{
cv::Mat pow2;
cv::pow(img1-img2, 2.0, pow2);
cv::Scalar result = cv::mean(pow2);
return result.val[0];
}
double ProcessingCore::peakMeanSqError(cv::Mat img1, cv::Mat img2)
{
cv::Mat pow2;
cv::pow(img1-img2, 2.0, pow2);
double max;
cv::minMaxLoc(img1, NULL, &max);
max = pow(max, 2.0);
cv::Scalar result = cv::mean(pow2);
;
return result.val[0]/pow(max, 2.0);
}
double ProcessingCore::normalizedAbsoluteError(cv::Mat img1, cv::Mat img2)
{
cv::Scalar result = cv::sum(img1-img2)/cv::sum(img1);
return result.val[0];
}
double ProcessingCore::normalizedMeanSquareError(cv::Mat img1, cv::Mat img2)
{
cv::Mat img1pow2, diff;
cv::pow(img1, 2.0, img1pow2);
cv::absdiff(img1, img2, diff);
cv::pow(diff, 2.0, diff);
cv::Scalar value = cv::sum(diff)/cv::sum(img1pow2);
return value.val[0];
}
double ProcessingCore::snr(cv::Mat img1, cv::Mat img2)
{
cv::Mat img1pow2;
cv::pow(img1, 2.0, img1pow2);
cv::Mat img1img2pow2;
cv::pow(img1-img2, 2.0, img1img2pow2);
cv::Scalar result = cv::sum(img1pow2)/cv::sum(img1img2pow2);
return 10.0*std::log10(result.val[0]);
}
double ProcessingCore::psnr(cv::Mat img1, cv::Mat img2)
{
cv::Mat pow2 = img1-img2;
cv::pow(pow2, 2.0, pow2);
cv::Scalar mean = cv::mean(pow2);
double max;
cv::minMaxLoc(img1, NULL, &max);
return 10.0*std::log10(std::pow(max, 2.0)/mean.val[0]);
}