我目前正在构建一个项目,允许用户输入图像,然后使用由伺服器制成的臂来绘制它。使用的语言是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);
}
}
}