histogramphaseunwrapping.cpp 28 KB
Newer Older
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
/*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, OpenCV Foundation, 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"

namespace cv {
namespace phase_unwrapping {
class CV_EXPORTS_W HistogramPhaseUnwrapping_Impl : public HistogramPhaseUnwrapping
{
public:
    // Constructor
    explicit HistogramPhaseUnwrapping_Impl( const HistogramPhaseUnwrapping::Params &parameters =
                                            HistogramPhaseUnwrapping::Params() );
    // Destructor
54
    virtual ~HistogramPhaseUnwrapping_Impl() CV_OVERRIDE {};
55 56 57

    // Unwrap phase map
    void unwrapPhaseMap( InputArray wrappedPhaseMap, OutputArray unwrappedPhaseMap,
58
                         InputArray shadowMask = noArray() ) CV_OVERRIDE;
59
    // Get reliability map computed from the wrapped phase map
60
    void getInverseReliabilityMap( OutputArray reliabilityMap ) CV_OVERRIDE;
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 770 771 772 773 774 775 776 777 778 779 780 781 782

private:
    // Class describing a pixel
    class Pixel
    {
    private:
        // Value from the wrapped phase map
        float phaseValue;
        // Id of a pixel. Computed from its position in the Mat
        int idx;
        // Pixel is valid if it's not in a shadow region
        bool valid;
        // "Quality" parameter. See reference paper
        float inverseReliability;
        // Number of 2pi  that needs to be added to the pixel to unwrap the phase map
        int increment;
        // Number of pixels that are in the same group as the current pixel
        int nbrOfPixelsInGroup;
        // Group id. At first, group id is the same value as idx
        int groupId;
        // Pixel is alone in its group
        bool singlePixelGroup;
    public:
        Pixel();
        Pixel( float pV, int id, bool v, float iR, int inc );
        float getPhaseValue();
        int getIndex();
        bool getValidity();
        float getInverseReliability();
        int getIncrement();
        int getNbrOfPixelsInGroup();
        int getGroupId();
        bool getSinglePixelGroup();
        void setIncrement( int inc );
        // When a pixel which is not in a single group is added to a new group, we need to keep the previous increment and add "inc" to it.
        void changeIncrement( int inc );
        void setNbrOfPixelsInGroup( int nbr );
        void setGroupId( int gId );
        void setSinglePixelGroup( bool s );
    };
    // Class describing an Edge as presented in the reference paper
    class Edge
    {
    private:
        // Id of the first pixel that forms the edge
        int pixOneId;
        // Id of the second pixel that forms the edge
        int pixTwoId;
        // Number of 2pi that needs to be added to the second pixel to remove discontinuities
        int increment;
    public:
        Edge();
        Edge( int p1, int p2, int inc );
        int getPixOneId();
        int getPixTwoId();
        int getIncrement();
    };
    // Class describing a bin from the histogram
    class HistogramBin
    {
    private:
        float start;
        float end;
        std::vector<Edge> edges;
    public:
        HistogramBin();
        HistogramBin( float s, float e );
        void addEdge( Edge e );
        std::vector<Edge> getEdges();
    };
    // Class describing the histogram. Bins before "thresh" are smaller than the one after "thresh" value
    class Histogram
    {
    private:
        std::vector<HistogramBin> bins;
        float thresh;
        float smallWidth;
        float largeWidth;
        int nbrOfSmallBins;
        int nbrOfLargeBins;
        int nbrOfBins;
    public:
        Histogram();
        void createBins( float t, int nbrOfBinsBeforeThresh, int nbrOfBinsAfterThresh );
        void addBin( HistogramBin b );
        void addEdgeInBin( Edge e, int binIndex);
        float getThresh();
        float getSmallWidth();
        float getLargeWidth();
        int getNbrOfBins();
        std::vector<Edge> getEdgesFromBin( int binIndex );
    };
    // Params for phase unwrapping
    Params params;
    // Pixels from the wrapped phase map
    std::vector<Pixel> pixels;
    // Histogram used to unwrap
    Histogram histogram;
    // Compute pixel reliability.
    void computePixelsReliability( InputArray wrappedPhaseMap, InputArray shadowMask = noArray() );
    // Compute edges reliability and sort them in the histogram
    void computeEdgesReliabilityAndCreateHistogram();
    // Methods that is used in the previous one
    void createAndSortEdge( int idx1, int idx2 );
    // Unwrap the phase map thanks to the histogram
    void unwrapHistogram();
    // add right number of 2*pi to the pixels
    void addIncrement( OutputArray unwrappedPhaseMap );
    // Gamma function from the paper
    float wrap( float a, float b );
    // Similar to the previous one but returns the number of 2pi that needs to be added
    int findInc( float a, float b );
};
// Default parameters
HistogramPhaseUnwrapping::Params::Params(){
    width = 800;
    height = 600;
    histThresh = static_cast<float>(3 * CV_PI * CV_PI);
    nbrOfSmallBins = 10;
    nbrOfLargeBins = 5;
}
HistogramPhaseUnwrapping_Impl::HistogramPhaseUnwrapping_Impl(
                            const HistogramPhaseUnwrapping::Params &parameters ) : params(parameters)
{

}

HistogramPhaseUnwrapping_Impl::Pixel::Pixel()
{

}
// Constructor
HistogramPhaseUnwrapping_Impl::Pixel::Pixel( float pV, int id, bool v, float iR, int inc )
{
    phaseValue = pV;
    idx = id;
    valid = v;
    inverseReliability = iR;
    increment = inc;
    nbrOfPixelsInGroup = 1;
    groupId = id;
    singlePixelGroup = true;
}

float HistogramPhaseUnwrapping_Impl::Pixel::getPhaseValue()
{
    return phaseValue;
}

int HistogramPhaseUnwrapping_Impl::Pixel::getIndex()
{
    return idx;
}

bool HistogramPhaseUnwrapping_Impl::Pixel::getValidity()
{
    return valid;
}

float HistogramPhaseUnwrapping_Impl::Pixel::getInverseReliability()
{
    return inverseReliability;
}

int HistogramPhaseUnwrapping_Impl::Pixel::getIncrement()
{
    return increment;
}

int HistogramPhaseUnwrapping_Impl::Pixel::getNbrOfPixelsInGroup()
{
    return nbrOfPixelsInGroup;
}

int HistogramPhaseUnwrapping_Impl::Pixel::getGroupId()
{
    return groupId;
}

bool HistogramPhaseUnwrapping_Impl::Pixel::getSinglePixelGroup()
{
    return singlePixelGroup;
}

void HistogramPhaseUnwrapping_Impl::Pixel::setIncrement( int inc )
{
    increment = inc;
}
/* When a pixel of a non-single group is added to an other non-single group, we need to add a new
increment to the one that was there previously and that was already removing some wraps.
*/
void HistogramPhaseUnwrapping_Impl::Pixel::changeIncrement( int inc )
{
    increment += inc;
}

void HistogramPhaseUnwrapping_Impl::Pixel::setNbrOfPixelsInGroup( int nbr )
{
    nbrOfPixelsInGroup = nbr;
}
void HistogramPhaseUnwrapping_Impl::Pixel::setGroupId( int gId )
{
    groupId = gId;
}

void HistogramPhaseUnwrapping_Impl::Pixel::setSinglePixelGroup( bool s )
{
    singlePixelGroup = s;
}

HistogramPhaseUnwrapping_Impl::Edge::Edge()
{

}
// Constructor
HistogramPhaseUnwrapping_Impl::Edge::Edge( int p1, int p2, int inc )
{
    pixOneId = p1;
    pixTwoId = p2;
    increment = inc;
}

int HistogramPhaseUnwrapping_Impl::Edge::getPixOneId()
{
    return pixOneId;
}

int HistogramPhaseUnwrapping_Impl::Edge::getPixTwoId()
{
    return pixTwoId;
}

int HistogramPhaseUnwrapping_Impl::Edge::getIncrement()
{
    return increment;
}

HistogramPhaseUnwrapping_Impl::HistogramBin::HistogramBin()
{

}

HistogramPhaseUnwrapping_Impl::HistogramBin::HistogramBin( float s, float e )
{
    start = s;
    end = e;
}

