【解決方法】カメラのライブ プレビューから取得した画像からオブジェクトを検出する方法


私は、画像をキャプチャし、C# .net-framework の画像ボックスに表示するプロジェクトに取り組んでいます。
画像内のオブジェクト検出のコードをテストするための新しいプロジェクトを作成しました。
画像に 4 つのダイヤモンドがあるとします。 画像をキャプチャした後、4 つのひし形に 4 つの長方形が表示されます。
だから私は Emgu.Cv とオブジェクトを検出するコードを使用しましたが、一部のオブジェクトのみを検出しました。 または正しく検出されません。

ここにいくつかの出力があります。

https://ibb.co/Hzv4SXB
https://ibb.co/ryGjjjS
https://ibb.co/3sQY9mB

ここでは、アプリが完璧なオブジェクトを検出する画像と、アプリが画像の境界線を作成する画像があります。 そこにあるすべてのオブジェクトを検出したい。

私が試したこと:

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 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)
        {

        }
    }
}

解決策 1

使用する必要があります ML.NET | .NET 向けの機械学習[^] . これは、画像認識を行う方法に関する多くの例をGoogleで検索したものです。 ml.net 画像認識 – Google 検索[^]

コメント

タイトルとURLをコピーしました