1

我目前正在构建一个项目,该项目允许用户输入图像,然后使用由伺服系统制成的手臂进行绘制。使用的语言是 C#。该程序的工作方式是用户上传图片,然后使用 Sobel 边缘检测算法对图片进行处理,并将新图片打印在图片框上。我已经设法从图片中获取 X 和 Y 的值,并使手臂中的每个伺服器沿着这些点移动,从而在白纸上绘制图片,但是 Sobel 的工作方式,几乎不可能绘制图像无需使用 Z 轴即可知道何时将笔推到纸上进行绘图,何时不进行绘图。这是我正在使用的代码。我想知道是否有像素描这样更平滑的东西,并且可以在一轮中将其全部绘制出来,而不必将笔作为 Z 轴上下移动。

int oldi = 0;
int oldj=0;
private void sobel()
{   



      Bitmap  originalImage = new Bitmap(ResizeImage(pictureBox1.Image, 70, 70));   
    Bitmap sobelImage = new Bitmap(originalImage);
    int width = originalImage.Width;
    int height = originalImage.Height;

    int[,] gx = new int[,] { { -1, 0, 1 }, { -2, 0, 2 }, { -1, 0, 1 } };
    int[,] gy = new int[,] { { 1, 2, 1 }, { 0, 0, 0 }, { -1, -2, -1 } };

    int[,] allPixR = new int[width, height];
    int[,] allPixG = new int[width, height];
    int[,] allPixB = new int[width, height];

    int limit = 150 * 150;


        for (int i = 0; i < width; i++)
        {
            for (int j = 0; j < height; j++)
            {
                allPixR[i, j] = originalImage.GetPixel(i, j).R;
                allPixG[i, j] = originalImage.GetPixel(i, j).G;
                allPixB[i, j] = originalImage.GetPixel(i, j).B;
            }
        }

        int new_rx = 0, new_ry = 0;
        int new_gx = 0, new_gy = 0;
        int new_bx = 0, new_by = 0;
        int rc, gc, bc;
        for (int i = 1; i < originalImage.Width - 1; i++)
        {
            for (int j = 1; j < originalImage.Height - 1; j++)
            {
                new_rx = 0;
                new_ry = 0;
                new_gx = 0;


         new_gy = 0;
                    new_bx = 0;
                    new_by = 0;
                    rc = 0;
                    gc = 0;
                    bc = 0;

                    for (int wi = -1; wi < 2; wi++)
                    {
                        for (int hw = -1; hw < 2; hw++)
                        {
                            rc = allPixR[i + hw, j + wi];
                            new_rx += gx[wi + 1, hw + 1] * rc;
                            new_ry += gy[wi + 1, hw + 1] * rc;

                            gc = allPixG[i + hw, j + wi];
                            new_gx += gx[wi + 1, hw + 1] * gc;
                            new_gy += gy[wi + 1, hw + 1] * gc;

                            bc = allPixB[i + hw, j + wi];
                            new_bx += gx[wi + 1, hw + 1] * bc;
                            new_by += gy[wi + 1, hw + 1] * bc;
                        }
                    }

                    if (new_rx * new_rx + new_ry * new_ry > limit || new_gx * new_gx + new_gy *                    new_gy > limit || new_bx * new_bx + new_by * new_by > limit)
                    {
    // Draws the edges of the picture on picture box 2                 

       sobelImage.SetPixel(i, j, Color.Black);





                                if (oldi != i)
                                {
                                    oldi = i;

    // move along the X axis   

                          ezB_Connect1.EZB.Servo.SetServoPosition(EZ_B.Servo.ServoPortEnum.D0, i);
                                     Thread.Sleep(500);


                                }




                                    if (oldj != j)
                                    {
                                       // move along the Y axis 

ezB_Connect1.EZB.Servo.SetServoPosition(EZ_B.Servo.ServoPortEnum.D1, j);

                                        oldj = j;



                                    }



                            }

                               pictureBox2.Image = sobelImage;
                            }




                        else
                        {
    // draws white "fills the insides of edges" 
                              sobelImage.SetPixel(i, j, Color.White);



                        }



                }

            }
4

0 回答 0