Separable Filters

For convolution / cross-correlation, we use kernels.

Application of an mXm kernel normally takes m2 operation. If the kernel is separable, then it takes only 2m operations.

Hence, some kernels K, can be split into UVT.

For any given K, we can determine if we can split it into separable kernel by taking its SVD.

Let u,z,v = SVD(K)
K is separable, if, Z[0]!= 0 and Z[!=0]=0
Then, U = √(Z[0]) X u1 and V = √(Z[0]) X v1T

See the Jupyter Notebook to see the implementation in python :

http://josephkj.in/wp-content/uploads/2017/01/SeparableFilters.html 

Animate water from a Tap

Write a program to animate the flow of water from a tap.



#include<conio.h>
#include<stdio.h>
#include<graphics.h>
void main()
{
	int gd=DETECT,gm,i;
	initgraph(&gd,&gm,"c:\\tc\\bgi");
	line(50,400,600,400);
	//up
	line(200,200,200,400);
	line(210,210,210,400);
	//horiz
	line(200,200,310,200);
	line(210,210,290,210);
	//down
	line(310,200,310,220);
	line(290,210,290,220);

	line(250,200,250,180);
	line(260,200,260,180);
	rectangle(240,180,270,170);

	line(250,300,250,399);
	line(350,300,350,399);
	line(250,399,350,399);

	setcolor(WHITE);
	for(i=220;i<=399;i++)
	{
		line(295,i,305,i);
		delay(10);
	}
	for(i=399;i>300;i--)
	{
		line(251,i,349,i);
		delay(100);
	}
	setcolor(RED);
	outtextxy(120,420,"The container is going to overflow.
The tap is stopped.");
	getch();
}


Output
1

Car

Write a program to simulate the movement of a car.



#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<math.h>
void wheel1(int x,int y)
{
	int r=20;
	circle(x,y,r);
	line(x,y-r,x,y+r);
	line(x-r,y,x+r,y);
}
void wheel2(int x,int y)
{
	int r=20,fac;
	fac=r*(sin((45*M_PI)/180));
	circle(x,y,r);
	line(x+fac,y+fac,x-fac,y-fac);
	line(x-fac,y+fac,x+fac,y-fac);

}
void main()
{
	int gd=DETECT,gm,x=100,y=300;
	int c=0;
	initgraph(&gd,&gm,"c:\\tc\\bgi");
	while(!kbhit())
	{
		cleardevice();
		c++;
		line(0,400,639,400);
		line(x,y,x+150,y);
		line(x+150,y,x+200,y+50);
		line(x+200,y+50,x+250,y+50);
		line(x+250,y+50,x+260,y+80);
		line(x+260,y+80,x+180,y+80);

		line(x+120,y+80,x+60,y+80);

		line(x+180,y+80,x+170,y+55);
		line(x+120,y+80,x+130,y+55);
		line(x+170,y+55,x+130,y+55);

		line(x+60,y+80,x+50,y+55);
		line(x+0,y+80,x+10,y+55);
		line(x+50,y+55,x+10,y+55);

		line(x+0,y+80,x-20,y+80);
		line(x-20,y+80,x-20,y+50);
		line(x-20,y+50,x,y);
		if(c%2==0)
		{
			wheel1(x+150,y+80);
			wheel1(x+30,y+80);
		}
		else
		{
			wheel2(x+150,y+80);
			wheel2(x+30,y+80);
		}
		x=(x+10)%639;
		delay(200);
	}
	getch();
}

Output
1

Cycle

Write a program to simulate the man riding a bicycle.



#include<graphics.h>
#include<math.h>

void main()
{
	int gd=DETECT,gm,i,j=-168;
	double x,y,x1,y1,x2,y2,x3,y3,
x4,y4,x5,y5,step=(12*M_PI)/360,a=0.0;
	initgraph(&gd,&gm,"c:\\tc\\bgi");
	while(!kbhit())
	{
	circle(50+j,270,28);
	setfillstyle(SOLID_FILL,BLACK);
	for(i=0;i<=360;i+=15)
		sector(50+j,270,i,i+15,25,25);

	circle(140+j,270,28);
	for(i=0;i<=360;i+=15)
	       sector(140+j,270,i,i+15,25,25);

	x=90+15*cos(a);
	y=275+15*sin(a);
	x1=90-15*cos(a);
	y1=275-15*sin(a);
	setcolor(WHITE);
	line(90+j,275,x+j,y);
	setcolor(WHITE);
	a+=step;
	if(a>=2*M_PI)
	a=0.00;
	x2=x+10; y2=y-35;
	x3=x1+10; y3=y1-35;
	x4=x-40; y4=y-5;
	x5=x+50; y5=y-5;
	circle(x4+j,y4,4);
	circle(x5+j,y5,4);
	setfillstyle(SOLID_FILL,WHITE);
	floodfill(x4+j,y4,WHITE);
	floodfill(x5+j,y5,WHITE);

	line(x+j,y,x2+j,y2);
	line(70+j,222,x2+j,y2);

	setcolor(GREEN);
	setfillstyle(EMPTY_FILL,WHITE);
	for(i=0;i<=360;i+=72)
		sector(90+j,275,i,i+72,8,8);
	setcolor(WHITE);

	line(50+j,268,90+j,273);
	line(50+j,271,90+j,276);
	line(50+j,268,50+j,272);
	line(90+j,273,90+j,277);

	line(49+j,268,71+j,232);
	line(51+j,268,73+j,232);

	line(71+j,232,89+j,275);
	line(73+j,232,91+j,275);

	line(139+j,270,124+j,232);
	line(141+j,270,126+j,232);

	line(72+j,231,125+j,231);
	line(72+j,233,125+j,233);

	line(90+j,274,125+j,233);
	line(90+j,276,125+j,235);

	line(72+j,232,71+j,227);
	line(68+j,227,80+j,226);
	line(63+j,222,68+j,227);
	line(63+j,222,77+j,223);
	line(77+j,223,80+j,226);
	setfillstyle(SOLID_FILL,WHITE);
	floodfill(70+j,225,WHITE);

	line(125+j,232,123+j,225);
	line(123+j,225,129+j,225);
	line(115+j,221,129+j,221);
	rectangle(115+j,220,125+j,222);
	arc(129+j,223,270,90,2);
	setcolor(WHITE);

	line(50+j,269,89+j,267);
	line(50+j,271,89+j,283);

	setcolor(WHITE);
	line(90+j,275,x1+j,y1);

	setcolor(WHITE);
	line(x1+j,y1,x3+j,y3);
	line(70+j,222,x3+j,y3);

	line(70+j,222,75+j,180);
	circle(75+j,168,12);

	line(75+j,190,95+j,210);
	line(95+j,210,120+j,220);
	line(75+j,190,105+j,205);
	line(105+j,205,120+j,219);
	delay(10);
	j++;
	if(j==getmaxx())
	j=-168;
	clearviewport();
	}
	closegraph();
}



Output
1

Pulley

Write a program to animate the movement of the pulley.


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
	int gd=DETECT,gm,x=320,y=100,d=0,dy=40,dyr=429-y;
	initgraph(&gd,&gm,"c:\\tc\\bgi");
	while(!kbhit())
	{
		cleardevice();
		circle(x,y,40);
		pieslice(x,y,0+d,20+d,40);
		pieslice(x,y,120+d,140+d,40);
		pieslice(x,y,240+d,260+d,40);
		d=(d+10)%100;
		line(x-40,y,x-40,y+dy);
		rectangle(x-70,y+dy,x-10,y+dy+50);
		dy+=10;

		line(x+40,y,x+40,y+dyr);
		rectangle(x+10,y+dyr,x+70,y+dyr+20);
		dyr-=10;
		line(10,451,620,451);
		if(y+dy+50>=451)
			break;
		if(y+dyr+20<=y+60)
			break;
		delay(100);
	}
	getch();
}


Output
1

Man Walking in Rain

Write a program to simulate a man walking in rain with an umbrella.


#include<conio.h>
#include<stdio.h>
#include<graphics.h>
void displayMan(int x,int y)
{
	circle(x,y,10);			//face
	line(x,y+10,x,y+30); 		//neck
	line(x,y+30,x-20,y+40);    //left hand
	line(x,y+30,x+20,y+40);    //right hand
	line(x+20,y+40,x+30,y+30);
	line(x,y+30,x,y+70);      	//body
	line(x+30,y+30,x+30,y-90);	//umbrella
	pieslice(x+30,y-30,0,180,55);       

}


void main()
{
	int gd=DETECT, gm,i,d=0,x=50,y=340,shouldMove=1;
	int rx,ry;
	initgraph(&gd,&gm,"C:\\tc\\bgi");
	while(!kbhit())
	{
		cleardevice();
		displayMan(x,340);
		line(0,430,639,430);
		
for(i=0;i<500;i++)
		{
			rx=rand()%639;
			ry=rand()%439;
			if(rx>=(x-40)&&rx<=(x+110))
				if(ry>=(y-50)&&ry<=479)
					continue;
			line(rx-10,ry+10,rx,ry);
		}

		//legs
		if(shouldMove)
		{
			if(d<20)
				d+=4;
			else
				shouldMove=0;
			line(x,y+70,x-d,y+90);
			line(x,y+70,x+d,y+90);
		}
		else
		{
			if(d>0)
				d-=4;
			else
				shouldMove=1;
			line(x,y+70,x-d,y+90);
			line(x,y+70,x+d,y+90);
		}
		delay(200);
		x=(x+10)%639;
	}
	getch();
}

