Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "EdgeDetection.h"
- EdgeDetection::EdgeDetection()
- {
- original = imread("C:\\circle.jpg", CV_LOAD_IMAGE_GRAYSCALE);
- original.convertTo(original, CV_32FC1, 1.0 / 255.0);
- output = Mat::zeros(original.rows, original.cols, CV_32FC1);
- outputColor = Mat::zeros(original.rows, original.cols, CV_32FC3);
- DeclareSobelOperators();
- blur(original, original, Size(5,5), Point(-1,-1), BORDER_DEFAULT );
- ApplySobel(&original, &output);
- //imshow("ORIGINAL", original);
- imshow("EDGES", outputColor);
- }
- void EdgeDetection::DeclareSobelOperators()
- {
- sobelY = Mat(3, 3, CV_32F);
- sobelY.at<float>(0, 0) = -1.0;
- sobelY.at<float>(0, 1) = 0.0;
- sobelY.at<float>(0, 2) = 1.0;
- sobelY.at<float>(1, 0) = -2.0;
- sobelY.at<float>(1, 1) = 0.0;
- sobelY.at<float>(1, 2) = 2.0;
- sobelY.at<float>(2, 0) = -1.0;
- sobelY.at<float>(2, 1) = 0.0;
- sobelY.at<float>(2, 2) = 1.0;
- sobelX = Mat(3, 3, CV_32F);
- sobelX.at<float>(0, 0) = -1.0;
- sobelX.at<float>(0, 1) = -2.0;
- sobelX.at<float>(0, 2) = -1.0;
- sobelX.at<float>(1, 0) = 0.0;
- sobelX.at<float>(1, 1) = 0.0;
- sobelX.at<float>(1, 2) = 0.0;
- sobelX.at<float>(2, 0) = 1.0;
- sobelX.at<float>(2, 1) = 2.0;
- sobelX.at<float>(2, 2) = 1.0;
- }
- float EdgeDetection::CountEdge(float gradY, float gradX)
- {
- return sqrt(sqr(gradY) + sqr(gradX));
- }
- float EdgeDetection::CountOrientantion(float gradY, float gradX)
- {
- return atan2(gradY,gradX);;
- }
- void EdgeDetection::DrawTangent(int y, int x, float orientation, Mat *image)
- {
- const int length = 150;
- arrowedLine(*image, Point(x, y), Point(x + length * cos(orientation), y - length * sin(orientation)), Scalar(255, 255, 0), 1, 8, 0, 0.1);
- }
- void EdgeDetection::DrawPerpendicular(int y, int x, float orientation, Mat *image)
- {
- const int length = 50;
- arrowedLine(*image, Point(x, y), Point(x + length * sin(orientation), y + length * cos(orientation)), Scalar(0, 255, 255), 1, 8, 0, 0.1);
- }
- Vec3f EdgeDetection::ColorEdges(float orientation)
- {
- Vec3f temp;
- if ((orientation < CV_PI && orientation > 2.9) || (orientation < -2.9 && orientation > -CV_PI))
- {
- temp = Vec3f(0, 0, 1);
- }
- else if (orientation <= 2.9 && orientation > 2.4)
- {
- temp = Vec3f(0, 0.5, 1);
- }
- else if (orientation <= 2.4 && orientation > 1.9)
- {
- temp = Vec3f(0, 1, 1);
- }
- else if (orientation <= 1.9 && orientation > 1.4)
- {
- temp = Vec3f(0, 1, 0.5);
- }
- else if (orientation <= 1.4 && orientation > 0.9)
- {
- temp = Vec3f(0, 1, 0);
- }
- else if (orientation <= 0.9 && orientation > 0.4)
- {
- temp = Vec3f(0.5, 1, 0);
- }
- else if ((orientation <= 0.4 && orientation > 0.0) || (orientation <= 0.0 && orientation > -0.2))
- {
- temp = Vec3f(1, 1, 0);
- }
- else if (orientation <= -0.2 && orientation > -0.8)
- {
- temp = Vec3f(1, 0.5, 0);
- }
- else if (orientation <= -0.8 && orientation > -1.3)
- {
- temp = Vec3f(1, 0, 0);
- }
- else if (orientation <= -1.3 && orientation > -1.8)
- {
- temp = Vec3f(1, 0, 0.5);
- }
- else if (orientation <= -1.8 && orientation > -2.3)
- {
- temp = Vec3f(1, 0, 1);
- }
- else if (orientation <= -2.3 && orientation > - 2.9)
- {
- temp = Vec3f(0.5, 0, 1);
- }
- return temp;
- }
- void EdgeDetection::ApplySobel(Mat *src, Mat *edges)
- {
- float pixValue = 0.0;
- float orientation = 0.0;
- edgesX = Mat::zeros(original.rows, original.cols, CV_32FC1);
- edgesY = Mat::zeros(original.rows, original.cols, CV_32FC1);
- for (int y = 1; y < src->rows - 1; y++)
- {
- for (int x = 1; x < src->cols - 1; x++)
- {
- pixValue += sobelY.at<float>(0, 0) * src->at<float>(y - 1, x - 1);
- pixValue += sobelY.at<float>(0, 2) * src->at<float>(y - 1, x + 1);
- pixValue += sobelY.at<float>(1, 0) * src->at<float>(y, x - 1);
- pixValue += sobelY.at<float>(1, 2) * src->at<float>(y, x + 1);
- pixValue += sobelY.at<float>(2, 0) * src->at<float>(y + 1, x - 1);
- pixValue += sobelY.at<float>(2, 2) * src->at<float>(y + 1, x + 1);
- edgesY.at<float>(y, x) = pixValue;
- pixValue = 0;
- pixValue += sobelX.at<float>(0, 0) * src->at<float>(y - 1, x - 1);
- pixValue += sobelX.at<float>(0, 1) * src->at<float>(y - 1, x);
- pixValue += sobelX.at<float>(0, 2) * src->at<float>(y - 1, x + 1);
- pixValue += sobelX.at<float>(2, 0) * src->at<float>(y + 1, x - 1);
- pixValue += sobelX.at<float>(2, 1) * src->at<float>(y + 1, x);
- pixValue += sobelX.at<float>(2, 2) * src->at<float>(y + 1, x + 1);
- edgesX.at<float>(y, x) = pixValue;
- pixValue = 0;
- float edgeIntensity = CountEdge(edgesY.at<float>(y, x), edgesX.at<float>(y, x));
- edges->at<float>(y, x) = edgeIntensity;
- if (edgeIntensity > 0.2)
- {
- orientation = CountOrientantion(edgesY.at<float>(y, x), edgesX.at<float>(y, x));
- outputColor.at<Vec3f>(y, x) = ColorEdges(orientation);
- if ((x % 20 == 0) && (y % 20 == 0))
- {
- EdgeDetection::DrawTangent(y, x, orientation, &outputColor);
- EdgeDetection::DrawPerpendicular(y, x, orientation, &outputColor);
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment