fuzzy_F0_math.cpp 17.1 KB
Newer Older
tucna's avatar
tucna committed
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
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                           License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2015, University of Ostrava, Institute for Research and Applications of Fuzzy Modeling,
// Pavel Vlasanek, all rights reserved. Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of the copyright holders may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

#include "precomp.hpp"

using namespace cv;

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
void ft::FT02D_FL_process(InputArray matrix, const int radius, OutputArray output)
{
    CV_Assert(matrix.channels() == 3);

    int borderPadding = 2 * radius + 1;
    Mat imagePadded;

    copyMakeBorder(matrix, imagePadded, radius, borderPadding, radius, borderPadding, BORDER_CONSTANT, Scalar(0));

    Mat channel[3];
    split(imagePadded, channel);

    uchar *im_r = channel[2].data;
    uchar *im_g = channel[1].data;
    uchar *im_b = channel[0].data;

    int width = imagePadded.cols;
    int height = imagePadded.rows;
    int n_width = width / radius + 1;
    int n_height = height / radius + 1;

    std::vector<uchar> c_r(n_width * n_height);
    std::vector<uchar> c_g(n_width * n_height);
    std::vector<uchar> c_b(n_width * n_height);

    int sum_r, sum_g, sum_b, num, c_wei;
    int c_pos, pos, pos2, wy;
    int cy = 0;
    float num_f;

    std::vector<int> wei(radius + 1);

    for (int i = 0; i <= radius; i++)
    {
        wei[i] = radius - i;
    }

    for (int y = radius; y < height - radius; y += radius)
    {
        c_pos = cy;

        for (int x = radius; x < width - radius; x += radius)
        {
            num = sum_r = sum_g = sum_b = 0;

            for (int y1 = y - radius; y1 <= y + radius; y1++)
            {
                pos = y1 * width;
                wy = wei[abs(y1 - y)];

                for (int x1 = x - radius; x1 <= x + radius; x1++)
                {
                    c_wei = wei[abs(x1 - x)] * wy;
                    pos2 = pos + x1;
                    sum_r += im_r[pos2] * c_wei;
                    sum_g += im_g[pos2] * c_wei;
                    sum_b += im_b[pos2] * c_wei;
                    num += c_wei;
                }
            }

            num_f = 1.0f / (float)num;

            c_r[c_pos] = (uchar)cvRound(sum_r * num_f);
            c_g[c_pos] = (uchar)cvRound(sum_g * num_f);
            c_b[c_pos] = (uchar)cvRound(sum_b * num_f);

            c_pos++;
        }

        cy += n_width;
    }

    int p1, p2, p3, p4, yw, w1, w2, w3, w4, lx, ly, lx1, ly1, pos_iFT;
    float num_iFT;

    int output_height = matrix.rows();
    int output_width = matrix.cols();

    uchar *img_r = new uchar[output_height * output_width];
    uchar *img_g = new uchar[output_height * output_width];
    uchar *img_b = new uchar[output_height * output_width];

    for (int y = 0; y < output_height; y++)
    {
        ly1 = (y % radius);
        ly = radius - ly1;
        yw = y / radius * n_width;
        pos_iFT = y * output_width;

        for (int x = 0; x < output_width; x++)
        {
            lx1 = (x % radius);
            lx = radius - lx1;

            p1 = x / radius + yw;
            p2 = p1 + 1;
            p3 = p1 + n_width;
            p4 = p3 + 1;

            w1 = lx * ly;
            w2 = lx1 * ly;
            w3 = lx * ly1;
            w4 = lx1 * ly1;

            num_iFT = 1.0f / (float)(w1 + w2 + w3 + w4);

            img_r[pos_iFT] = (uchar)((c_r[p1] * w1 + c_r[p2] * w2 + c_r[p3] * w3 + c_r[p4] * w4) * num_iFT);
            img_g[pos_iFT] = (uchar)((c_g[p1] * w1 + c_g[p2] * w2 + c_g[p3] * w3 + c_g[p4] * w4) * num_iFT);
            img_b[pos_iFT] = (uchar)((c_b[p1] * w1 + c_b[p2] * w2 + c_b[p3] * w3 + c_b[p4] * w4) * num_iFT);

            pos_iFT++;
        }
    }

    Mat compR(output_height, output_width, CV_8UC1, img_r);
    Mat compG(output_height, output_width, CV_8UC1, img_g);
    Mat compB(output_height, output_width, CV_8UC1, img_b);

    std::vector<Mat> oComp;

    oComp.push_back(compB);
    oComp.push_back(compG);
    oComp.push_back(compR);

    merge(oComp, output);
}

void ft::FT02D_FL_process_float(InputArray matrix, const int radius, OutputArray output)
{
    CV_Assert(matrix.channels() == 3);

    int borderPadding = 2 * radius + 1;
    Mat imagePadded;

    copyMakeBorder(matrix, imagePadded, radius, borderPadding, radius, borderPadding, BORDER_CONSTANT, Scalar(0));

    Mat channel[3];
    split(imagePadded, channel);

    uchar *im_r = channel[2].data;
    uchar *im_g = channel[1].data;
    uchar *im_b = channel[0].data;

    int width = imagePadded.cols;
    int height = imagePadded.rows;
    int n_width = width / radius + 1;
    int n_height = height / radius + 1;

    std::vector<float> c_r(n_width * n_height);
    std::vector<float> c_g(n_width * n_height);
    std::vector<float> c_b(n_width * n_height);

    int sum_r, sum_g, sum_b, num, c_wei;
    int c_pos, pos, pos2, wy;
    int cy = 0;
    float num_f;

    std::vector<int> wei(radius + 1);

    for (int i = 0; i <= radius; i++)
    {
        wei[i] = radius - i;
    }

    for (int y = radius; y < height - radius; y += radius)
    {
        c_pos = cy;

        for (int x = radius; x < width - radius; x += radius)
        {
            num = sum_r = sum_g = sum_b = 0;

            for (int y1 = y - radius; y1 <= y + radius; y1++)
            {
                pos = y1 * width;
                wy = wei[abs(y1 - y)];

                for (int x1 = x - radius; x1 <= x + radius; x1++)
                {
                    c_wei = wei[abs(x1 - x)] * wy;
                    pos2 = pos + x1;
                    sum_r += im_r[pos2] * c_wei;
                    sum_g += im_g[pos2] * c_wei;
                    sum_b += im_b[pos2] * c_wei;
                    num += c_wei;
                }
            }

            num_f = 1.0f / (float)num;

            c_r[c_pos] = sum_r * num_f;
            c_g[c_pos] = sum_g * num_f;
            c_b[c_pos] = sum_b * num_f;

            c_pos++;
        }

        cy += n_width;
    }

    int p1, p2, p3, p4, yw, w1, w2, w3, w4, lx, ly, lx1, ly1, pos_iFT;
    float num_iFT;

    int output_height = matrix.rows();
    int output_width = matrix.cols();

    float *img_r = new float[output_height * output_width];
    float *img_g = new float[output_height * output_width];
    float *img_b = new float[output_height * output_width];

    for (int y = 0; y < output_height; y++)
    {
        ly1 = (y % radius);
        ly = radius - ly1;
        yw = y / radius * n_width;
        pos_iFT = y * output_width;

        for (int x = 0; x < output_width; x++)
        {
            lx1 = (x % radius);
            lx = radius - lx1;

            p1 = x / radius + yw;
            p2 = p1 + 1;
            p3 = p1 + n_width;
            p4 = p3 + 1;

            w1 = lx * ly;
            w2 = lx1 * ly;
            w3 = lx * ly1;
            w4 = lx1 * ly1;

            num_iFT = 1.0f / (float)(w1 + w2 + w3 + w4);

            img_r[pos_iFT] = (c_r[p1] * w1 + c_r[p2] * w2 + c_r[p3] * w3 + c_r[p4] * w4) * num_iFT;
            img_g[pos_iFT] = (c_g[p1] * w1 + c_g[p2] * w2 + c_g[p3] * w3 + c_g[p4] * w4) * num_iFT;
            img_b[pos_iFT] = (c_b[p1] * w1 + c_b[p2] * w2 + c_b[p3] * w3 + c_b[p4] * w4) * num_iFT;

            pos_iFT++;
        }
    }

    Mat compR(output_height, output_width, CV_32FC1, img_r);
    Mat compG(output_height, output_width, CV_32FC1, img_g);
    Mat compB(output_height, output_width, CV_32FC1, img_b);

    std::vector<Mat> oComp;

    oComp.push_back(compB);
    oComp.push_back(compG);
    oComp.push_back(compR);

    merge(oComp, output);
}

