알고리즘 원리
로버츠 모서리 검출 연산자라고도 알려진 로버트 연산자는 지역 차이 연산자를 사용하여 모서리를 찾는 연산자입니다. 로버트 연산자는 아래와 같이 모서리를 찾기 위해 이미지의 대각선 방향의 기울기를 해결하는 데 사용됩니다.
그라데이션 계산:
계산 예시:
(0, 0)에 있는 픽셀의 첫 번째 컨볼루션 계산 결과는 다음과 같습니다. |120×1+125×(-1)|=5
알고리즘 구현
코드 구현(순수 알고리즘):
if (fileDialog.ShowDialog() == DialogResult.OK)
{
picFile = fileDialog.FileName;
inputMat = Cv2.ImRead(picFile, ImreadModes.Grayscale);
outMat = new Mat(new Size(inputMat.Cols, inputMat.Rows), inputMat.Type());
for (int i = 0; i < inputMat.Rows - 1; i++)
{
for (int j = 0; j < inputMat.Cols - 1; j++)
{
var left_up = inputMat.Get<byte>(i, j);
var right_down= inputMat.Get<byte>(i+1, j+1);
var result = Math.Abs(left_up - right_down);
outMat.Set<byte>(i, j, OpenCvSharp.Internal.Util.SaturateCast.ToByte(result));
}
}
picBox_Display.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(inputMat);
picBox_After.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(outMat);
}
같은 방법으로 오른쪽 위에서 왼쪽 아래 방향으로 Edge를 유지하기 위해 다음 연산자를 정의합니다.
OpenCV API 구현
if (fileDialog.ShowDialog() == DialogResult.OK)
{
picFile = fileDialog.FileName;
inputMat = Cv2.ImRead(picFile, ImreadModes.Grayscale);
outMat = new Mat(new Size(inputMat.Cols, inputMat.Rows), inputMat.Type());
//左上到右下方向滤波器
InputArray kernel = InputArray.Create<int>(new int[2, 2] {
{
1, 0}, {
0, -1 } });
Cv2.Filter2D(inputMat, outMat, MatType.CV_16SC1, kernel);
//矩阵取绝对值
Cv2.ConvertScaleAbs(outMat, outMat);
picBox_Display.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(inputMat);
picBox_After.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(outMat);
}
추가 지침:
- 이 경우 .NET에서 사용된 OpenCV 라이브러리는 OpenCvSharp4 입니다.
- 전체 이미지의 에지 추출은 사진을 왼쪽 위에서 오른쪽 아래로, 오른쪽 위에서 왼쪽 아래 방향으로 추가하여 얻을 수 있습니다.