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.
https://ibb.co/Hzv4SXB
https://ibb.co/ryGjjjS
https://ibb.co/3sQY9mB
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()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
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);
foreach (var it in item)
{
int key = int.Parse(it.Key.ToString());
Rectangle rect = CvInvoke.BoundingRectangle(contours[key]);
CvInvoke.Rectangle(imgInput, rect, new MCvScalar(0, 0, 255), 5);
}
pictureBox4.Image = imgInput.Bitmap;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
}
}