tucna's avatar
tucna committed
302 303
void ft::FT02D_components(InputArray matrix, InputArray kernel, OutputArray components, InputArray mask)
{
304 305 306 307 308 309 310 311 312 313 314 315 316 317
    CV_Assert(matrix.channels() == kernel.channels());

    Mat inputMask;

    if (mask.getMat().empty())
    {
        inputMask = Mat::ones(matrix.size(), CV_8U);
    }
    else
    {
        CV_Assert(mask.channels() == 1);

        inputMask = mask.getMat();
    }
tucna's avatar
tucna committed
318

319 320 321 322
    int radiusX = (kernel.cols() - 1) / 2;
    int radiusY = (kernel.rows() - 1) / 2;
    int An = matrix.cols() / radiusX + 1;
    int Bn = matrix.rows() / radiusY + 1;
tucna's avatar
tucna committed
323 324 325 326

    Mat matrixPadded;
    Mat maskPadded;

327
    copyMakeBorder(matrix, matrixPadded, radiusY, kernel.rows(), radiusX, kernel.cols(), BORDER_CONSTANT, Scalar(0));
328
    copyMakeBorder(inputMask, maskPadded, radiusY, kernel.rows(), radiusX, kernel.cols(), BORDER_CONSTANT, Scalar(0));
329 330

    components.create(Bn, An, CV_MAKETYPE(CV_32F, matrix.channels()));
tucna's avatar
tucna committed
331 332 333 334 335 336 337 338 339

    Mat componentsMat = components.getMat();

    for (int i = 0; i < An; i++)
    {
        for (int o = 0; o < Bn; o++)
        {
            int centerX = (i * radiusX) + radiusX;
            int centerY = (o * radiusY) + radiusY;
340
            Rect area(centerX - radiusX, centerY - radiusY, kernel.cols(), kernel.rows());
tucna's avatar
tucna committed
341 342 343 344 345

            Mat roiImage(matrixPadded, area);
            Mat roiMask(maskPadded, area);
            Mat kernelMasked;

346
            kernel.copyTo(kernelMasked, roiMask);
tucna's avatar
tucna committed
347 348 349 350

            Mat numerator;
            multiply(roiImage, kernelMasked, numerator, 1, CV_32F);

351 352 353 354
            Scalar value;
            divide(sum(numerator), sum(kernelMasked), value, 1, CV_32F);

            componentsMat.row(o).col(i).setTo(value);
tucna's avatar
tucna committed
355 356 357 358 359 360
        }
    }
}

void ft::FT02D_inverseFT(InputArray components, InputArray kernel, OutputArray output, int width, int height)
{
361
    CV_Assert(components.channels() == 1 && kernel.channels() == 1);
tucna's avatar
tucna committed
362

363
    Mat componentsMat = components.getMat();
tucna's avatar
tucna committed
364

365 366 367 368
    int radiusX = (kernel.cols() - 1) / 2;
    int radiusY = (kernel.rows() - 1) / 2;
    int outputWidthPadded = radiusX + width + kernel.cols();
    int outputHeightPadded = radiusY + height + kernel.rows();
tucna's avatar
tucna committed
369 370 371

    output.create(height, width, CV_32F);

372
    Mat outputZeroes(outputHeightPadded, outputWidthPadded, CV_32F, Scalar(0));
tucna's avatar
tucna committed
373 374 375 376 377 378 379

    for (int i = 0; i < componentsMat.cols; i++)
    {
        for (int o = 0; o < componentsMat.rows; o++)
        {
            int centerX = (i * radiusX) + radiusX;
            int centerY = (o * radiusY) + radiusY;
380 381 382 383 384 385
            Rect area(centerX - radiusX, centerY - radiusY, kernel.cols(), kernel.rows());

            float component = componentsMat.at<float>(o, i);

            Mat inverse;
            multiply(kernel, component, inverse, 1, CV_32F);
tucna's avatar
tucna committed
386 387

            Mat roiOutput(outputZeroes, area);
388
            add(roiOutput, inverse, roiOutput);
tucna's avatar
tucna committed
389 390 391 392 393 394
        }
    }

    outputZeroes(Rect(radiusX, radiusY, width, height)).copyTo(output);
}