Output
1

Wall Clock

Write a program to simulate the wall clock.


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
	int gdriver = DETECT, gmode, errorcode;


	int theta=270,x=0,sec=0,min=1,y=66;
	initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
	while(!kbhit())
	{
		cleardevice();

		circle(350,250,100);
		circle(350,250,101);
		pieslice(350,250,301-y,300-y,60);
		pieslice(350,250,91-x,90-x,90);
		pieslice(350,250,360-theta+1,360-theta,100);
		theta=theta+6;
		setcolor(WHITE);
		if(sec%60==0)
		{
			x=x+6;
			min=min+1;
		}
		sec=sec+1;
		if(min%60==0)
		{
			y=y+6;
		}
		delay(1000);
	}

}


Output
1

Rotation of a Wheel

Write a program to implement rotation of a wheel.


#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<math.h>
void wheel1(int x,int y)
{
	int r=20;
	circle(x,y,r);
	line(x,y-r,x,y+r);
	line(x-r,y,x+r,y);
}
void wheel2(int x,int y)
{
	int r=20,fac;
	fac=r*(sin((45*M_PI)/180));
	circle(x,y,r);
	line(x+fac,y+fac,x-fac,y-fac);
	line(x-fac,y+fac,x+fac,y-fac);

}
void main()
{
	int gd=DETECT,gm,x=100,y=380;
	int c=0;
	initgraph(&gd,&gm,"c:\\tc\\bgi");
	while(!kbhit())
	{
		cleardevice();
		c++;
		line(0,400,639,400);
		if(c%2==0)
			wheel1(x,y);
		else
			wheel2(x,y);
		x=(x+10)%639;
		delay(200);
	}
	getch();
}


Output
1

Polygon Clipping

