11.48 Sample ‘dilate

Example of dilate and erode.

MGL code:

subplot 2 2 0:title 'Dilate&Erode 1D sample'
new y 11:put y 1 5
ranges 0 10 0 1:axis:box
plot y 'b*'
dilate y 0.5 2
plot y 'rs'
erode y 0.5 1
plot y 'g#o'

subplot 2 2 1:title 'Dilate&Erode 2D sample':rotate 40 60
ranges 0 10 0 10 0 3
axis:box
new z 11 11:put z 3 5 5
boxs z 'b':boxs z 'k#'
dilate z 1 2
boxs z 'r':boxs z 'k#'
erode z 1 1
boxs 2*z 'g':boxs 2*z 'k#'

subplot 2 2 2
text 0.5 0.7 'initial' 'ba';size -2
text 0.5 0.5 'dilate=2' 'ra';size -2
text 0.5 0.3 'erode=1' 'ga';size -2

subplot 2 2 3:title 'Dilate&Erode 3D sample'
rotate 60 50:light on:alpha on
ranges 0 10 0 10 0 10:crange 0 3
axis:box
new a 11 11 11:put a 3 5 5 5
surf3a a a 1.5 'b'
dilate a 1 2
surf3a a a 0.5 'r'
erode a 1 1
surf3a 2*a 2*a 1 'g'

C++ code:

void smgl_dilate_erode(mglGraph *gr)
{
	mglData y(11),	z(11,11), a(11,11,11);
	y.a[5]=1;	z.a[5+11*5]=a.a[5+11*(5+11*5)] = 3;

	if(big!=3)	{	gr->SubPlot(2,2,0);	gr->Title("Dilate&Erode 1D sample");	}
	else	gr->SubPlot(1,1,0,"");
	gr->SetRanges(0,10,0,1);	gr->Axis();	gr->Box();	gr->Plot(y,"b*");
	y.Dilate(1,2);	gr->Plot(y,"rs");
	y.Erode(1,1);	gr->Plot(y,"g#o");
	if(big==3)	return;
	
	gr->SubPlot(2,2,1);	gr->Title("Dilate&Erode 2D sample");
	gr->Rotate(40,60);	gr->SetRanges(0,10,0,10,0,3);
	gr->Axis();	gr->Box();	gr->Boxs(z,"b");	gr->Boxs(z,"k#");
	z.Dilate(1,2);			gr->Boxs(z,"r");	gr->Boxs(z,"k#");
	z.Erode(1,1);	z*=2;	gr->Boxs(z,"g");	gr->Boxs(z,"k#");
	
	gr->SubPlot(2,2,2);
	gr->Puts(0.5,0.7,"initial","ba",-2);
	gr->Puts(0.5,0.5,"dilate=2","ra",-2);
	gr->Puts(0.5,0.3,"erode=1","ga",-2);
	
	gr->SubPlot(2,2,3);	gr->Title("Dilate&Erode 3D sample");
	gr->Rotate(60,50);	gr->Alpha(true);	gr->Light(true);
	gr->SetRanges(0,10,0,10,0,10);	gr->SetRange('c',0,3);
	gr->Axis();	gr->Box();	gr->Surf3A(1.5,a,a,"b");
	a.Dilate(1,2);			gr->Surf3A(0.5,a,a,"r");
	a.Erode(1,1);	a*=2;	gr->Surf3A(1,a,a,"g");
}

Sample dilate