I am using EmguCV C# for a project where I want to detect objects and draw rectangle on objects and after crop that rectangle images and display them into a dataGridview as a list.
for ex. stone 1,stone 2, stone 3, stone 4 as in list for detected diamonds. when i click on stone 1, stone 1 should be cropped and saved into database. database is accessDb and using Query
I have done detection portion,i have detected the objects on image. now I have to crop that detected objects and save them into database . how do I crop that object part inner rectangles?
Screenshot-104 — ImgBB[
^]
What I have tried:
public partial class Form1 : Form
{
Image<Bgr, byte> imgInput;
public Bitmap img;
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);
CvInvoke.Threshold(imgout[0], imgout2, 1, 255, Emgu.CV.CvEnum.ThresholdType.Binary);
VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
Mat hier = new Mat();
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 > 50 && rect.Height > 50 && area > 100)
{
dict.Add(i, area);
}
}
}
var item = dict.OrderByDescending(v => v.Value);
foreach (var itt in item)
{
int key = int.Parse(itt.Key.ToString());
Rectangle rect = CvInvoke.BoundingRectangle(contours[key]);
CvInvoke.Rectangle(imgInput, rect, new MCvScalar(255, 0, 0), 2);
}
pictureBox4.Image = imgInput.Bitmap;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
}