0% found this document useful (0 votes)
43 views

VISION

This document contains the code for a computer vision application written in C# that tracks an object using a webcam. It initializes the webcam, searches for available video devices, and captures frames in a callback function. It then performs image processing on each frame to detect the object's position and color thresholding to create a binary image. Based on the object's coordinates, it determines motor controls for a robotic arm and sends the appropriate signals to move it and center on the object.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

VISION

This document contains the code for a computer vision application written in C# that tracks an object using a webcam. It initializes the webcam, searches for available video devices, and captures frames in a callback function. It then performs image processing on each frame to detect the object's position and color thresholding to create a binary image. Based on the object's coordinates, it determines motor controls for a robotic arm and sends the appropriate signals to move it and center on the object.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

///////////////VISION//////////////////

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AForge.Video;
using AForge.Video.DirectShow;
using System.Drawing.Drawing2D;
using System.Threading;
namespace Vision_Artificial_Grua_Portico
{
public partial class Form1 : Form
{
int cenx=150,ceny=320,cap,val,i;
string[] puertos = new string[20];
private bool ExistenDispositivos = false;
char caracter;
private FilterInfoCollection DispositivosDeVideo;
private VideoCaptureDevice FuenteDeVideo = null;
public Form1()
{
InitializeComponent();
serialPort1.Open();
if (serialPort1.IsOpen)
label5.Text = "PUERTO CONECTADO";
BuscarDispositivos();
IniciarCamara();
}
public void BuscarDispositivos()
{
DispositivosDeVideo = new
FilterInfoCollection(FilterCategory.VideoInputDevice);
if (DispositivosDeVideo.Count == 0)
{
ExistenDispositivos = false;
}
else
{
ExistenDispositivos = true;
CargarDispositivos(DispositivosDeVideo);
}
}

public void IniciarCamara()
{
FuenteDeVideo = new
VideoCaptureDevice(DispositivosDeVideo[0].MonikerString);
FuenteDeVideo.NewFrame += new NewFrameEventHandler(video_nuevoFrame);
FuenteDeVideo.Start();
}

public void CargarDispositivos(FilterInfoCollection Dispositivos)
{
for (int i = 0; i < Dispositivos.Count; i++)
{
comboBox1.Items.Add(Dispositivos[i].Name.ToString());
}
}

private void video_nuevoFrame(object sender, NewFrameEventArgs eventArgs)
{
cap++;
Bitmap Imagen = (Bitmap)eventArgs.Frame.Clone();
Thread.Sleep(20);
pictureBox1.Image = Imagen;
}

private void button2_Click(object sender, EventArgs e)
{
FuenteDeVideo.Stop();
pictureBox1.Image = null;
}

private void Recibir(object sender, EventArgs e)
{
label6.Text = "Dato Recibido = " + caracter.ToString();
if(caracter==Convert.ToChar("e"))
this.Invoke(new EventHandler(procesado));
}

private void procesado(object sender, EventArgs e)
{
pictureBox2.Image = pictureBox1.Image;
int rojo, verde, azul, x, y, xmin = 100000, xmax = 0, ymin = 100000,
ymax = 0, cenxv, cenyv, error = 28;
Bitmap originalBMP = new Bitmap(pictureBox2.Width, pictureBox2.Height);
Bitmap binariaBMP = new Bitmap(pictureBox2.Width, pictureBox2.Height);
originalBMP = (Bitmap)pictureBox2.Image;
binariaBMP = (Bitmap)pictureBox2.Image;
Graphics g;
g = Graphics.FromImage(binariaBMP);
for (x = 0; x < originalBMP.Width; x++)
{
for (y =150; y < originalBMP.Height; y++)
{
rojo = originalBMP.GetPixel(x, y).R;
verde = originalBMP.GetPixel(x, y).G;
azul = originalBMP.GetPixel(x, y).B;
if ((rojo > 245) && (verde > 245) && (azul > 245))
{
if (ymin > y)
ymin = y;
if (ymax < y)
ymax = y;
if (xmin > x)
xmin = x;
if (xmax < x)
xmax = x;
binariaBMP.SetPixel(x, y, Color.White);
}
else
binariaBMP.SetPixel(x, y, Color.Black);
}
}

cenxv = xmin + (xmax - xmin) / 2;
cenyv = ymin + (ymax - ymin) / 2;
label3.Text = "";
label4.Text = "";
val = 0;
if (cenxv > cenx + error)
{
label3.Text = "Motor X Izquierda";
val = 32;
}
if (cenxv < cenx - error)
{
label3.Text = "Motor X Derecha";
val = 31;
}
if (Math.Abs(cenx - cenxv) <= error)
{
label3.Text = "Motor X Apagado";
val = 2;
}
if (cenyv > ceny + error)
{
//val = 21;
label4.Text = "Motor Y Arriba";
}
if (cenyv < ceny - error)
{
//val = 22;
label4.Text = "Motor Y Abajo";
}
if (Math.Abs(ceny - cenyv) <= error)
{
//val = 20;
label4.Text = "Motor Y Apagado";
}
for (i = 0; i <= 100; i++)
{
byte[] BufferEnvio = new byte[0];
BufferEnvio = BitConverter.GetBytes(val);
label4.Text = "Deteccion";
serialPort1.Write(BufferEnvio, 0, BufferEnvio.Length);
}
val = 0;

Pen mypen = new Pen(Brushes.GreenYellow, 7);
g.DrawLine(mypen, xmin - 10, ymax + 10, xmax + 10, ymax + 10);
g.DrawLine(mypen, xmin - 10, ymax + 10, xmin - 10, ymin - 10);
g.DrawLine(mypen, xmin - 10, ymin - 10, xmax + 10, ymin - 10);
g.DrawLine(mypen, xmax + 10, ymin - 10, xmax + 10, ymax + 10);
cenxv = xmin + (xmax - xmin) / 2;
cenyv = ymin + (ymax - ymin) / 2;
g.DrawLine(mypen, cenxv - 1, cenyv - 1, cenxv + 1, cenyv + 1);
label3.Text = "";
label4.Text = "";
if (cenxv > cenx + error)
label3.Text = "Motor X Izquierda";
if (cenxv < cenx - error)
label3.Text = "Motor X Derecha";
if (Math.Abs(cenx - cenxv) <= error)
label3.Text = "Motor X Apagado";

if (cenyv > ceny + error)
label4.Text = "Motor Y Arriba";
if (cenyv < ceny - error)
label4.Text = "Motor Y Abajo";
if (Math.Abs(ceny - cenyv) <= error)
label4.Text = "Motor Y Apagado";
//label1.Text="xmin= "+xmin.ToString()+"xmax= "+xmax.ToString()+"ymin=
"+ymin.ToString()+"ymax= "+ymax.ToString();
label2.Text = "cenx= " + cenxv.ToString() + "\n" + "ceny= " +
cenyv.ToString();
pictureBox2.Image = binariaBMP;


}
private void button3_Click(object sender, EventArgs e)
{
pictureBox2.Image = pictureBox1.Image;
int rojo, verde, azul, x,
y,xmin=100000,xmax=0,ymin=100000,ymax=0,cenxv,cenyv,error=10;
Bitmap originalBMP = new Bitmap(pictureBox2.Width, pictureBox2.Height);
Bitmap binariaBMP = new Bitmap(pictureBox2.Width, pictureBox2.Height);
originalBMP = (Bitmap)pictureBox2.Image;
binariaBMP = (Bitmap)pictureBox2.Image;
Graphics g;
g = Graphics.FromImage(binariaBMP);
for (y = 290; y < originalBMP.Height; y++)
{
for (x = 0; x < originalBMP.Width; x++)
{
rojo = originalBMP.GetPixel(x, y).R;
verde = originalBMP.GetPixel(x, y).G;
azul = originalBMP.GetPixel(x, y).B;
if ((rojo > 245) && (verde > 245) && (azul > 245))
{
if (ymin > y)
ymin = y;
if (ymax < y)
ymax = y;
if (xmin > x)
xmin = x;
if (xmax < x)
xmax = x;
binariaBMP.SetPixel(x, y, Color.White);
}
else
binariaBMP.SetPixel(x, y, Color.Black);
}
}
Pen mypen = new Pen(Brushes.GreenYellow, 7);
g.DrawLine(mypen, xmin - 10, ymax + 10, xmax + 10, ymax + 10);
g.DrawLine(mypen, xmin - 10, ymax + 10, xmin - 10, ymin - 10);
g.DrawLine(mypen, xmin - 10, ymin - 10, xmax + 10, ymin - 10);
g.DrawLine(mypen, xmax + 10, ymin - 10, xmax + 10, ymax + 10);
cenxv = xmin + (xmax - xmin) / 2;
cenyv = ymin + (ymax - ymin) / 2;
g.DrawLine(mypen, cenxv-1, cenyv-1, cenxv+1, cenyv+1);
label3.Text = "";
label4.Text = "";
if (cenxv > cenx + error)
label3.Text = "Motor X Izquierda";
if (cenxv < cenx - error)
label3.Text = "Motor X Derecha";
if (Math.Abs(cenx-cenxv)<error)
label3.Text = "Motor X Apagado";

if (cenyv > ceny + error)
label4.Text = "Motor Y Arriba";
if (cenyv < ceny - error)
label4.Text = "Motor Y Abajo";
if (Math.Abs(ceny - cenyv) < error)
label4.Text = "Motor Y Apagado";
//label1.Text="xmin= "+xmin.ToString()+"xmax= "+xmax.ToString()+"ymin=
"+ymin.ToString()+"ymax= "+ymax.ToString();
label2.Text = "cenx= " + cenxv.ToString() + "\n" + "ceny= " +
cenyv.ToString();
pictureBox2.Image = binariaBMP;
}

private void serialPort1_DataReceived(object sender,
System.IO.Ports.SerialDataReceivedEventArgs e)
{
caracter = (char)serialPort1.ReadChar();
this.Invoke(new EventHandler(Recibir));
}

private void button1_Click(object sender, EventArgs e)
{
byte[] BufferEnvio = new byte[0];
BufferEnvio = BitConverter.GetBytes(100);
serialPort1.Write(BufferEnvio, 0, BufferEnvio.Length);
}

private void button4_Click(object sender, EventArgs e)
{
byte[] BufferEnvio = new byte[0];
BufferEnvio = BitConverter.GetBytes(200);
serialPort1.Write(BufferEnvio, 0, BufferEnvio.Length);
}

}
}
////////////////////////////////////////////
//////////////Microcontrolador///////////
#include <18f4550.h>
#fuses HSPLL,PLL5,CPUDIV1,NOWDT
#use delay(clock=48Mhz)
#use rs232(baud=9600, xmit=pin_c6, rcv=pin_c7, bits=8, parity=N)
#define st PIN_D2 //////SENTIDO MOTOR DE RUEDAS
#define st2 PIN_D3 //////SENTIDO 2 MOTOR DE RUEDAS
#define sr PIN_D4 //////SENTIDO MOTOR DE TORNILLO
#define sr2 PIN_D5 //////SENTIDO 2 MOTOR DE TORNILLO
#define sw PIN_B7 //////SENTIDO MOTOR DE WINCH
#define sw2 PIN_B6 //////SENTIDO 2 MOTOR DE WINCH
#define bob PIN_B5 //////SENTIDO 2 MOTOR DE WINCH
int16 i,j;
int d;
char caracter;
int x=0,ct,accion=0;