void HistogramPhaseUnwrapping_Impl::HistogramBin::addEdge( Edge e )
{
    edges.push_back(e);
}
std::vector<HistogramPhaseUnwrapping_Impl::Edge> HistogramPhaseUnwrapping_Impl::HistogramBin::getEdges()
{
    return edges;
}
HistogramPhaseUnwrapping_Impl::Histogram::Histogram()
{

}
/*
 * create histogram bins. Bins size is not uniform, as in the reference paper
 *
 */
void HistogramPhaseUnwrapping_Impl::Histogram::createBins( float t, int nbrOfBinsBeforeThresh,
                                                           int nbrOfBinsAfterThresh )
{
    thresh = t;

    nbrOfSmallBins = nbrOfBinsBeforeThresh;
    nbrOfLargeBins = nbrOfBinsAfterThresh;
    nbrOfBins = nbrOfBinsBeforeThresh + nbrOfBinsAfterThresh;

    smallWidth = thresh / nbrOfSmallBins;
    largeWidth = static_cast<float>(32 * CV_PI * CV_PI - thresh) / static_cast<float>(nbrOfLargeBins);

    for( int i = 0; i < nbrOfSmallBins; ++i )
    {
        addBin(HistogramBin(i * smallWidth, ( i + 1 ) * smallWidth));
    }
    for( int i = 0; i < nbrOfLargeBins; ++i )
    {
        addBin(HistogramBin(thresh + i * largeWidth, thresh + ( i + 1 ) * largeWidth));
    }
}
// Add a bin b to the histogram
void HistogramPhaseUnwrapping_Impl::Histogram::addBin( HistogramBin b )
{
    bins.push_back(b);
}
// Add edge E in bin binIndex
void HistogramPhaseUnwrapping_Impl::Histogram::addEdgeInBin( Edge e, int binIndex )
{
    bins[binIndex].addEdge(e);
}
float HistogramPhaseUnwrapping_Impl::Histogram::getThresh()
{
    return thresh;
}

float HistogramPhaseUnwrapping_Impl::Histogram::getSmallWidth()
{
    return smallWidth;
}

float HistogramPhaseUnwrapping_Impl::Histogram::getLargeWidth()
{
    return largeWidth;
}

int HistogramPhaseUnwrapping_Impl::Histogram::getNbrOfBins()
{
    return nbrOfBins;
}

std::vector<HistogramPhaseUnwrapping_Impl::Edge> HistogramPhaseUnwrapping_Impl::
                                                 Histogram::getEdgesFromBin( int binIndex )
{
    std::vector<HistogramPhaseUnwrapping_Impl::Edge> temp;
    temp = bins[binIndex].getEdges();
    return temp;
}
/* Method in which reliabilities are computed and edges are sorted in the histogram.
Increments are computed for each pixels.
 */
void HistogramPhaseUnwrapping_Impl::unwrapPhaseMap( InputArray wrappedPhaseMap,
                                                    OutputArray unwrappedPhaseMap,
                                                    InputArray shadowMask )
{
    Mat &wPhaseMap = *(Mat*) wrappedPhaseMap.getObj();
    Mat mask;
    int rows = params.height;
    int cols = params.width;
    if( shadowMask.empty() )
    {
        mask.create(rows, cols, CV_8UC1);
        mask = Scalar::all(255);
    }
    else
    {
        Mat &temp = *(Mat*) shadowMask.getObj();
        temp.copyTo(mask);
    }

    computePixelsReliability(wPhaseMap, mask);
    computeEdgesReliabilityAndCreateHistogram();

    unwrapHistogram();
    addIncrement(unwrappedPhaseMap);
}

//compute pixels reliabilities according to "A novel algorithm based on histogram processing of reliability for two-dimensional phase unwrapping"

