how to remove straight lines or non-curvical lines in a canny image

风格不统一 提交于 2019-12-17 09:58:34

问题


I have a canny edge image

I want to remove all line except the lines that look like a semi-circle/ellipse or a 'C'. Tried Hough Circle transforms, it detects all curves.Don't need that.


回答1:


A simple approach would be:

  1. Find connected components
  2. Find the minimum oriented bounding box
  3. Compute the aspect ratio of the box, and check if it's too much elongated.

On your image, I marked in red almost straight lines, and in green the curved lines. You can play with the threshold on the aspect ratio:

Code:

#include<opencv2/opencv.hpp>
using namespace cv;


int main()
{
    // Load image
    Mat1b img = imread("path_to_img", IMREAD_GRAYSCALE);

    // Create output image
    Mat3b out;
    cvtColor(img, out, COLOR_GRAY2BGR);

    // Find contours
    vector<vector<Point>> contours;
    findContours(img.clone(), contours, RETR_LIST, CHAIN_APPROX_NONE);

    for (const auto& contour : contours)
    {
        // Find minimum area rectangle
        RotatedRect rr = minAreaRect(contour);

        // Compute aspect ratio
        float aspect_ratio = min(rr.size.width, rr.size.height) / max(rr.size.width, rr.size.height);

        // Define a threshold on the aspect ratio in [0, 1]
        float thresh = 0.2f;

        Vec3b color;
        if (aspect_ratio < thresh) { 
            // Almost straight line
            color = Vec3b(0,0,255); // RED
        }
        else {
            // Curved line
            color = Vec3b(0, 255, 0); // GREEN
        }

        // Color output image
        for (const auto& pt : contour) {
            out(pt) = color;
        }
    }

    imshow("Out", out);
    waitKey();

    return 0;
}



回答2:


  1. Find contours from edge.
  2. Get bounding box.
  3. Calculate ratio of size of diagonal of bounding box to size of contour.

This value will be close to '1' for straight edges. The higher the value of this ratio, the curvy the edge is going to be. It can roughly but quite accurately estimate curliness of the edge.

Happy coding



来源:https://stackoverflow.com/questions/37025890/how-to-remove-straight-lines-or-non-curvical-lines-in-a-canny-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!