Click here to Skip to main content
15,614,608 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i am a fresher working on project that captures image and display on the picture box in C# .net-framework.
i have made new project for just testing code for object detection in image.
suppose i have 4 diamonds on an image. after capturing image there will be a 4 rectangles on 4 diamonds.
so i have used Emgu.Cv and code that detects object but only some objects. or not proper detection.

here is some outputs.

here in some images the app detects the perfect objects and in some images that the app create border on image. i want to detect all the objets are there.

What I have tried:

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 Emgu.CV;
using Emgu.CV.Structure;
using Emgu.CV.Util;

namespace objectdetection
    public partial class Form1 : Form
        Image<Bgr, byte> imgInput;
        public Bitmap img;
        bool show = false;

        public Form1()
        private void button1_Click(object sender, EventArgs e)
            OpenFileDialog ofd = new OpenFileDialog();

            if (ofd.ShowDialog() == DialogResult.OK)
               // imgInput = new Emgu.CV.Image<Emgu.CV.Structure.Bgr,Int32>((Bitmap)img);
                imgInput = new Image<Bgr, byte>(ofd.FileName);
                pictureBox1.Image = imgInput.Bitmap;

        private void button2_Click(object sender, EventArgs e)

            Image<Gray, byte> imgout = new Image<Gray, byte>(imgInput.Width, imgInput.Height, new Gray(10));
            Image<Gray, byte> imgout2 = new Image<Gray, byte>(imgInput.Width, imgInput.Height, new Gray(10));

            CvInvoke.CvtColor(imgInput, imgout, Emgu.CV.CvEnum.ColorConversion.Bgr2Rgb);
            pictureBox2.Image = imgout[0].Bitmap;

            CvInvoke.Threshold(imgout[0], imgout2, 1, 255, Emgu.CV.CvEnum.ThresholdType.Binary);

            VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
            Mat hier = new Mat();

            pictureBox3.Image = imgout2.Bitmap;

            CvInvoke.FindContours(imgout[0], contours, hier, Emgu.CV.CvEnum.RetrType.External, Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxSimple);

            Dictionary<int, double> dict = new Dictionary<int, double>();

            if (contours.Size > 0)
                for (int i = 0; i < contours.Size; i++)
                    double area = CvInvoke.ContourArea(contours[i]);
                    Rectangle rect = CvInvoke.BoundingRectangle(contours[i]);

                      if (rect.Width > 10 && rect.Height > 10 && area > 500)
                            dict.Add(i, area);

            var item = dict.OrderByDescending(v => v.Value);

           // Image<Bgr, byte> imgout1 = new Image<Bgr, byte>(imgInput.Width, imgInput.Height, new Bgr(0, 0, 0));

            foreach (var it in item)
                int key = int.Parse(it.Key.ToString());
                Rectangle rect = CvInvoke.BoundingRectangle(contours[key]);
               // CvInvoke.DrawContours(imgInput, contours, key, new MCvScalar(255, 255, 255),4);
                CvInvoke.Rectangle(imgInput, rect, new MCvScalar(0, 0, 255), 5);

            pictureBox4.Image = imgInput.Bitmap;


        private void pictureBox1_Click(object sender, EventArgs e)

Updated 29-Dec-22 19:15pm

1 solution

You will need to use ML.NET | Machine learning made for .NET[^] . Here is a google search for many examples on how to do image recognition: image recognition - Google Search[^]
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900