Click here to Skip to main content
15,868,004 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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);
       // 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 > 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.DrawContours(imgInput, contours, key, new MCvScalar(255, 255, 255),4);
            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)
    {
       
    }
}
Posted
Comments
[no name] 5-Jan-23 11:13am    
Extract the pixels into a new WriteableBitmap after decoding the original into a pixel buffer.

https://learn.microsoft.com/en-us/dotnet/api/system.windows.media.imaging.writeablebitmap?view=windowsdesktop-7.0

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