hfs.cpp 3.87 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.


#include "precomp.hpp"
#include "opencv2/hfs.hpp"
#include "hfs_core.hpp"

namespace cv{ namespace hfs{

12
class HfsSegmentImpl CV_FINAL : public cv::hfs::HfsSegment{
13 14
public:

15
    void setSegEgbThresholdI(float c) CV_OVERRIDE
16 17 18
    {
        core->hfsSettings.egbThresholdI = c;
    }
19
    float getSegEgbThresholdI() CV_OVERRIDE {
20 21 22 23
        return core->hfsSettings.egbThresholdI;
    }


24
    void setMinRegionSizeI(int n) CV_OVERRIDE
25 26 27
    {
        core->hfsSettings.minRegionSizeI = n;
    }
28
    int getMinRegionSizeI() CV_OVERRIDE
29 30 31 32
    {
        return core->hfsSettings.minRegionSizeI;
    }

33
    void setSegEgbThresholdII(float c) CV_OVERRIDE
34 35 36
    {
        core->hfsSettings.egbThresholdII = c;
    }
37
    float getSegEgbThresholdII() CV_OVERRIDE {
38 39 40 41
        return core->hfsSettings.egbThresholdII;
    }


42
    void setMinRegionSizeII(int n) CV_OVERRIDE
43 44 45
    {
        core->hfsSettings.minRegionSizeII = n;
    }
46
    int getMinRegionSizeII() CV_OVERRIDE
47 48 49 50
    {
        return core->hfsSettings.minRegionSizeII;
    }

51
    void setSpatialWeight(float w) CV_OVERRIDE
52 53 54 55
    {
        core->hfsSettings.slicSettings.coh_weight = w;
        core->reconstructEngine();
    }
56
    float getSpatialWeight() CV_OVERRIDE
57 58 59 60 61
    {
        return core->hfsSettings.slicSettings.coh_weight;
    }


62
    void setSlicSpixelSize(int n) CV_OVERRIDE
63 64 65 66
    {
        core->hfsSettings.slicSettings.spixel_size = n;
        core->reconstructEngine();
    }
67
    int getSlicSpixelSize() CV_OVERRIDE
68 69 70 71 72
    {
        return core->hfsSettings.slicSettings.spixel_size;
    }


73
    void setNumSlicIter(int n) CV_OVERRIDE
74 75 76 77
    {
        core->hfsSettings.slicSettings.num_iters = n;
        core->reconstructEngine();
    }
78
    int getNumSlicIter() CV_OVERRIDE
79 80 81 82 83 84 85 86 87 88 89 90 91 92
    {
        return core->hfsSettings.slicSettings.num_iters;
    }


    HfsSegmentImpl(int height, int width,
        float segEgbThresholdI, int minRegionSizeI, float segEgbThresholdII, int minRegionSizeII,
        float spatialWeight, int spixelSize, int numIter)
    {
        core = Ptr<HfsCore>(new HfsCore(height, width,
            segEgbThresholdI, minRegionSizeI, segEgbThresholdII, minRegionSizeII,
            spatialWeight, spixelSize, numIter));
    }

93 94
    Mat performSegmentGpu(InputArray src, bool ifDraw = true) CV_OVERRIDE;
    Mat performSegmentCpu(InputArray src, bool ifDraw = true) CV_OVERRIDE;
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
private:
    Ptr<HfsCore> core;
};

Mat HfsSegmentImpl::performSegmentGpu(InputArray src, bool ifDraw) {
    Mat src_ = src.getMat();

    CV_Assert(src_.rows == core->hfsSettings.slicSettings.img_size.y);
    CV_Assert(src_.cols == core->hfsSettings.slicSettings.img_size.x);

    Mat seg;
    int num_css = core->processImageGpu(src_, seg);
    if(ifDraw){
        Mat res;
        core->drawSegmentationRes( seg, src_, num_css, res );
        return res;
    }else{
        return seg;
    }
}

Mat HfsSegmentImpl::performSegmentCpu(InputArray src, bool ifDraw) {
    Mat src_ = src.getMat();

    CV_Assert(src_.rows == core->hfsSettings.slicSettings.img_size.y);
    CV_Assert(src_.cols == core->hfsSettings.slicSettings.img_size.x);

    Mat seg;
    int num_css = core->processImageCpu(src_, seg);
    if (ifDraw) {
        Mat res;
        core->drawSegmentationRes(seg, src_, num_css, res);
        return res;
    }
    else {
        return seg;
    }
}

Ptr<HfsSegment> HfsSegment::create(int height, int width, float segEgbThresholdI, int minRegionSizeI,
                                   float segEgbThresholdII, int minRegionSizeII,
                                   float spatialWeight, int spixelSize, int numIter)
{
    return Ptr<HfsSegmentImpl>(new HfsSegmentImpl(height, width,
        segEgbThresholdI, minRegionSizeI, segEgbThresholdII, minRegionSizeII,
        spatialWeight, spixelSize, numIter));
}

}}