我在C ++和C#中看过很多Sobel Edge检测操作,但我很难将代码转换为16位图形。我见过的大多数代码都被编程为在R.G.B风格的位图上运行。我的图形是一个ushort []数组,每行中有一个强度值col col。
以下是我提出的代码:(问题是,它不太有用......)有了软糖因素,它可能会找到边缘,但它也找到了很多噪音。
但是,如果我将原始文件保存为JPG,并将其运行到另一个接受Bitmaps的Sobel-Edge-Detector C#代码中,那就更好了......几乎没有噪音!难道我的16位灰度版本不是更好吗?!但它必须是我的代码只是不符合鼻烟。我不理解逻辑。
public void sobel()
{
ushort[] pixels = new ushort[9];
ushort[,] imageData = image_array[0].GetArray();
ushort[,] imageOut = (ushort[,])imageData.Clone();
uint xDim = (ushort)(imageData.GetLength(1) - imageData.GetLength(1) % regions);
uint yDim = (ushort)(imageData.GetLength(0) - imageData.GetLength(0) % regions);
double intensityX = 0.0;
double intensityY = 0.0;
double intensityTotal = 0.0;
int limit = 1000000; //Just arbitrary junk number
int filterOffset = 1;
for (int offsetY = filterOffset; offsetY <
yDim - (filterOffset+1); offsetY++)
{
for (int offsetX = filterOffset; offsetX <
xDim - (filterOffset+1); offsetX++)
{
//intensityX = intensityY = 0;
//intensityTotal = 0;
pixels[0] = imageData[offsetY - 1, offsetX - 1];
pixels[1] = imageData[offsetY - 1, offsetX];
pixels[2] = imageData[offsetY - 1, offsetX + 1];
pixels[3] = imageData[offsetY, offsetX - 1];
pixels[4] = imageData[offsetY, offsetX];
pixels[5] = imageData[offsetY, offsetX + 1];
pixels[6] = imageData[offsetY + 1, offsetX - 1];
pixels[7] = imageData[offsetY + 1, offsetX];
pixels[8] = imageData[offsetY + 1, offsetX + 1];
intensityX = pixels[8] + 2 * pixels[5] + pixels[2] - pixels[0] - 2 * pixels[3] - pixels[6];
intensityY = pixels[8] + 2 * pixels[7] + pixels[6] - pixels[2] - 2 * pixels[1] - pixels[0];
//intensityTotal = Math.Sqrt((intensityX * intensityX) + (intensityY * intensityY));
//intensityTotal = Math.Abs(intensityX) + Math.Abs(intensityY);
intensityTotal = intensityX * intensityX + intensityY * intensityY;
//int sobel = (int)Math.Sqrt((xSobel * xSobel) + (ySobel * ySobel));
//if (intensityTotal > MaximumValue)
// intensityTotal = MaximumValue; //Presumably Black
//if (intensityTotal < -1000) //1000 fudgefactor ...still not working correctly
// intensityTotal = MinimumValue; //Presumably White
//if (intensityTotal < (MinimumValue - 1000))
// intensityTotal = MaximumValue;
if (intensityTotal > limit)
imageOut[offsetY, offsetX] = MinimumValue;
else
imageOut[offsetY, offsetX] = MaximumValue;
//imageOut[offsetY, offsetX] = (ushort)intensityTotal;
}
}
SetImageArrayItem(ImageDataIndex.RAWData, new DiskBackedArray(imageOut));
}
假设&#39; image_array&#39;,xDim,yDim是给予的。您会注意到其他一些评论。
答案 0 :(得分:0)
limit = 1000000
对应于差值幅度约为1000 - 这对于16位(MAX = 65535)图片而言相当小。