395
void ft::FT02D_process(InputArray matrix, InputArray kernel, OutputArray output, InputArray mask)
396
{
397
    CV_Assert(matrix.channels() == kernel.channels());
398

399
    Mat inputMask;
400

401 402 403 404 405 406 407 408 409 410
    if (mask.getMat().empty())
    {
        inputMask = Mat::ones(matrix.size(), CV_8U);
    }
    else
    {
        CV_Assert(mask.channels() == 1);

        inputMask = mask.getMat();
    }
tucna's avatar
tucna committed
411

412 413 414 415 416 417
    int radiusX = (kernel.cols() - 1) / 2;
    int radiusY = (kernel.rows() - 1) / 2;
    int An = matrix.cols() / radiusX + 1;
    int Bn = matrix.rows() / radiusY + 1;
    int outputWidthPadded = radiusX + matrix.cols() + kernel.cols();
    int outputHeightPadded = radiusY + matrix.rows() + kernel.rows();
tucna's avatar
tucna committed
418

419
    Mat matrixPadded;
tucna's avatar
tucna committed
420 421
    Mat maskPadded;

422 423 424
    output.create(matrix.size(), CV_MAKETYPE(CV_32F, matrix.channels()));

    Mat outputZeroes(outputHeightPadded, outputWidthPadded, output.type(), Scalar(0));
tucna's avatar
tucna committed
425

426
    copyMakeBorder(matrix, matrixPadded, radiusY, kernel.rows(), radiusX, kernel.cols(), BORDER_CONSTANT, Scalar(0));
427
    copyMakeBorder(inputMask, maskPadded, radiusY, kernel.rows(), radiusX, kernel.cols(), BORDER_CONSTANT, Scalar(0));
tucna's avatar
tucna committed
428 429 430 431 432 433 434

    for (int i = 0; i < An; i++)
    {
        for (int o = 0; o < Bn; o++)
        {
            int centerX = (i * radiusX) + radiusX;
            int centerY = (o * radiusY) + radiusY;
435
            Rect area(centerX - radiusX, centerY - radiusY, kernel.cols(), kernel.rows());
tucna's avatar
tucna committed
436

437
            Mat roiMatrix(matrixPadded, area);
tucna's avatar
tucna committed
438 439 440 441 442 443
            Mat roiMask(maskPadded, area);
            Mat kernelMasked;

            kernel.copyTo(kernelMasked, roiMask);

            Mat numerator;
444
            multiply(roiMatrix, kernelMasked, numerator, 1, CV_32F);
tucna's avatar
tucna committed
445 446 447 448 449 450 451

            Scalar component;
            divide(sum(numerator), sum(kernelMasked), component, 1, CV_32F);

            Mat inverse;
            multiply(kernel, component, inverse, 1, CV_32F);

452
            Mat roiOutput(outputZeroes, area);
tucna's avatar
tucna committed
453 454 455 456
            add(roiOutput, inverse, roiOutput);
        }
    }

457
    outputZeroes(Rect(radiusX, radiusY, matrix.cols(), matrix.rows())).copyTo(output);
tucna's avatar
tucna committed
458 459
}

