简体   繁体   English

C#AForge.Net图像处理在图像上绘图

[英]C# AForge.Net image processing drawing on image

I'm using this example: http://www.aforgenet.com/framework/features/blobs_processing.html 我正在使用以下示例: http : //www.aforgenet.com/framework/features/blobs_processing.html

I tried using the last example and show the output in a picture box after button click: 我尝试使用最后一个示例,并在单击按钮后在图片框中显示输出:

using AForge;
using AForge.Imaging;
using AForge.Imaging.Filters;
using AForge.Math.Geometry;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace Image_Processing_testings
{
public partial class Form1 : Form
{
    Bitmap image = null;
    public Form1()
    {
        InitializeComponent();
        Bitmap bitmap = new Bitmap("C:\\Users\\user\\Desktop\\test.png");
        Bitmap gsImage = Grayscale.CommonAlgorithms.BT709.Apply(bitmap);

        DifferenceEdgeDetector filter = new DifferenceEdgeDetector();
        image = filter.Apply(gsImage);



        // process image with blob counter
        BlobCounter blobCounter = new BlobCounter();
        blobCounter.ProcessImage(image);
        Blob[] blobs = blobCounter.GetObjectsInformation();

        // create convex hull searching algorithm
        GrahamConvexHull hullFinder = new GrahamConvexHull();

        // lock image to draw on it
        BitmapData data = image.LockBits(
            new Rectangle(0, 0, image.Width, image.Height),
                ImageLockMode.ReadWrite, image.PixelFormat);
        int i = 0;
        // process each blob
        foreach (Blob blob in blobs)
        {
            List<IntPoint> leftPoints, rightPoints, edgePoints = new List<IntPoint>();

            // get blob's edge points
            blobCounter.GetBlobsLeftAndRightEdges(blob,
                out leftPoints, out rightPoints);

            edgePoints.AddRange(leftPoints);
            edgePoints.AddRange(rightPoints);

            // blob's convex hull
            List<IntPoint> hull = hullFinder.FindHull(edgePoints);


            Drawing.Polygon(data, hull, Color.Red);
            i++;
        }

        image.UnlockBits(data);


        MessageBox.Show("Found: " + i + " Objects");
    }

    private void button1_Click_1(object sender, EventArgs e)
    {
        pictureBox1.Image = image;
    }
}
}

The result is that i'm getting the image after filter, but without any polygon on it. 结果是我在过滤器后得到图像,但是上面没有任何多边形。

I counted the number of blob and got 3 for this picture : 我计算了斑点的数量,得到了3张照片:

图片进行处理

The examples in the link you've provided assume that white pixels belong to the object and black pixels belong to the background. 您提供的链接中的示例假定白色像素属于对象,黑色像素属于背景。 Your image that you've provided is the opposite. 您提供的图像则相反。 Therefore, invert the image before applying the algorithm and that should work. 因此,在应用算法之前将图像反转,这样就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM