Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class EdgeDetection
- {
- private:
- Mat original;
- Mat sobelX;
- Mat sobelY;
- Mat edgesX;
- Mat edgesY;
- Mat output;
- Mat outputColor;
- Mat thinEdges;
- const float threshold1 = 0.45;
- float CountEdge(float gradY, float gradX);
- float CountOrientantion(float gradY, float gradX);
- Vec3f ColorEdges(float orentation);
- void DeclareSobelOperators();
- bool lineDrawn = false;
- void DrawTangent(int y, int x, float orientation, Mat *image);
- void DrawPerpendicular(int y, int x, float orientation, Mat *image);
- void DrawLine(int originY, int originX, float orientation, Mat *image);
- void ApplyEdgeThinning(Mat *src, Mat *edges);
- public:
- EdgeDetection(Mat *filtered);
- void ApplySobel(Mat *src, Mat *edges);
- };
- EdgeDetection::EdgeDetection(Mat *filtered)
- {
- output = Mat::zeros(filtered->rows, filtered->cols, CV_32FC1);
- outputColor = Mat::zeros(filtered->rows, filtered->cols, CV_32FC3);
- thinEdges = Mat::zeros(500, 500, CV_32FC3);
- DeclareSobelOperators();
- ApplySobel(filtered, &output);
- ApplyEdgeThinning(&output, &thinEdges);
- imshow("FILTERED", *filtered);
- //imshow("EDGES", output);
- imshow("EDGES COLOR", outputColor);
- //imshow("THIN EDGES", thinEdges);
- }
- 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);;
- }
- 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(src->rows, src->cols, CV_32FC1);
- edgesY = Mat::zeros(src->rows, src->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.4)
- {
- orientation = CountOrientantion(edgesY.at<float>(y, x), edgesX.at<float>(y, x));
- outputColor.at<Vec3f>(y, x) = ColorEdges(orientation);
- if ((x % 10 == 0) && (y % 10 == 0))
- {
- //EdgeDetection::DrawTangent(y, x, orientation, &outputColor);
- //EdgeDetection::DrawPerpendicular(y, x, orientation, &outputColor);
- //EdgeDetection::DrawLine(y,x, CV_PI / 2, &output);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment