0

この画像など、スキャンしたドキュメントから空白をトリミングするために使用するアプリケーションがあります。私がやりたいのは、カードのみを抽出し、白い/空白の領域をすべて削除することです。私はこれを行うために Emgucv FindContours を使用しています。現時点では、以下に示すように、カードの輪郭とスキャナーによってキャプチャされたノイズを画像で見つけることができます。

ここに画像の説明を入力

私の質問は、見つかった最大の輪郭を切り取る方法、または他の輪郭と空白/空白を削除して抽出する方法です。または、輪郭インデックスで可能ですか?

編集:おそらく別の可能な解決策は、輪郭を別のpictureBoxに描画できる場合です。

私が使用しているコードは次のとおりです。

Image<Bgr, byte> imgInput;
Image<Bgr, byte> imgCrop;

private void abrirToolStripMenuItem_Click(object sender, EventArgs e)
{
    try
    {
        OpenFileDialog dialog = new OpenFileDialog();

        if (dialog.ShowDialog() ==DialogResult.OK)
        {
            imgInput = new Image<Bgr, byte>(dialog.FileName);
            pictureBox1.Image = imgInput.Bitmap;

            imgCrop = imgInput;
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);        
    }
}

private void shapeToolStripMenuItem_Click(object sender, EventArgs e)
{
    if (imgCrop == null)
    {
        return;
    }

    try
    {
        var temp = imgCrop.SmoothGaussian(5).Convert<Gray, byte>().ThresholdBinaryInv(new Gray(230), new Gray(255));
        VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
        Mat m = new Mat();

        CvInvoke.FindContours(temp, contours, m, Emgu.CV.CvEnum.RetrType.External, Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxSimple);

        for (int i = 0; i < contours.Size; i++)
        {
            double perimeter = CvInvoke.ArcLength(contours[i], true);
            VectorOfPoint approx = new VectorOfPoint();
            CvInvoke.ApproxPolyDP(contours[i], approx, 0.04 * perimeter, true);

            CvInvoke.DrawContours(imgCrop, contours, i, new MCvScalar(0, 0, 255), 2);
            pictureBox2.Image = imgCrop.Bitmap;
        }

    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
4

1 に答える 1