SKy Scripter
2012-04-18 15:36:45
SHAPES THAT HAVE BEEN DETECTED

So i was bored and decided to put together a corner detection script
Directions:
1.) make any shape make sure the background is (WHITE) 256^3
2.) make the dimensions of the image no more than 400x300 and center the
object.
3.) load image, hit corner detect, and watch it do magic :)
The way it works.
1.) scans for an edge then gets the edges by using a flood fill algorithm that only follows the edge path.
2.) it scans through each edge pixel and gets the two furthest edge points within the distance of 15 pixels and measures the angle using ATan2(y, x)
[ATTACH]15469[/ATTACH]
3.) measures the angle and defines whether of not its a line or a corner.
please feel free to look and learn :)
DOWNLOAD: [ATTACH]15470[/ATTACH]

So i was bored and decided to put together a corner detection script
Directions:
1.) make any shape make sure the background is (WHITE) 256^3
2.) make the dimensions of the image no more than 400x300 and center the
object.
3.) load image, hit corner detect, and watch it do magic :)
The way it works.
1.) scans for an edge then gets the edges by using a flood fill algorithm that only follows the edge path.
2.) it scans through each edge pixel and gets the two furthest edge points within the distance of 15 pixels and measures the angle using ATan2(y, x)
[ATTACH]15469[/ATTACH]
3.) measures the angle and defines whether of not its a line or a corner.
please feel free to look and learn :)
DOWNLOAD: [ATTACH]15470[/ATTACH]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Fast_Corner_Detect
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Bitmap bmp;
int w = 0;
int h = 0;
int[,] c;
bool[,] cn, bc;
int area, param;
int clred = ColorTranslator.ToWin32(Color.Red);
int clpurple = ColorTranslator.ToWin32(Color.Purple);
int clblue = ColorTranslator.ToWin32(Color.Blue);
int clwhite = ColorTranslator.ToWin32(Color.White);
int[] cx = new int[8] { 1, 0, -1, 0, 1, 1, -1, -1 };
int[] cy = new int[8] { 0, 1, 0, -1, -1, 1, 1, -1 };
void edgefill(int x, int y)
{
area++;
c[x, y] = clblue;
cn[x, y] = true;
for (int i = 0; i < 8; i++)
{
int a = x + cx[i];
int b = y + cy[i];
if ((c[a, b] < clwhite) && (cn[a, b] == false))
for (int j = 0; j < 8; j++)
{
if (c[a + cx[j], b + cy[j]] == clwhite) edgefill(a, b);
}
}
}
int sx, sy;
double adist, filldist, maxd;
Point es, ee;
void ffillc(int x, int y)
{
bc[x, y] = true;
area++;
for (int i = 0; i < 8; i++)
if (bc[x + cx[i], y + cy[i]] == false)
if (cn[x + cx[i], y + cy[i]] == true)
{
int a = x + cx[i];
int b = y + cy[i];
double sqrX = Math.Pow(a - sx, 2);
double sqrY = Math.Pow(b - sy, 2);
adist = Math.Sqrt(sqrX + sqrY);
if (adist < filldist)
{
ffillc(a, b);
}
else
{
if (es.IsEmpty)
// start the max dist for angle
es = new Point(a, b);
sqrX = Math.Pow(es.X - a, 2);
sqrY = Math.Pow(es.Y - b, 2);
adist = Math.Sqrt(sqrX + sqrY);
if (maxd < adist)
{
maxd = adist;
ee = new Point(a, b);
}
}
}
}
public double convertdeg(double deg)
{
// deg = Math.Atan2(y - j, x - i);
double pi2 = Math.PI * 2;
while (deg < pi2) deg = deg + pi2;
while (deg > pi2) deg = deg - pi2;
deg = deg * (180 / Math.PI);
// deg = Math.Abs(deg - 180);
return deg;
}
private void button1_Click(object sender, EventArgs e)
{
// Label edgelb;
// Quick Edge Find
// []
//
// []
//
bool fillit = false;
for (int y = 2; y < h - 2; y++)
for (int x = 2; x < w - 2; x++)
if (c[x, y] < clwhite)
{
if (fillit == false)
{
edgefill(x, y);
fillit = true;
}
}
filldist = 15; // distance to scan
for (int y = 2; y < h - 2; y++)
for (int x = 2; x < w - 2; x++)
if (cn[x, y] == true)
{
bc = new bool[w, h];
sx = x;
sy = y;
es = new Point();
ee = new Point();
maxd = 0;
ffillc(x, y);
// double deg = Math.Atan2(y - j, x - i);
double angle1 = convertdeg(Math.Atan2(y - ee.Y, x - ee.X));
double angle2 = convertdeg(Math.Atan2(y - es.Y, x - es.X));
double realangle = Math.Abs(angle2 - angle1);
realangle = Math.Abs(realangle - 180);
// richTextBox1.AppendText("ANGLE: " + realangle.ToString() + "\n");
if (realangle > 42) c[x, y] = clred; // this is where it finds that its an angle
}
for (int y = 0; y < h; y++)
for (int x = 0; x < w; x++)
{
bmp.SetPixel(x, y, ColorTranslator.FromWin32(c[x, y]));
}
Graphics g;
g = pictureBox1.CreateGraphics();
g.DrawImage(bmp, 0, 0);
}
// int[] hist;
private void button2_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
bmp = new Bitmap(openFileDialog1.FileName);
w = bmp.Width;
h = bmp.Height;
// richTextBox1.AppendText("Width: " + w.ToString() + "Height: " + h.ToString());
c = new int[w, h];
cn = new bool[w, h];
for (int y = 0; y < h; y++)
for (int x = 0; x < w; x++)
{
c[x, y] = ColorTranslator.ToWin32(bmp.GetPixel(x, y));
}
pictureBox1.ImageLocation = openFileDialog1.FileName;
}
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void pictureBox1_DoubleClick(object sender, EventArgs e)
{
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
}
}
}

