Ask Your Question
0

misusing of erode & dilate

asked 2017-02-01 19:35:02 -0600

updated 2017-02-01 19:54:15 -0600

could someone try the code below and share his remarks.

thanks in advance.

#include "opencv2\opencv.hpp"

using namespace cv;

int main() {
    Mat binary(Size(400, 400), CV_8U, Scalar(0));
    rectangle(binary, Rect(200, 200, 100, 100), Scalar(255), -1);
    Mat test1, test2, test3, test4;
    for (int i = 0; i < 90; i++)
    {
        erode(binary, test1, Mat(Size(i, i), CV_8U, Scalar(1)));
        erode(binary, test2, Mat(Size(i, i), CV_8U, Scalar(0)));
        dilate(binary, test3, Mat(Size(i, i), CV_8U, Scalar(1)));
        dilate(binary, test4, Mat(Size(i, i), CV_8U, Scalar(0)));
        imshow("binary", binary);
        imshow("test1", test1);
        imshow("test2", test2);
        imshow("test3", test3);
        imshow("test4", test4);
        moveWindow("test1", 0, 0);
        moveWindow("test2", 400, 0);
        moveWindow("test3", 0, 400);
        moveWindow("test4", 400, 400);
        waitKey(100);
    }
    waitKey();
    return 0;
}
edit retag flag offensive close merge delete

Comments

Huh. There's something screwy about the way it centers the structuring element when it's all zeros. Point(0,0) works for the test2 and test4 cases. That's weird.

Tetragramm gravatar imageTetragramm ( 2017-02-01 20:18:24 -0600 )edit

and your remarks ?

pklab gravatar imagepklab ( 2017-02-02 08:56:45 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
2

answered 2017-02-01 20:45:30 -0600

Tetragramm gravatar image

Here it is. in filter.cpp, in preprocess2DKernel, if the kernel is all zero, it creates a fake coordinate. However, it defaults to Point(0,0). Change it to Point(kernel.rows / 2, kernel.cols / 2) and the problem goes away.

edit flag offensive delete link more

Comments

Thanks for your answer.

sturkmen gravatar imagesturkmen ( 2017-02-02 06:27:27 -0600 )edit
1

answered 2017-02-02 06:36:43 -0600

pklab gravatar image

I think it's a borderType related. When your kernel is growing the default int borderType = BORDER_CONSTANT with the default borderValue assumes white outside therefore white come into the image.

check this modified version

int main()
{
    Mat binary(Size(400, 400), CV_8U, Scalar(0));
    rectangle(binary, Rect(200, 200, 100, 100), Scalar(255), -1);
    Mat test1, test2, test3, test4;
    Mat k1, k0;
    for (int i = 0; i < 90; i++)
    {
        int border = BORDER_REPLICATE;
        k1 = Mat(Size(i, i), CV_8U, Scalar(1));
        k0 = Mat(Size(i, i), CV_8U, Scalar(0));
        erode(binary, test1, k1, Point(-1, -1), 1, border);
        erode(binary, test2, k0, Point(-1, -1), 1, border);
        dilate(binary, test3, k1, Point(-1, -1), 1, border);
        dilate(binary, test4, k0, Point(-1, -1), 1, border);
        imshow("binary", binary);
        imshow("test1", test1);
        imshow("test2", test2);
        imshow("test3", test3);
        imshow("test4", test4);
        moveWindow("test1", 0, 0);
        moveWindow("test2", 400, 0);
        moveWindow("test3", 0, 400);
        moveWindow("test4", 400, 400);
        waitKey(100);
    }
    waitKey();
    return 0;
}
edit flag offensive delete link more

Question Tools

Stats

Asked: 2017-02-01 19:35:02 -0600

Seen: 311 times

Last updated: Feb 02 '17