void HistogramPhaseUnwrapping_Impl::computePixelsReliability( InputArray wrappedPhaseMap,
                                                              InputArray shadowMask )
{
    int rows = params.height;
    int cols = params.width;

    Mat &wPhaseMap = *(Mat*) wrappedPhaseMap.getObj();
    Mat &mask = *(Mat*) shadowMask.getObj();

    int idx; //idx is used to store pixel position (idx = i*cols + j)
    bool valid;//tells if a pixel is in the valid mask region

    // H, V, D1, D2 are from the paper
    float H, V, D1, D2, D;
    /* used to store neighbours coordinates
     * ul = upper left, um = upper middle, ur = upper right
     * ml = middle left, mr = middle right
     * ll = lower left, lm = lower middle, lr = lower right
     */
    Point ul, um, ur, ml, mr, ll, lm, lr;

    for( int i = 0; i < rows; ++i )
    {
        for( int j = 0; j < cols; ++j )
        {
            if( mask.at<uchar>( i, j ) != 0 ) //if pixel is in a valid region
            {
                if( i == 0 || i == rows - 1 || j == 0 || j == cols - 1 )
                {
                    idx = i * cols + j;
                    valid = true;
                    Pixel p(wPhaseMap.at<float>(i, j), idx, valid,
                            static_cast<float>(16 * CV_PI * CV_PI), 0);
                    pixels.push_back(p);
                }
                else
                {
                    ul = Point(j-1, i-1);
                    um = Point(j, i-1);
                    ur = Point(j+1, i-1);
                    ml = Point(j-1, i);
                    mr = Point(j+1, i);
                    ll = Point(j-1, i+1);
                    lm = Point(j, i+1);
                    lr = Point(j+1, i+1);

                    Mat neighbourhood = mask( Rect( j-1, i-1, 3, 3 ) );
                    Scalar meanValue = mean(neighbourhood);

                    /* if mean value is different from 255, it means that one of the neighbouring
                     * pixel is not valid -> pixel (i,j) is considered as being on the border.
                     */
                    if( meanValue[0] != 255 )
                    {
                        idx = i * cols + j;
                        valid = true;
                        Pixel p(wPhaseMap.at<float>(i, j), idx, valid,
                                static_cast<float>(16 * CV_PI * CV_PI), 0);
                        pixels.push_back(p);
                    }
                    else
                    {
                        H = wrap(wPhaseMap.at<float>(ml.y, ml.x), wPhaseMap.at<float>(i, j))
                            - wrap(wPhaseMap.at<float>(i, j), wPhaseMap.at<float>(mr.y, mr.x));
                        V = wrap(wPhaseMap.at<float>(um.y, um.x), wPhaseMap.at<float>(i, j))
                            - wrap(wPhaseMap.at<float>(i, j), wPhaseMap.at<float>(lm.y, lm.x));
                        D1 = wrap(wPhaseMap.at<float>(ul.y, ul.x), wPhaseMap.at<float>(i, j))
                            - wrap(wPhaseMap.at<float>(i, j), wPhaseMap.at<float>(lr.y, lr.x));
                        D2 = wrap(wPhaseMap.at<float>(ur.y, ur.x), wPhaseMap.at<float>(i, j))
                            - wrap(wPhaseMap.at<float>(i, j), wPhaseMap.at<float>(ll.y, ll.x));
                        D = H * H + V * V + D1 * D1 + D2 * D2;

                        idx = i * cols + j;
                        valid = true;
                        Pixel p(wPhaseMap.at<float>(i, j), idx, valid, D, 0);
                        pixels.push_back(p);
                    }
                }
            }
            else // pixel is not in a valid region. It's inverse reliability is set to the maximum
            {
                idx = i * cols + j;
                valid = false;
                Pixel p(wPhaseMap.at<float>(i, j), idx, valid,
                        static_cast<float>(16 * CV_PI * CV_PI), 0);
                pixels.push_back(p);
            }
        }
    }
}
/* Edges are created from the vector of pixels. We loop on the vector and create the edges
 * that link the current pixel to his right neighbour (first edge) and the one that is under it (second edge)
 */
void HistogramPhaseUnwrapping_Impl::computeEdgesReliabilityAndCreateHistogram()
{
    int row;
    int col;
    histogram.createBins(params.histThresh, params.nbrOfSmallBins, params.nbrOfLargeBins);
    int nbrOfPixels = static_cast<int>(pixels.size());
    /* Edges are built by considering a pixel and it's right-neighbour and lower-neighbour.
     We discard non-valid pixels here.
     */
    for( int i = 0; i < nbrOfPixels; ++i )
    {
        if( pixels[i].getValidity() )
        {
            row = pixels[i].getIndex() / params.width;
            col = pixels[i].getIndex() % params.width;

            if( row != params.height - 1 && col != params.width -1 )
            {
                int idxRight, idxDown;
                idxRight = row * params.width + col + 1; // Pixel to the right
                idxDown = ( row + 1 ) * params.width + col; // Pixel under pixel i.
                createAndSortEdge(i, idxRight);
                createAndSortEdge(i, idxDown);
            }
            else if( row != params.height - 1 && col == params.width - 1 )
            {
                int idxDown = ( row + 1 ) * params.width + col;
                createAndSortEdge(i, idxDown);
            }
            else if( row == params.height - 1 && col != params.width - 1 )
            {
                int idxRight = row * params.width + col + 1;
                createAndSortEdge(i, idxRight);
            }
        }
    }
}
/*used along the previous method to sort edges in the histogram*/
void HistogramPhaseUnwrapping_Impl::createAndSortEdge( int idx1, int idx2 )
{
    if( pixels[idx2].getValidity() )
    {
        float edgeReliability = pixels[idx1].getInverseReliability() +
                                pixels[idx2].getInverseReliability();
        int inc = findInc(pixels[idx2].getPhaseValue(), pixels[idx1].getPhaseValue());
        Edge e(idx1, idx2, inc);

        if( edgeReliability < histogram.getThresh() )
        {
            int binIndex = static_cast<int> (ceil(edgeReliability / histogram.getSmallWidth()) - 1);
            if( binIndex == -1 )
            {
                binIndex = 0;
            }
            histogram.addEdgeInBin(e, binIndex);
        }
        else
        {
            int binIndex = params.nbrOfSmallBins +
                           static_cast<int> (ceil((edgeReliability - histogram.getThresh()) /
                                 histogram.getLargeWidth()) - 1);
            histogram.addEdgeInBin(e, binIndex);
        }
    }
}

