opencv GPU和CPUX实现直方图均衡差异

opencv gpu和cpu实现直方图均衡会出现不同,版本3.3.1,代码如下:

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
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;

void testCPU()
{
//cpu
cv::Mat src = imread("/home/ubuntu/Pictures/2.png");
//cv::resize(src, src, cv::Size(0, 0), 0.5, 0.5);
vector<Mat> channels;
split(src, channels);
Mat B,G,R;
//#pragma omp parallel sections
{
//#pragma omp section
{
equalizeHist( channels[0], B );
//GaussianBlur(B,B,Size(3, 3), 2.0);
//B = (B+ channels[0]) / 2;
//addWeighted(B, 0.5, channels[0], 0.5, 0, B);
}
//#pragma omp section
{
equalizeHist( channels[1], G );
//GaussianBlur(G,G,Size(3, 3), 2.0);
//G = (G+ channels[1]) / 2;
//addWeighted(G, 0.5, channels[1], 0.5, 0, G);
}
//#pragma omp section
{
equalizeHist( channels[2], R );
//GaussianBlur(R,R,Size(3, 3), 2.0);
//R = (R+ channels[2]) / 2;
//addWeighted(R, 0.5, channels[2], 0.5, 0, R);
}
}

vector<Mat> combined;
combined.push_back(B);
combined.push_back(G);
combined.push_back(R);
Mat sample_single;
merge(combined, sample_single);

imwrite("cpu.jpg", sample_single);
imshow("cpu", sample_single);
//waitKey(0);
}

void testGPU()
{
//gpu
cv::Mat src = imread("/home/ubuntu/Pictures/2.png");
//cv::resize(src, src, cv::Size(0, 0), 0.5, 0.5);
cv::cuda::GpuMat gpu_src;// = cv::cuda::GpuMat(src.height, src.width, CV_8UC3, src.data);
gpu_src.upload(src);

std::vector<cv::cuda::GpuMat> channels;
cv::cuda::split(gpu_src, channels);

cv::cuda::GpuMat B, G, R;
cv::cuda::equalizeHist(channels[0], B);
cv::cuda::equalizeHist(channels[1], G);
cv::cuda::equalizeHist(channels[2], R);

//创建高斯滤波器
cv::Ptr<cv::cuda::Filter> gauss = cv::cuda::createGaussianFilter(CV_8UC1, CV_8UC1, Size(3, 3), 2.0);
//高斯滤波
//gauss->apply(B, B);
//gauss->apply(G, G);
//gauss->apply(R, R);

// cv::cuda::addWeighted(B, 0.5, channels[0], 0.5, 0, B);
// cv::cuda::addWeighted(G, 0.5, channels[1], 0.5, 0, G);
// cv::cuda::addWeighted(R, 0.5, channels[2], 0.5, 0, R);
vector<cv::cuda::GpuMat> combined;
combined.push_back(B);
combined.push_back(G);
combined.push_back(R);
cv::cuda::GpuMat gpu_dst;
cv::cuda::merge(combined, gpu_dst);

Mat img;
gpu_dst.download(img);
imwrite("gpu.jpg", img);
imshow("gpu", img);
waitKey(0);

}

int main()
{
testCPU();
testGPU();

return 0;
}

原图

原图

CPU

cpu

GPU

gpu