#INT_EXT1
void sc()
{
accion=2;
}

#INT_EXT
void sm()
{
accion=1;
}

#INT_RDA
void RDA_isr()
{
d=getc();
if(d==200)
{
accion=10;
output_toggle(st2);
output_low(st);
}

if(accion!=1)
{
if(d==1)
{
output_low(st2);
delay_ms(50);
output_high(st);
}
if(d==2)
{
output_low(st);
delay_ms(50);
output_high(st2);
}
if(d==3)
{
output_low(sr2);
delay_ms(100);
output_high(sr);
}
if(d==4)
{
output_low(sr);
delay_ms(100);
output_high(sr2);
}
if(d==5)
{
output_low(sw2);
delay_ms(50);
output_high(sw);
}
if(d==8)
{
output_low(sw);
delay_ms(50);
output_high(sw2);
}
if(d==7)
{
output_toggle(bob);
}
if(d==0)
{
output_low(st);
output_low(st2);
output_low(sr);
output_low(sr2);
output_low(sw);
output_low(sw2);
}
}


output_toggle(PIN_C1);
}

#INT_TIMER0
void timer0 ()
{
ct++;
//output_toggle(PIN_C1);
set_timer0(18661);
}

void main()
{
//port_b_pullups(TRUE);
enable_interrupts(GLOBAL);
ext_int_edge(H_TO_L);
setup_timer_0(RTCC_Internal|RTCC_DIV_16);
enable_interrupts(INT_TIMER0);
enable_interrupts(INT_RDA);
set_timer0(18661);
enable_interrupts(INT_EXT);
enable_interrupts(INT_EXT1);
output_high(bob);
do{accion=0;}while(d!=100);
do{output_low(st2);output_high(st);}while(accion!=1);
output_low(st2);output_low(st);
accion=1;
while(1)
{
if(accion==1)
{
do
{
ct=0;
do{output_high(sr);output_low(sr2);}while(ct==0);
do{output_low(sr);output_low(sr2);}while(ct<=10);
caracter="e";
putc(caracter);
do{output_low(sr);output_low(sr2);if(d==2)break;}while(ct<=100);
}while(d!=2);
accion=3;
}
do{}while(accion!=1);

}
}

You might also like