void HistogramPhaseUnwrapping_Impl::unwrapHistogram()
{
    int nbrOfPixels = static_cast<int>(pixels.size());
    int nbrOfBins = histogram.getNbrOfBins();
    /* This vector is used to keep track of the number of pixels in each group and avoid useless group.
       For example, if lastPixelAddedToGroup[10] is equal to 5, it means that pixel "5" was the last one
       to be added to group 10. So, pixel "5" is the only one that has the correct value for parameter
       "numberOfPixelsInGroup" in order to avoid a loop on all the pixels to update this number*/
    std::vector<int> lastPixelAddedToGroup(nbrOfPixels, 0);
    for( int i = 0; i < nbrOfBins; ++i )
    {
        std::vector<Edge> currentEdges = histogram.getEdgesFromBin(i);
        int nbrOfEdgesInBin = static_cast<int>(currentEdges.size());

        for( int j = 0; j < nbrOfEdgesInBin; ++j )
        {

            int pOneId = currentEdges[j].getPixOneId();
            int pTwoId = currentEdges[j].getPixTwoId();
            // Both pixels are in a single group.
            if( pixels[pOneId].getSinglePixelGroup() && pixels[pTwoId].getSinglePixelGroup() )
            {
                float invRel1 = pixels[pOneId].getInverseReliability();
                float invRel2 = pixels[pTwoId].getInverseReliability();
                // Quality of pixel 2 is better than that of pixel 1 -> pixel 1 is added to group 2
                if( invRel1 > invRel2 )
                {
                    int newGroupId = pixels[pTwoId].getGroupId();
                    int newInc = pixels[pTwoId].getIncrement() + currentEdges[j].getIncrement();
                    pixels[pOneId].setGroupId(newGroupId);
                    pixels[pOneId].setIncrement(newInc);
                    lastPixelAddedToGroup[newGroupId] = pOneId; // Pixel 1 is the last one to be added to group 2
                }
                else
                {
                    int newGroupId = pixels[pOneId].getGroupId();
                    int newInc = pixels[pOneId].getIncrement() - currentEdges[j].getIncrement();
                    pixels[pTwoId].setGroupId(newGroupId);
                    pixels[pTwoId].setIncrement(newInc);
                    lastPixelAddedToGroup[newGroupId] = pTwoId;
                }
                pixels[pOneId].setNbrOfPixelsInGroup(2);
                pixels[pTwoId].setNbrOfPixelsInGroup(2);
                pixels[pOneId].setSinglePixelGroup(false);
                pixels[pTwoId].setSinglePixelGroup(false);
            }
            //p1 is in a single group, p2 is not -> p1 added to p2
            else if( pixels[pOneId].getSinglePixelGroup() && !pixels[pTwoId].getSinglePixelGroup() )
            {
                int newGroupId = pixels[pTwoId].getGroupId();
                int lastPix = lastPixelAddedToGroup[newGroupId];
                int newNbrOfPixelsInGroup = pixels[lastPix].getNbrOfPixelsInGroup() + 1;
                int newInc = pixels[pTwoId].getIncrement() + currentEdges[j].getIncrement();

                pixels[pOneId].setGroupId(newGroupId);
                pixels[pOneId].setNbrOfPixelsInGroup(newNbrOfPixelsInGroup);
                pixels[pTwoId].setNbrOfPixelsInGroup(newNbrOfPixelsInGroup);
                pixels[pOneId].setIncrement(newInc);
                pixels[pOneId].setSinglePixelGroup(false);

                lastPixelAddedToGroup[newGroupId] = pOneId;
            }
            //p2 is in a single group, p1 is not -> p2 added to p1
            else if( !pixels[pOneId].getSinglePixelGroup() && pixels[pTwoId].getSinglePixelGroup() )
            {
                int newGroupId = pixels[pOneId].getGroupId();
                int lastPix = lastPixelAddedToGroup[newGroupId];
                int newNbrOfPixelsInGroup = pixels[lastPix].getNbrOfPixelsInGroup() + 1;
                int newInc = pixels[pOneId].getIncrement() - currentEdges[j].getIncrement();

                pixels[pTwoId].setGroupId(newGroupId);
                pixels[pTwoId].setNbrOfPixelsInGroup(newNbrOfPixelsInGroup);
                pixels[pOneId].setNbrOfPixelsInGroup(newNbrOfPixelsInGroup);
                pixels[pTwoId].setIncrement(newInc);
                pixels[pTwoId].setSinglePixelGroup(false);

                lastPixelAddedToGroup[newGroupId] = pTwoId;
            }
            //p1 and p2 are in two different groups
            else if( pixels[pOneId].getGroupId() != pixels[pTwoId].getGroupId() )
            {
                int pOneGroupId = pixels[pOneId].getGroupId();
                int pTwoGroupId = pixels[pTwoId].getGroupId();

                float invRel1 = pixels[pOneId].getInverseReliability();
                float invRel2 = pixels[pTwoId].getInverseReliability();

                int lastAddedToGroupOne = lastPixelAddedToGroup[pOneGroupId];
                int lastAddedToGroupTwo = lastPixelAddedToGroup[pTwoGroupId];

                int nbrOfPixelsInGroupOne = pixels[lastAddedToGroupOne].getNbrOfPixelsInGroup();
                int nbrOfPixelsInGroupTwo = pixels[lastAddedToGroupTwo].getNbrOfPixelsInGroup();

                int totalNbrOfPixels = nbrOfPixelsInGroupOne + nbrOfPixelsInGroupTwo;

                if( nbrOfPixelsInGroupOne < nbrOfPixelsInGroupTwo ||
                   (nbrOfPixelsInGroupOne == nbrOfPixelsInGroupTwo && invRel1 >= invRel2) ) //group p1 added to group p2
                {
                    pixels[pTwoId].setNbrOfPixelsInGroup(totalNbrOfPixels);
                    pixels[pOneId].setNbrOfPixelsInGroup(totalNbrOfPixels);
                    int inc = pixels[pTwoId].getIncrement() + currentEdges[j].getIncrement() -
                                 pixels[pOneId].getIncrement();
                    lastPixelAddedToGroup[pTwoGroupId] = pOneId;

                    for( int k = 0; k < nbrOfPixels; ++k )
                    {
                        if( pixels[k].getGroupId() == pOneGroupId )
                        {
                            pixels[k].setGroupId(pTwoGroupId);
                            pixels[k].changeIncrement(inc);
                        }
                    }
                }
                else if( nbrOfPixelsInGroupOne > nbrOfPixelsInGroupTwo ||
                        (nbrOfPixelsInGroupOne == nbrOfPixelsInGroupTwo && invRel2 > invRel1) ) //group p2 added to group p1
                {
                    int oldGroupId = pTwoGroupId;
                    pixels[pOneId].setNbrOfPixelsInGroup(totalNbrOfPixels);
                    pixels[pTwoId].setNbrOfPixelsInGroup(totalNbrOfPixels);
                    int inc = pixels[pOneId].getIncrement() - currentEdges[j].getIncrement() -
                              pixels[pTwoId].getIncrement();
                    lastPixelAddedToGroup[pOneGroupId] = pTwoId;

                    for( int k = 0; k < nbrOfPixels; ++k )
                    {
                        if( pixels[k].getGroupId() == oldGroupId )
                        {
                            pixels[k].setGroupId(pOneGroupId);
                            pixels[k].changeIncrement(inc);
                        }
                    }
                }
            }
        }
    }
}
void HistogramPhaseUnwrapping_Impl::addIncrement( OutputArray unwrappedPhaseMap )
{
    Mat &uPhaseMap = *(Mat*) unwrappedPhaseMap.getObj();
    int rows = params.height;
    int cols = params.width;
    if( uPhaseMap.empty() )
        uPhaseMap.create(rows, cols, CV_32FC1);
    int nbrOfPixels = static_cast<int>(pixels.size());
    for( int i = 0; i < nbrOfPixels; ++i )
    {
        int row = pixels[i].getIndex() / params.width;
        int col = pixels[i].getIndex() % params.width;

        if( pixels[i].getValidity() )
        {
            uPhaseMap.at<float>(row, col) = pixels[i].getPhaseValue() +
                                            static_cast<float>(2 * CV_PI * pixels[i].getIncrement());
        }
    }
}
float HistogramPhaseUnwrapping_Impl::wrap( float a, float b )
{
    float result;
    float difference = a - b;
    float pi = static_cast<float>(CV_PI);
    if( difference > pi )
        result = ( difference - 2 * pi );
    else if( difference < -pi )
        result = ( difference + 2 * pi );
    else
        result = difference;
    return result;
}

