Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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;
}