-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnlmeans.cpp
67 lines (56 loc) · 2.4 KB
/
nlmeans.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
#include "nlmeans.h"
#include <opencv2/imgproc/imgproc.hpp>
#include "image2vectors_single.h"
#include "vectors_nlmeans_single.h"
cv::Mat nlmeans(cv::Mat img,
float h,
int neighborhoodRadius,
int searchWindowRadius,
int blockSize,
int nThreads)
{
assert(CV_32FC1 == img.type());
int borderSize = neighborhoodRadius + searchWindowRadius;
cv::Mat paddedImg;
cv::copyMakeBorder(img,
paddedImg,
borderSize,
borderSize,
borderSize,
borderSize,
cv::BORDER_REFLECT);
cv::Mat outputImg(img.size(), img.type());
int realBlockSize
= blockSize - 2 * (neighborhoodRadius + searchWindowRadius);
for (int y1 = 0; y1 < paddedImg.size().height; y1 += realBlockSize) {
for (int x1 = 0; x1 < paddedImg.size().width; x1 += realBlockSize) {
int x2 = x1 + blockSize;
int y2 = y1 + blockSize;
x2 = cv::max(cv::min(x2, paddedImg.size().width), 1);
y2 = cv::max(cv::min(y2, paddedImg.size().height), 1);
int x3 = x1;
int y3 = y1;
int x4 = cv::min(x1 + realBlockSize, img.size().width);
int y4 = cv::min(y1 + realBlockSize, img.size().height);
if ((x4 > x3) && (y4 > y3)) {
cv::Mat imgBlock = paddedImg(cv::Range(y1, y2),
cv::Range(x1, x2)).clone();
cv::Mat V = image2vectors_single(imgBlock,
neighborhoodRadius,
nThreads);
cv::Mat filteredImgBlock
= vectors_nlmeans_single(imgBlock,
V,
neighborhoodRadius,
searchWindowRadius,
h,
nThreads);
assert(y4 - y3 == filteredImgBlock.size().height);
assert(x4 - x3 == filteredImgBlock.size().width);
filteredImgBlock.copyTo(outputImg(cv::Range(y3, y4),
cv::Range(x3, x4)));
}
}
}
return outputImg;
}