Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int edgeDetector(cv::Mat src) {
- src.convertTo(src, CV_64F, 1.0f / 255);
- cv::imshow("src", src);
- cv::Mat edge = cv::Mat(src.rows, src.cols, CV_64F);
- cv::Mat edgeSobel = cv::Mat(src.rows, src.cols, CV_64F);
- edge = 0.0f;
- edgeSobel = 0.0f;
- double xMaskData[9] = { -1.0f, 0, 1.0f, -2.0f, 0, 2.0f,-1.0f, 0, 1.0f };
- cv::Mat xMask = cv::Mat(3, 3, CV_64F, xMaskData);
- double yMaskData[9] = { -1.0f, -2.0f, -1.0f, 0, 0, 0, 1.0f, 2.0f, 1.0f };
- cv::Mat yMask = cv::Mat(3, 3, CV_64F, yMaskData);
- std::cout << xMask << std::endl << yMask;
- for (int x = 1; x < src.cols - 1; x++)
- {
- for (int y = 1; y < src.rows -1; y++)
- {
- double xVal = (src.at<double>(y, x - 1) - src.at<double>(y, x + 1)) / 2.0f;
- double yVal = (src.at<double>(y - 1, x) - src.at<double>(y + 1, x)) / 2.0f;
- edge.at<double>(y, x) = sqrt(SQR(xVal) + SQR(yVal));
- double sXVal = 0.0f;
- double sYVal = 0.0f;
- for (int mX = 0; mX < 3; mX++)
- {
- for (int mY = 0; mY < 3; mY++)
- {
- double cVal = src.at<double>(y - mY + 1, x - mX + 1);
- sXVal += cVal * xMask.at<double>(mY, mX);
- sYVal += cVal * yMask.at<double>(mY, mX);
- }
- }
- sXVal *= 1.0f / 8.0f;
- sYVal *= 1.0f / 8.0f;
- edgeSobel.at<double>(y, x) = sqrt(SQR(sXVal) + SQR(sYVal));
- }
- }
- cv::imshow("edge", edge);
- cv::imshow("edgeSobel", edgeSobel);
- cv::waitKey();
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement