extract all marked rectangle from hand print image c#

few days ago i have to make an application where a template image with some marked rectangle will be given using that template i have to extract same positions rectangle from other input  images. in c# i make that application using aforge color filtering and blob extraction.

purpose was required data gather from hand print , the print size will be always same so extracted places would be same, so this application was needed.

full code for that application is here

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using AForge.Imaging;
using AForge.Imaging.Filters;
using AForge;
using System.Drawing;
using System.Drawing.Imaging;

namespace createdtemplateforhand
    public partial class Form1 : Form
        ColorFiltering filter = new ColorFiltering();
        GrayscaleBT709 grayscaleFilter = new GrayscaleBT709();
        BlobCounter blobCounter = new BlobCounter();
        public Form1()
            filter.Red = new IntRange(160, 255);
            filter.Green = new IntRange(0, 100);
            filter.Blue = new IntRange(0, 100);
        Rectangle[] rects = null;
        private void button1_Click(object sender, EventArgs e)
            OpenFileDialog o = new OpenFileDialog();
            if (DialogResult.OK == o.ShowDialog())
                pictureBox1.ImageLocation = o.FileName;
            Bitmap objectsImage = null;
            Bitmap image = new Bitmap(o.FileName);
            objectsImage = filter.Apply(image);
            BitmapData objectsData = objectsImage.LockBits(new Rectangle(0, 0, image.Width, image.Height),
                ImageLockMode.ReadOnly, image.PixelFormat);

            // grayscaling
            UnmanagedImage grayImage = grayscaleFilter.Apply(new UnmanagedImage(objectsData));

            // unlock image

            // locate blobs 
            rects = blobCounter.GetObjectRectangles();

        private void button2_Click(object sender, EventArgs e)
            OpenFileDialog o = new OpenFileDialog();
            if (DialogResult.OK == o.ShowDialog())
          pictureBox2.ImageLocation = o.FileName;
            Bitmap image = new Bitmap(o.FileName);
            int i = 0;
                     foreach (Rectangle r in rects)
                Bitmap bmp = new Bitmap(r.Width, r.Height);
                Graphics g = Graphics.FromImage(bmp);
               g.DrawImage(image, 0, 0, r, GraphicsUnit.Pixel);

here the input template was given and i getting rectangle data.
first taking the template data

now input the images

now see the extracted images

for the csharp file download here


About kishordgupta

A software developer
This entry was posted in C#, Image Processing, Winform app and tagged , , , . Bookmark the permalink.

2 Responses to extract all marked rectangle from hand print image c#

  1. Ashish says:

    how can i get the code..?

  2. Burak says:

    Link is broken,please renew link.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s