460
int ft::FT02D_iteration(InputArray matrix, InputArray kernel, OutputArray output, InputArray mask, OutputArray maskOutput, bool firstStop)
tucna's avatar
tucna committed
461
{
462 463 464 465 466 467 468 469
    CV_Assert(matrix.channels() == kernel.channels() && mask.channels() == 1);

    int radiusX = (kernel.cols() - 1) / 2;
    int radiusY = (kernel.rows() - 1) / 2;
    int An = matrix.cols() / radiusX + 1;
    int Bn = matrix.rows() / radiusY + 1;
    int outputWidthPadded = radiusX + matrix.cols() + kernel.cols();
    int outputHeightPadded = radiusY + matrix.rows() + kernel.rows();
tucna's avatar
tucna committed
470 471
    int undefinedComponents = 0;

472 473
    output.create(matrix.size(), CV_MAKETYPE(CV_32F, matrix.channels()));
    output.setTo(0);
tucna's avatar
tucna committed
474

475 476 477 478 479
    if (maskOutput.needed())
    {
        maskOutput.create(mask.rows(), mask.cols(), CV_8UC1);
        maskOutput.setTo(1);
    }
tucna's avatar
tucna committed
480

481 482 483 484 485 486 487 488
    Mat matrixOutputMat = Mat::zeros(outputHeightPadded, outputWidthPadded, CV_MAKETYPE(CV_32F, matrix.channels()));
    Mat maskOutputMat = Mat::ones(outputHeightPadded, outputWidthPadded, CV_8UC1);

    Mat matrixPadded;
    Mat maskPadded;

    copyMakeBorder(matrix, matrixPadded, radiusY, kernel.rows(), radiusX, kernel.cols(), BORDER_CONSTANT, Scalar(0));
    copyMakeBorder(mask, maskPadded, radiusY, kernel.rows(), radiusX, kernel.cols(), BORDER_CONSTANT, Scalar(0));
tucna's avatar
tucna committed
489 490 491 492 493 494 495

    for (int i = 0; i < An; i++)
    {
        for (int o = 0; o < Bn; o++)
        {
            int centerX = (i * radiusX) + radiusX;
            int centerY = (o * radiusY) + radiusY;
496
            Rect area(centerX - radiusX, centerY - radiusY, kernel.cols(), kernel.rows());
tucna's avatar
tucna committed
497

498
            Mat roiMatrix(matrixPadded, area);
tucna's avatar
tucna committed
499 500 501 502 503 504
            Mat roiMask(maskPadded, area);
            Mat kernelMasked;

            kernel.copyTo(kernelMasked, roiMask);

            Mat numerator;
505
            multiply(roiMatrix, kernelMasked, numerator, 1, CV_32F);
tucna's avatar
tucna committed
506 507 508 509 510 511 512

            Scalar denominator = sum(kernelMasked);

            if (denominator[0] == 0)
            {
                if (firstStop)
                {
513 514
                    matrixOutputMat = matrixPadded(Rect(radiusX, radiusY, matrix.cols(), matrix.rows()));
                    maskOutputMat = maskPadded(Rect(radiusX, radiusY, matrix.cols(), matrix.rows()));
tucna's avatar
tucna committed
515 516 517 518 519 520 521

                    return -1;
                }
                else
                {
                    undefinedComponents++;

522 523
                    Mat roiMaskOutput(maskOutputMat, Rect(centerX - radiusX + 1, centerY - radiusY + 1, kernel.cols() - 2, kernel.rows() - 2));
                    roiMaskOutput.setTo(0);
tucna's avatar
tucna committed
524 525 526 527 528 529 530 531 532 533 534

                    continue;
                }
            }

            Scalar component;
            divide(sum(numerator), denominator, component, 1, CV_32F);

            Mat inverse;
            multiply(kernel, component, inverse, 1, CV_32F);

535 536
            Mat roiMatrixOutput(matrixOutputMat, area);
            add(roiMatrixOutput, inverse, roiMatrixOutput);
tucna's avatar
tucna committed
537 538 539
        }
    }

540 541 542 543 544 545
    matrixOutputMat(Rect(radiusX, radiusY, matrix.cols(), matrix.rows())).copyTo(output);

    if (maskOutput.needed())
    {
        maskOutputMat(Rect(radiusX, radiusY, matrix.cols(), matrix.rows())).copyTo(maskOutput);
    }
tucna's avatar
tucna committed
546 547 548

    return undefinedComponents;
}