1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| Mat sobel3(Mat inputImg) { int gx, gy; Mat tempImg; inputImg.copyTo(tempImg);
Mat outputImg = Mat::zeros(inputImg.size(), inputImg.type());
for (int y = 1; y < inputImg.rows - 1; y++) { for (int x = 1; x < inputImg.cols - 1; x++) { gx = (-1 * tempImg.at<uchar>(y - 1, x - 1) + 1 * tempImg.at<uchar>(y - 1, x + 1) + -2 * tempImg.at<uchar>(y, x - 1) + 2 * tempImg.at<uchar>(y, x + 1) + -1 * tempImg.at<uchar>(y + 1, x - 1) + 1 * tempImg.at<uchar>(y + 1, x + 1)) / 4; gy = (1 * tempImg.at<uchar>(y - 1, x - 1) + 2 * tempImg.at<uchar>(y - 1, x) + 1 * tempImg.at<uchar>(y - 1, x + 1) + -1 * tempImg.at<uchar>(y + 1, x - 1) - 2 * tempImg.at<uchar>(y + 1, x) - 1 * tempImg.at<uchar>(y + 1, x + 1));
outputImg.at<uchar>(y, x) = (abs(gx) + abs(gy)) / 2; } }
return outputImg.clone(); }
|