distrans.cpp 5.03 KB
Newer Older
wester committed
1 2
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
wester committed
3 4 5 6 7

#include <stdio.h>

using namespace cv;

wester committed
8
int maskSize0 = CV_DIST_MASK_5;
wester committed
9 10
int voronoiType = -1;
int edgeThresh = 100;
wester committed
11
int distType0 = CV_DIST_L1;
wester committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

// The output and temporary images
Mat gray;

// threshold trackbar callback
static void onTrackbar( int, void* )
{
    static const Scalar colors[] =
    {
        Scalar(0,0,0),
        Scalar(255,0,0),
        Scalar(255,128,0),
        Scalar(255,255,0),
        Scalar(0,255,0),
        Scalar(0,128,255),
        Scalar(0,255,255),
        Scalar(0,0,255),
        Scalar(255,0,255)
    };

wester committed
32 33
    int maskSize = voronoiType >= 0 ? CV_DIST_MASK_5 : maskSize0;
    int distType = voronoiType >= 0 ? CV_DIST_L2 : distType0;
wester committed
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

    Mat edge = gray >= edgeThresh, dist, labels, dist8u;

    if( voronoiType < 0 )
        distanceTransform( edge, dist, distType, maskSize );
    else
        distanceTransform( edge, dist, labels, distType, maskSize, voronoiType );

    if( voronoiType < 0 )
    {
        // begin "painting" the distance transform result
        dist *= 5000;
        pow(dist, 0.5, dist);

        Mat dist32s, dist8u1, dist8u2;

        dist.convertTo(dist32s, CV_32S, 1, 0.5);
        dist32s &= Scalar::all(255);

        dist32s.convertTo(dist8u1, CV_8U, 1, 0);
        dist32s *= -1;

        dist32s += Scalar::all(255);
        dist32s.convertTo(dist8u2, CV_8U);

        Mat planes[] = {dist8u1, dist8u2, dist8u2};
        merge(planes, 3, dist8u);
    }
    else
    {
        dist8u.create(labels.size(), CV_8UC3);
        for( int i = 0; i < labels.rows; i++ )
        {
            const int* ll = (const int*)labels.ptr(i);
            const float* dd = (const float*)dist.ptr(i);
            uchar* d = (uchar*)dist8u.ptr(i);
            for( int j = 0; j < labels.cols; j++ )
            {
                int idx = ll[j] == 0 || dd[j] == 0 ? 0 : (ll[j]-1)%8 + 1;
                float scale = 1.f/(1 + dd[j]*dd[j]*0.0004f);
                int b = cvRound(colors[idx][0]*scale);
                int g = cvRound(colors[idx][1]*scale);
                int r = cvRound(colors[idx][2]*scale);
                d[j*3] = (uchar)b;
                d[j*3+1] = (uchar)g;
                d[j*3+2] = (uchar)r;
            }
        }
    }

    imshow("Distance Map", dist8u );
}

static void help()
{
    printf("\nProgram to demonstrate the use of the distance transform function between edge images.\n"
            "Usage:\n"
wester committed
91
            "./distrans [image_name -- default image is stuff.jpg]\n"
wester committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
            "\nHot keys: \n"
            "\tESC - quit the program\n"
            "\tC - use C/Inf metric\n"
            "\tL1 - use L1 metric\n"
            "\tL2 - use L2 metric\n"
            "\t3 - use 3x3 mask\n"
            "\t5 - use 5x5 mask\n"
            "\t0 - use precise distance transform\n"
            "\tv - switch to Voronoi diagram mode\n"
            "\tp - switch to pixel-based Voronoi diagram mode\n"
            "\tSPACE - loop through all the modes\n\n");
}

const char* keys =
{
wester committed
107
    "{1| |stuff.jpg|input image file}"
wester committed
108 109 110 111 112
};

int main( int argc, const char** argv )
{
    help();
wester committed
113 114 115
    CommandLineParser parser(argc, argv, keys);
    string filename = parser.get<string>("1");
    gray = imread(filename.c_str(), 0);
wester committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    if(gray.empty())
    {
        printf("Cannot read image file: %s\n", filename.c_str());
        help();
        return -1;
    }

    namedWindow("Distance Map", 1);
    createTrackbar("Brightness Threshold", "Distance Map", &edgeThresh, 255, onTrackbar, 0);

    for(;;)
    {
        // Call to update the view
        onTrackbar(0, 0);

a  
Kai Westerkamp committed
131
        int c = waitKey(0) & 255;
wester committed
132 133 134 135 136 137 138 139 140

        if( c == 27 )
            break;

        if( c == 'c' || c == 'C' || c == '1' || c == '2' ||
            c == '3' || c == '5' || c == '0' )
            voronoiType = -1;

        if( c == 'c' || c == 'C' )
wester committed
141
            distType0 = CV_DIST_C;
wester committed
142
        else if( c == '1' )
wester committed
143
            distType0 = CV_DIST_L1;
wester committed
144
        else if( c == '2' )
wester committed
145
            distType0 = CV_DIST_L2;
wester committed
146
        else if( c == '3' )
wester committed
147
            maskSize0 = CV_DIST_MASK_3;
wester committed
148
        else if( c == '5' )
wester committed
149
            maskSize0 = CV_DIST_MASK_5;
wester committed
150
        else if( c == '0' )
wester committed
151
            maskSize0 = CV_DIST_MASK_PRECISE;
wester committed
152 153 154 155 156 157 158 159 160 161 162
        else if( c == 'v' )
            voronoiType = 0;
        else if( c == 'p' )
            voronoiType = 1;
        else if( c == ' ' )
        {
            if( voronoiType == 0 )
                voronoiType = 1;
            else if( voronoiType == 1 )
            {
                voronoiType = -1;
wester committed
163 164
                maskSize0 = CV_DIST_MASK_3;
                distType0 = CV_DIST_C;
wester committed
165
            }
wester committed
166 167 168 169 170 171 172 173 174
            else if( distType0 == CV_DIST_C )
                distType0 = CV_DIST_L1;
            else if( distType0 == CV_DIST_L1 )
                distType0 = CV_DIST_L2;
            else if( maskSize0 == CV_DIST_MASK_3 )
                maskSize0 = CV_DIST_MASK_5;
            else if( maskSize0 == CV_DIST_MASK_5 )
                maskSize0 = CV_DIST_MASK_PRECISE;
            else if( maskSize0 == CV_DIST_MASK_PRECISE )
wester committed
175 176 177 178 179 180
                voronoiType = 0;
        }
    }

    return 0;
}