int HistogramPhaseUnwrapping_Impl::findInc( float a, float b )
{
    float difference;
    int wrapValue;
    difference = b - a;
    float pi = static_cast<float>(CV_PI);
    if( difference > pi )
        wrapValue = -1;
    else if( difference < -pi )
        wrapValue = 1;
    else
        wrapValue = 0;
    return wrapValue;
}

//create a Mat that shows pixel inverse reliabilities
void HistogramPhaseUnwrapping_Impl::getInverseReliabilityMap( OutputArray inverseReliabilityMap )
{
    int rows = params.height;
    int cols = params.width;
    Mat &reliabilityMap_ = *(Mat*) inverseReliabilityMap.getObj();
    if( reliabilityMap_.empty() )
        reliabilityMap_.create(rows, cols, CV_32FC1);
    for( int i = 0; i < rows; ++i )
    {
        for( int j = 0; j < cols; ++j )
        {
            int idx = i * cols + j;
            reliabilityMap_.at<float>(i, j) = pixels[idx].getInverseReliability();
        }
    }
}

Ptr<HistogramPhaseUnwrapping> HistogramPhaseUnwrapping::create( const HistogramPhaseUnwrapping::Params
                                                                &params )
{
    return makePtr<HistogramPhaseUnwrapping_Impl>(params);
}

}
783
}