Write a program to implement Polygon Clipping.


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int cx1,cx2,cy1,cy2,i;
float x[50],y[50],xt[50],yt[50];
void check(float x1,float y1,float x2,float y2,int side);
int gd=DETECT,gm;
int nt=0,k=0,g,h,n;
void display()
{
	int i;
	for(i=0;i<n-1;i++)
		line(x[i],y[i],x[i+1],y[i+1]);
	line(x[i],y[i],x[0],y[0]);
	getch();
}
void main()
{
	int p,j;
	initgraph(&gd,&gm,"c:\\tc\\bgi");
	printf("\nEnter values for cx1,cy1,cx2,cy2\t:");
	scanf("%d%d%d%d",&cx1,&cy1,&cx2,&cy2);
	printf("\nEnter value of n\t:");
	scanf("%d",&n);

	putpixel(0,0,WHITE);
	putpixel(639,479,WHITE);
	for(p=0;p<n;p++)
	{
		printf("enter x[%d],y[%d]\t:",p+1,p+1);
		scanf("%f%f",&x[p],&y[p]);
	}
	g=x[n-1];h=y[n-1];
	setcolor(5);
	rectangle(cx1,cy1,cx2,cy2);
	getch();
	display();
	for(j=1;j<=4;j++)
	{
		k=0;
		i=0;
		for(i=0;i<n-1;)
		{
			check(x[i],y[i],x[i+1],y[i+1],j);
			i++;
		}
		check(x[i],y[i],x[0],y[0],j);
		n=k;
		for(p=0;p<nt;p++)
		{
			x[p]=xt[p];
			y[p]=yt[p];

		}
	}
	setcolor(WHITE);
	rectangle(cx1,cy1,cx2,cy2);
	setcolor(WHITE);
	display();
}
void check(float x1,float y1,float x2,float y2,int side)
{
	float m;
	  //int i=0;
	if(x1!=x2)
	m=(y2-y1)/(x2-x1);
	switch(side)
	{
		case 1:if(x1<cx1&&x2>cx1)
		{
			xt[k]=cx1;
			yt[k]=m*(cx1-x1)+y1;
			k++;
			xt[k]=x2;
			yt[k]=y2;
			k++;

		}
		else if(x1>=cx1&&x2>=cx1)
		{
			xt[k]=x2;
			yt[k]=y2;
			k++;
		}
		else if(x1>cx1&&x2<cx1)
		{
			xt[k]=cx1;
			yt[k]=m*(cx1-x1)+y1;
			k++;
		}
		break;
		case 2:if(x1>cx2&&x2<cx2)
		{
			xt[k]=cx2;
			yt[k]=m*(cx2-x1)+y1;
			k++;
			xt[k]=x2;
			yt[k]=y2;
			k++;
		}
		else if(x1<cx2&&x2>cx2)
		{
			xt[k]=cx2;
			yt[k]=m*(cx2-x1)+y1;
			k++;
		}
		else if(x1<=cx2&&x2<=cx2)
		{
			xt[k]=x2;
			yt[k]=y2;k++;
		}
		break;
		case 3:if(y1>cy2&&y2<cy2)
		{
			yt[k]=cy2;
			if(x1==x2)
				xt[k]=x1;
			else
				xt[k]=(cy2-y2)/m+x2;
			k++;
			xt[k]=x2;
			yt[k]=y2;
			k++;
		}
		else if(y1<=cy2&&y2<=cy2)
		{
			xt[k]=x2;
			yt[k]=y2;
			k++;
		}
		else if(y1<cy2&&y2>cy2)
		{
			yt[k]=cy2;
			if(x1==x2)
				xt[k]=x1;
			else
			xt[k]=(cy2-y2)/m+x2;
			k++;
		}
		break;
		case 4:if(y1<cy1&&y2>cy1)
		{
			yt[k]=cy1;
			if(x1==x2)
				xt[k]=x1;
			else
			xt[k]=(cy1-y1)/m+x1;
			k++;
			xt[k]=x2;
			yt[k]=y2;
			k++;
		}
		else if(y1>cy1&&y2<cy1)
		{
			yt[k]=cy1;
			if(x1==x2)
				xt[k]=x1;
			else
			xt[k]=(cy1-y2)/m+x2;
			k++;
		}
		else if(y1>=cy1&&y2>=cy1)
		{
			yt[k]=y2;
			xt[k]=x2;
			k++;
		}
		break;
	}
	nt=k;
}


