Difference between revisions of "Edge"

From Ilianko
 
(3 intermediate revisions by the same user not shown)
Line 33: Line 33:
  
 
=====Edge detection=====
 
=====Edge detection=====
 +
 +
<code><pre>
 
namespace WindowsFormsApplication1
 
namespace WindowsFormsApplication1
 
{
 
{
Line 123: Line 125:
 
     }
 
     }
 
}
 
}
 +
</pre></code>
  
 
=====Objects detection=====
 
=====Objects detection=====
Line 130: Line 133:
  
 
==Change background==
 
==Change background==
 +
 +
==webcam==
 +
 +
<code><pre>
 +
<!DOCTYPE html>
 +
<html>
 +
<head>
 +
 +
<script type="text/javascript">
 +
 +
refreshImage = function()
 +
{
 +
  img = document.getElementById("cam");
 +
  img.src="xxx.jpg?"+Math.random();
 +
}
 +
</script>
 +
 +
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
 +
 +
<title>Auto-Refresh</title>
 +
 +
</head>
 +
 +
<body onload="window.setInterval(refreshImage, 1*1000);">
 +
<img src="xxx.jpg?xxx" id="cam" />
 +
</body>
 +
</html>
 +
</pre></code>
 +
 +
Image MySystemImage = frame.ToBitmap();
 +
MySystemImage.Save("xxx.jpg", ImageFormat.Jpeg);
 +
 +
using System.IO;
 +
using System.Drawing.Imaging;
 +
 +
== background==
 +
 +
<code><pre>
 +
namespace WindowsFormsApplication1
 +
{
 +
    public partial class Form1 : Form
 +
    {
 +
        public Form1()
 +
        {
 +
            InitializeComponent();
 +
        }
 +
 +
        private Capture cap = new Capture();
 +
        Image<Bgr, byte> frame;
 +
        Image<Bgr, byte> background;
 +
        Image<Gray, byte> bOr;
 +
        private Image<Gray, byte> mask;
 +
        private Image<Gray, byte> maskI;
 +
 +
        private DispatcherTimer timer = new DispatcherTimer();
 +
 +
        private void Form1_Load(object sender, EventArgs e)
 +
        {
 +
            background = new Image<Bgr, byte>("C:/xxx/sunset.jpg");
 +
            this.Size = new System.Drawing.Size(100, 100);
 +
 +
            frame = cap.QueryFrame();
 +
            frame = cap.QueryFrame();
 +
            frame = cap.QueryFrame();
 +
            frame = cap.QueryFrame();
 +
            frame = cap.QueryFrame();
 +
            frame = cap.QueryFrame();
 +
            frame = cap.QueryFrame();
 +
            frame = cap.QueryFrame();
 +
            frame = cap.QueryFrame();
 +
            frame = cap.QueryFrame();
 +
            frame = cap.QueryFrame();
 +
            bOr = frame.Convert<Gray, byte>().PyrDown().PyrUp();
 +
 +
            this.Size = frame.Size;
 +
 +
 +
            mask = frame.Convert<Gray, byte>();
 +
            maskI = frame.Convert<Gray, byte>();
 +
 +
            background = background.Resize(frame.Size.Width, frame.Size.Height, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR);
 +
 +
            timer.Tick += new EventHandler(timer_tick);
 +
            timer.Interval = new TimeSpan(0, 0, 0, 0, 35);
 +
            timer.Start();
 +
        }
 +
 +
        private Image<Gray, byte> gray;
 +
        Image<Bgr, byte> combined;
 +
 +
 +
        void timer_tick(object sender, EventArgs e)
 +
        {
 +
            frame = cap.QueryFrame();
 +
            gray = frame.Convert<Gray, byte>();
 +
 +
            mask.SetValue(0);
 +
            maskI.SetValue(255);
 +
 +
            gray = gray.PyrDown().PyrUp().AbsDiff(bOr);
 +
            gray = gray.ThresholdBinary(new Gray(20), new Gray(255));
 +
 +
            double largestarea = 0;
 +
 +
            Contour<Point> largestContour = null;
 +
            var sourceContours = gray.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_NONE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_LIST);
 +
 +
            while (sourceContours != null)
 +
            {
 +
                if (largestarea < sourceContours.Area)
 +
                {
 +
                    largestarea = sourceContours.Area;
 +
                    largestContour = sourceContours;
 +
                }
 +
                sourceContours = sourceContours.HNext;
 +
            }
 +
 +
            if (largestContour != null)
 +
            {
 +
                largestarea = largestContour.Area;
 +
                mask.Draw(largestContour, new Gray(255), -1);
 +
                maskI = maskI.Sub(mask);
 +
 +
                combined = background.Copy(maskI);
 +
                frame = frame.Copy(mask);
 +
                frame = frame.Add(combined);
 +
            }
 +
 +
            pictureBox1.Image = frame.Bitmap;
 +
 +
 +
        }
 +
 +
        private void pictureBox1_Click(object sender, EventArgs e)
 +
        {
 +
 +
        }
 +
 +
 +
    }
 +
}
 +
</pre>
 +
</code>

Latest revision as of 07:05, 23 April 2015

Разпознаване на геометрични обекти

Намиране на ръбове(Edge Detection)

Матлаб

im = imread("lena.tif");

Sy = [-1 -2 -1; 0 0 0; 1 2 1];
Sx = Sy';

Gr = im(:,:,1)*0.2+im(:,:,2)*0.7+im(:,:,3)*0.1;

DimY = conv2(Sy,Gr);
DimX = conv2(Sx,Gr);

DimYn = DimY./max(max(DimY));
DimXn = DimX./max(max(DimX));
figure
imshow(DimYn);
figure
imshow(DimXn);


Dim = (DimY.^2 + DimX.^2).^(1/2);
Dimn = Dim./max(max(Dim));
figure
imshow(Dimn)
figure
imshow(Dimn > 0.2)

OpenCV

Размер на изображението (кадъра)
Edge detection
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private DispatcherTimer timer = new DispatcherTimer();

        private Capture cap = new Capture();
        Image<Bgr, byte> frame;

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Size = new System.Drawing.Size(100, 100);
            frame = cap.QueryFrame();
            this.Size = frame.Size;

           

        }


        private void pictureBox1_Click(object sender, EventArgs e)
        {
            Bgr color = new Bgr(255, 0, 0);
            frame = new Image<Bgr, byte>(this.Size.Width, this.Size.Height, color);

            pictureBox1.Image = frame.Bitmap;

            Thread workerThread = new Thread(showImage);
            workerThread.Start();

        }

        private void button1_Click(object sender, EventArgs e)
        {


        }

        void showImage()
        {
            MCvFont font = new MCvFont(Emgu.CV.CvEnum.FONT.CV_FONT_HERSHEY_PLAIN, 10.0, 10.0);

            Bgr color = new Bgr(255, 255, 255);
            Bgr Bcolor = new Bgr(255, 0, 0);

            frame = new Image<Bgr, byte>(frame.Size.Width, frame.Size.Height, Bcolor);

            frame.Draw("0", ref font, new Point(250, 250), color);
            pictureBox1.Image = frame.Bitmap;
            System.Threading.Thread.Sleep(1000);

            frame = new Image<Bgr, byte>(frame.Size.Width, frame.Size.Height, Bcolor);
            frame.Draw("1", ref font, new Point(250, 250), color);
            pictureBox1.Image = frame.Bitmap;
            System.Threading.Thread.Sleep(1000);


            frame = new Image<Bgr, byte>(frame.Size.Width, frame.Size.Height, Bcolor);
            frame.Draw("2", ref font, new Point(250, 250), color);
            pictureBox1.Image = frame.Bitmap;
            System.Threading.Thread.Sleep(1000);

            frame = cap.QueryFrame();
            pictureBox1.Image = frame.Bitmap;
            System.Threading.Thread.Sleep(1000);


            timer.Tick += new EventHandler(timer_tick);
            timer.Interval = new TimeSpan(0, 0, 0, 0, 35);
            timer.Start();
            

        }

        void timer_tick(object sender, EventArgs e)
        {
            frame = cap.QueryFrame();
            Image<Gray, Byte> cannyEdges = frame.Convert<Gray, Byte>().Canny(100, 60);//cannyThreshold, cannyThresholdLinking);
            pictureBox1.Image = cannyEdges.Bitmap;
        }



    }
}
Objects detection

Motion detection

Change background

webcam

<!DOCTYPE html>
<html>
<head>

<script type="text/javascript">

refreshImage = function()
{
  img = document.getElementById("cam");
  img.src="xxx.jpg?"+Math.random();
}
</script>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Auto-Refresh</title>

</head>

<body onload="window.setInterval(refreshImage, 1*1000);">
<img src="xxx.jpg?xxx" id="cam" />
</body>
</html>
Image MySystemImage = frame.ToBitmap();
MySystemImage.Save("xxx.jpg", ImageFormat.Jpeg);
using System.IO;
using System.Drawing.Imaging;

background

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private Capture cap = new Capture();
        Image<Bgr, byte> frame;
        Image<Bgr, byte> background;
        Image<Gray, byte> bOr;
        private Image<Gray, byte> mask;
        private Image<Gray, byte> maskI;

        private DispatcherTimer timer = new DispatcherTimer();

        private void Form1_Load(object sender, EventArgs e)
        {
            background = new Image<Bgr, byte>("C:/xxx/sunset.jpg");
            this.Size = new System.Drawing.Size(100, 100);

            frame = cap.QueryFrame();
            frame = cap.QueryFrame();
            frame = cap.QueryFrame();
            frame = cap.QueryFrame();
            frame = cap.QueryFrame();
            frame = cap.QueryFrame();
            frame = cap.QueryFrame();
            frame = cap.QueryFrame();
            frame = cap.QueryFrame();
            frame = cap.QueryFrame();
            frame = cap.QueryFrame();
            bOr = frame.Convert<Gray, byte>().PyrDown().PyrUp();

            this.Size = frame.Size;


            mask = frame.Convert<Gray, byte>();
            maskI = frame.Convert<Gray, byte>();

            background = background.Resize(frame.Size.Width, frame.Size.Height, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR);

            timer.Tick += new EventHandler(timer_tick);
            timer.Interval = new TimeSpan(0, 0, 0, 0, 35);
            timer.Start();
        }

        private Image<Gray, byte> gray;
        Image<Bgr, byte> combined;


        void timer_tick(object sender, EventArgs e)
        {
            frame = cap.QueryFrame();
            gray = frame.Convert<Gray, byte>();

            mask.SetValue(0);
            maskI.SetValue(255);

            gray = gray.PyrDown().PyrUp().AbsDiff(bOr);
            gray = gray.ThresholdBinary(new Gray(20), new Gray(255));

            double largestarea = 0;

            Contour<Point> largestContour = null;
            var sourceContours = gray.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_NONE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_LIST);

            while (sourceContours != null)
            {
                if (largestarea < sourceContours.Area)
                {
                    largestarea = sourceContours.Area;
                    largestContour = sourceContours;
                }
                sourceContours = sourceContours.HNext;
            }

            if (largestContour != null)
            {
                largestarea = largestContour.Area;
                mask.Draw(largestContour, new Gray(255), -1);
                maskI = maskI.Sub(mask);

                combined = background.Copy(maskI);
                frame = frame.Copy(mask);
                frame = frame.Add(combined);
            }

            pictureBox1.Image = frame.Bitmap;


        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }


    }
}