今回はC++でNon-local Means Filterを自作して画像のノイズを除去する方法について記載する。
画像のノイズ除去を実施するときにNon-local Means Filterを使いたいことがある。
PythonのOpenCVでは「cv2.fastNlMeansDenoisingColored()」を使うことができるが、C++版のOpenCV1.0ではこれに該当する関数がない。
よって、このような場合は「Non-local Means Filter」を自作する必要がある。
今回は以下のサイトにその内容があったので備忘録として残しておく。
Non-local Means Filterによるデノイジング
結果としてVisualStudioを起動して以下の内容を実装するとC++でNon-local Means Filterを実行することができた。
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 |
#define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #include <iostream> #include <cmath> #include <opencv2/opencv.hpp> using namespace std; using namespace cv; string win_src = "src"; string win_dst = "dst"; string win_msk = "msk"; // additional functions///////////////////////////////////// void addNoiseSoltPepperMono(Mat& src, Mat& dest, double per) { cv::RNG rng; #pragma omp parallel for for (int j = 0; j < src.rows; j++) { uchar* s = src.ptr(j); uchar* d = dest.ptr(j); for (int i = 0; i < src.cols; i++) { double a1 = rng.uniform((double)0, (double)1); if (a1 > per) d[i] = s[i]; else { double a2 = rng.uniform((double)0, (double)1); if (a2 > 0.5)d[i] = 0; else d[i] = 255; } } } } void addNoiseMono(Mat& src, Mat& dest, double sigma) { Mat s; src.convertTo(s, CV_16S); Mat n(s.size(), CV_16S); randn(n, 0, sigma); Mat temp = s + n; temp.convertTo(dest, CV_8U); } void addNoise(Mat&src, Mat& dest, double sigma, double sprate = 0.0) { if (src.channels() == 1) { addNoiseMono(src, dest, sigma); if (sprate != 0)addNoiseSoltPepperMono(dest, dest, sprate); return; } else { vector<Mat> s; vector<Mat> d(src.channels()); split(src, s); for (int i = 0; i < src.channels(); i++) { addNoiseMono(s[i], d[i], sigma); if (sprate != 0)addNoiseSoltPepperMono(d[i], d[i], sprate); } cv::merge(d, dest); } } static double getPSNR(Mat& src, Mat& dest) { int i, j; double sse, mse, psnr; sse = 0.0; for (j = 0; j < src.rows; j++) { uchar* d = dest.ptr(j); uchar* s = src.ptr(j); for (i = 0; i < src.cols; i++) { sse += ((d[i] - s[i])*(d[i] - s[i])); } } if (sse == 0.0) { return 0; } else { mse = sse / (double)(src.cols*src.rows); psnr = 10.0*log10((255 * 255) / mse); return psnr; } } double calcPSNR(Mat& src, Mat& dest) { Mat ssrc; Mat ddest; if (src.channels() == 1) { src.copyTo(ssrc); dest.copyTo(ddest); } else { //cvtColor(src, ssrc, CV_BGR2YUV); //cvtColor(dest, ddest, CV_BGR2YUV); cvtColor(src, ssrc, COLOR_YUV2BGR_NV12); cvtColor(dest, ddest, COLOR_YUV2BGR_NV12); } double sn = getPSNR(ssrc, ddest); return sn; } //////////////////////////////////////////////////////////////////////////////// //main implementaion void nonlocalMeansFilter(Mat& src, Mat& dest, int templeteWindowSize, int searchWindowSize, double h, double sigma = 0.0) { if (templeteWindowSize > searchWindowSize) { cout << "searchWindowSize should be larger than templeteWindowSize" << endl; return; } if (dest.empty())dest = Mat::zeros(src.size(), src.type()); const int tr = templeteWindowSize >> 1; const int sr = searchWindowSize >> 1; const int bb = sr + tr; const int D = searchWindowSize * searchWindowSize; const int H = D / 2 + 1; const double div = 1.0 / (double)D;//search area div const int tD = templeteWindowSize * templeteWindowSize; const double tdiv = 1.0 / (double)(tD);//templete square div //create large size image for bounding box; Mat im; copyMakeBorder(src, im, bb, bb, bb, bb, cv::BORDER_DEFAULT); //weight computation; vector<double> weight(256 * 256 * src.channels()); double* w = &weight[0]; const double gauss_sd = (sigma == 0.0) ? h : sigma; double gauss_color_coeff = -(1.0 / (double)(src.channels()))*(1.0 / (h*h)); int emax; for (int i = 0; i < 256 * 256 * src.channels(); i++) { double v = std::exp(max(i - 2.0*gauss_sd*gauss_sd, 0.0)*gauss_color_coeff); w[i] = v; if (v < 0.001) { emax = i; break; } } for (int i = emax; i < 256 * 256 * src.channels(); i++)w[i] = 0.0; if (src.channels() == 3) { const int cstep = im.step - templeteWindowSize * 3; const int csstep = im.step - searchWindowSize * 3; #pragma omp parallel for for (int j = 0; j < src.rows; j++) { uchar* d = dest.ptr(j); int* ww = new int[D]; double* nw = new double[D]; for (int i = 0; i < src.cols; i++) { double tweight = 0.0; //search loop uchar* tprt = im.data + im.step*(sr + j) + 3 * (sr + i); uchar* sptr2 = im.data + im.step*j + 3 * i; for (int l = searchWindowSize, count = D - 1; l--;) { uchar* sptr = sptr2 + im.step*(l); for (int k = searchWindowSize; k--;) { //templete loop int e = 0; uchar* t = tprt; uchar* s = sptr + 3 * k; for (int n = templeteWindowSize; n--;) { for (int m = templeteWindowSize; m--;) { // computing color L2 norm e += (s[0] - t[0])*(s[0] - t[0]) + (s[1] - t[1])*(s[1] - t[1]) + (s[2] - t[2])*(s[2] - t[2]);//L2 norm s += 3, t += 3; } t += cstep; s += cstep; } const int ediv = e * tdiv; ww[count--] = ediv; //get weighted Euclidean distance tweight += w[ediv]; } } //weight normalization if (tweight == 0.0) { for (int z = 0; z < D; z++) nw[z] = 0; nw[H] = 1; } else { double itweight = 1.0 / (double)tweight; for (int z = 0; z < D; z++) nw[z] = w[ww[z]] * itweight; } double r = 0.0, g = 0.0, b = 0.0; uchar* s = im.ptr(j + tr); s += 3 * (tr + i); for (int l = searchWindowSize, count = 0; l--;) { for (int k = searchWindowSize; k--;) { r += s[0] * nw[count]; g += s[1] * nw[count]; b += s[2] * nw[count++]; s += 3; } s += csstep; } d[0] = saturate_cast<uchar>(r); d[1] = saturate_cast<uchar>(g); d[2] = saturate_cast<uchar>(b); d += 3; }//i delete[] ww; delete[] nw; }//j } else if (src.channels() == 1) { const int cstep = im.step - templeteWindowSize; const int csstep = im.step - searchWindowSize; #pragma omp parallel for for (int j = 0; j < src.rows; j++) { uchar* d = dest.ptr(j); int* ww = new int[D]; double* nw = new double[D]; for (int i = 0; i < src.cols; i++) { double tweight = 0.0; //search loop uchar* tprt = im.data + im.step*(sr + j) + (sr + i); uchar* sptr2 = im.data + im.step*j + i; for (int l = searchWindowSize, count = D - 1; l--;) { uchar* sptr = sptr2 + im.step*(l); for (int k = searchWindowSize; k--;) { //templete loop int e = 0; uchar* t = tprt; uchar* s = sptr + k; for (int n = templeteWindowSize; n--;) { for (int m = templeteWindowSize; m--;) { // computing color L2 norm e += (*s - *t)*(*s - *t); s++, t++; } t += cstep; s += cstep; } const int ediv = e * tdiv; ww[count--] = ediv; //get weighted Euclidean distance tweight += w[ediv]; } } //weight normalization if (tweight == 0.0) { for (int z = 0; z < D; z++) nw[z] = 0; nw[H] = 1; } else { double itweight = 1.0 / (double)tweight; for (int z = 0; z < D; z++) nw[z] = w[ww[z]] * itweight; } double v = 0.0; uchar* s = im.ptr(j + tr); s += (tr + i); for (int l = searchWindowSize, count = 0; l--;) { for (int k = searchWindowSize; k--;) { v += *(s++)*nw[count++]; } s += csstep; } *(d++) = saturate_cast<uchar>(v); }//i delete[] ww; delete[] nw; }//j } } int main() { string file_src = "src.jpg"; string file_dst = "dst.jpg"; Mat img_src = imread(file_src, 1); Mat img_dst; if (!img_src.data) { cout << "error" << endl; return -1; } namedWindow(win_src, WINDOW_AUTOSIZE); namedWindow(win_dst, WINDOW_AUTOSIZE); //Non-local Means Filterによるノイズ除去 const double noise_sigma = 15.0; nonlocalMeansFilter(img_src, img_dst, 3, 7, noise_sigma, noise_sigma); imshow(win_src, img_src); imshow(win_dst, img_dst); imwrite(file_dst, img_dst); waitKey(0); return 0; } |
コメントを残す