Output
1

Line Clipping

Write a program to implement Line Clipping.


#include<stdio.h>
#include<conio.h>
#include<graphics.h>

#define LEFT 1
#define RIGHT 2
#define BOTTOM 4
#define TOP 8

#define INSIDE(a) (!a)
#define REJECT(a,b) (a&b)
#define ACCEPT(a,b) (!(a|b))

int gd=DETECT,gm;
struct wc
{
	float x,y;
}pt1,pt2;

struct dc
{
	float x,y;
}winmin,winmax;

int encode(struct wc pt,struct dc winmin,struct dc winmax)
{
	int code=0x00;
	if(pt.x<winmin.x)
		code=code|LEFT;
	if(pt.x>winmax.x)
		code=code|RIGHT;
	if(pt.y<winmin.y)
		code=code|BOTTOM;
	if(pt.y>winmax.y)
		code=code|TOP;
	return(code);
}
void swapPts(struct wc *p1,struct wc *p2)
{
	struct wc temp;
	temp=*p1;
	*p1=*p2;
	*p2=temp;
}
void swapCode(int *c1,int *c2)
{
	int temp;
	temp=*c1;
	*c1=*c2;
	*c2=temp;
}
int main()
{
	int x,y,draw=0,code1,code2,c,done=0;
	float m;
	int poly[10]={150,200,350,200,350,400,150,400,150,200};
	initgraph(&gd,&gm,"c:\\tc\\bgi");
	printf("\nEnter the coordinates of the first point: ");
	scanf("%d%d",&x,&y);
	pt1.x=x;
	pt1.y=y;
	printf("\nEnter the coordinates of he second point: ");
	scanf("%d%d",&x,&y);
	pt2.x=x;
	pt2.y=y;

	setcolor(3);
	line(pt1.x,pt1.y,pt2.x,pt2.y);
	drawpoly(5,poly);
	winmin.x=150;
	winmax.x=350;
	winmin.y=200;
	winmax.y=400;
	printf("\n1.CLIP\n2.EXIT\nENTER UR CHOICE:\t");
	scanf("%d",&c);
	switch(c)
	{
		case 1:
			while(!done)
			{
				code1=encode(pt1,winmin,winmax);
				code2=encode(pt2,winmin,winmax);
				if(ACCEPT(code1,code2))
				{
					done=1;
					draw=1;
				}
				else if(REJECT(code1,code2))
					done=1;
				else
				{
					if(INSIDE(code1))
					{
						swapPts(&pt1,&pt2);
						swapCode(&code1,&code2);
					}
					if(pt2.x!=pt1.x)
						m=(pt2.y-pt1.y)/(pt2.x-pt1.x);
					if(code1 & LEFT)
					{
						pt1.y+=(winmin.x-pt1.x)*m;
						pt1.x=winmin.x;
					}
					else if(code1 & RIGHT)
					{
						pt1.y+=(winmax.x-pt1.x)*m;
						pt1.x=winmax.x;
					}
					else if(code1 & BOTTOM)
					{
					   if(pt2.x!=pt1.x)
						pt1.x+=(winmin.y-pt1.y)*m;
						pt1.y=winmin.y;
					}
					else if(code1 & TOP)
					{
					    if(pt2.x!=pt1.x)
						pt1.x+=(winmax.y-pt1.y)*m;
						pt1.x=winmax.y;
					}
				}
			}

			if(draw)
			{
				//clrscr();
				//initgraph(&gd,&gm,"c:\\tc\\bgi");
				setcolor(WHITE);
				drawpoly(5,poly);
				line(pt1.x,pt1.y,pt2.x,pt2.y);
			}
			break;
		case 2:
			break;
		default:
			printf("\nINVALID OPTION");
	}
	getch();
	return(0);
}


Output
1