我正在尝试使用C#在Emgu CV中转换this snippet of code。我想我已经将大多数内容转换为EmguCV中应该包含的内容,但是cvKMeans2一直在向我射击,这是没有意义的异常。

这是我的代码:

Image<Bgr, float> src = new Image<Bgr, float>("c:\\blanc.jpg");
         Matrix<Single> samples = new Matrix<Single>(src.Rows * src.Cols, 3);
         for (int y = 0; y < src.Rows; y++)
         {
            for (int x = 0; x < src.Cols; x++)
            {
               for( int z = 0; z < 3; z++)
               {
                  if(z == 0)
                     samples[y + x * src.Rows, z] = Convert.ToSingle(src[y, x].Blue);
                  else if(z == 1)
                     samples[y + x * src.Rows, z] = Convert.ToSingle(src[y, x].Green);
                  else if (z == 2)
                     samples[y + x * src.Rows, z] = Convert.ToSingle(src[y, x].Red);
               }
            }
         }

         MCvTermCriteria term = new MCvTermCriteria(10000, 0.0001);
         term.type = TERMCRIT.CV_TERMCRIT_ITER | TERMCRIT.CV_TERMCRIT_EPS;

         int clusterCount = 3;
         Matrix<Int32> labels = new Matrix<Int32>(src.Width, 1);
         int attempts = 5;
         Matrix<Single> centers = new Matrix<Single>(clusterCount, samples.Cols);
         CvInvoke.cvKMeans2(samples, clusterCount, labels, term, attempts, IntPtr.Zero, KMeansInitType.PPCenters, centers, IntPtr.Zero );

         Image<Bgr, float> new_image = new Image<Bgr, float>(src.Size);

         for (int y = 0; y < src.Rows; y++)
         {
            for (int x = 0; x < src.Cols; x++)
            {
               //nTotal++;
               int cluster_idx = labels[y + x * src.Rows, 0];

               float n1 = centers[cluster_idx, 0];
               float n2 = centers[cluster_idx, 1];
               float n3 = centers[cluster_idx, 2];

               MCvScalar sca = new MCvScalar(n1, n2, n3);
               CvInvoke.cvSet2D(new_image, y, x, sca);
            }
         }

         CvInvoke.cvShowImage( "clustered image", new_image );
         CvInvoke.cvWaitKey( 0 );

我不断收到此异常:



标签必须为Single类型没有意义,因为我需要在cvKMeans2之后将其用作循环中的索引。谁能帮我使此代码正常工作?如果此代码有效,则我们很有可能会购买Emgu的商业许可以在我们的软件中使用。

谢谢!

编辑

根据以下答案,我调整了代码,使其工作如下:
Image<Bgr, float> src = new Image<Bgr, float>(@"C:\\test.png");
            Matrix<float> samples = new Matrix<float>(src.Rows * src.Cols, 1, 3);
            Matrix<int> finalClusters = new Matrix<int>(src.Rows * src.Cols, 1);

            for (int y = 0; y < src.Rows; y++)
            {
                for (int x = 0; x < src.Cols; x++)
                {
                    samples.Data[y + x * src.Rows, 0] = (float)src[y, x].Blue;
                    samples.Data[y + x * src.Rows, 1] = (float)src[y, x].Green;
                    samples.Data[y + x * src.Rows, 2] = (float)src[y, x].Red;
                }
            }

            MCvTermCriteria term = new MCvTermCriteria(10000, 0.0001);
            term.type = TERMCRIT.CV_TERMCRIT_ITER | TERMCRIT.CV_TERMCRIT_EPS;

            int clusterCount = 3;
            int attempts = 5;
            Matrix<Single> centers = new Matrix<Single>(clusterCount, samples.Cols, 3);
            CvInvoke.cvKMeans2(samples, clusterCount, finalClusters, term, attempts, IntPtr.Zero, KMeansInitType.PPCenters, centers, IntPtr.Zero);

            Image<Bgr, Byte> new_image = new Image<Bgr, Byte>(src.Size);

            for (int y = 0; y < src.Rows; y++)
            {
                for (int x = 0; x < src.Cols; x++)
                {
                    int cluster_idx = finalClusters[y + x * src.Rows, 0];
                    MCvScalar sca1 = CvInvoke.cvGet2D(centers, cluster_idx, 0);
                    Bgr color = new Bgr(sca1.v0, sca1.v1, sca1.v2);

                    PointF p = new PointF(x, y);
                    new_image.Draw(new CircleF(p, 1.0f), color, 1);
                }
            }

            CvInvoke.cvShowImage("clustered image", new_image);
            CvInvoke.cvWaitKey(0);

最佳答案

我看一下example you refers to并编写一些普通代码,这些代码可用于在rgb空间中执行kmeans的输入rgb图像上。您可以调整一些参数以使其适应您的需求。

以这个输入图像为例:

EMGUCV代码

Bgr[] clusterColors = new Bgr[] {
        new Bgr(0,0,255),
        new Bgr(0, 255, 0),
        new Bgr(255, 100, 100),
        new Bgr(255,0,255),
        new Bgr(133,0,99),
        new Bgr(130,12,49),
        new Bgr(0, 255, 255)};

        Image<Bgr, float> src = new Image<Bgr, float>("fotobp.jpg");
        Matrix<float> samples = new Matrix<float>(src.Rows * src.Cols, 1, 3);
        Matrix<int> finalClusters = new Matrix<int>(src.Rows * src.Cols, 1);

        for (int y = 0; y < src.Rows; y++)
        {
            for (int x = 0; x < src.Cols; x++)
            {
                samples.Data[y + x * src.Rows, 0] = (float)src[y, x].Blue;
                samples.Data[y + x * src.Rows, 1] = (float)src[y, x].Green;
                samples.Data[y + x * src.Rows, 2] = (float)src[y, x].Red;
            }
        }

        MCvTermCriteria term = new MCvTermCriteria(100, 0.5);
        term.type = TERMCRIT.CV_TERMCRIT_ITER | TERMCRIT.CV_TERMCRIT_EPS;

        int clusterCount = 4;
        int attempts = 5;
        Matrix<Single> centers = new Matrix<Single>(clusterCount, src.Rows * src.Cols);
        CvInvoke.cvKMeans2(samples, clusterCount, finalClusters, term, attempts, IntPtr.Zero, KMeansInitType.PPCenters, IntPtr.Zero, IntPtr.Zero);

        Image<Bgr, float> new_image = new Image<Bgr, float>(src.Size);

        for (int y = 0; y < src.Rows; y++)
        {
            for (int x = 0; x < src.Cols; x++)
            {
                PointF p = new PointF(x, y);
                new_image.Draw(new CircleF(p, 1.0f), clusterColors[finalClusters[y + x * src.Rows, 0]], 1);
            }
        }

        CvInvoke.cvShowImage("clustered image", new_image);
        CvInvoke.cvWaitKey(0);

结果(CLUSTER_NUM = 4)

10-08 09:36