Removing long horizontal / vertical lines from an edge image using OpenCV

How to use standard image processing filters (from OpenCV) to remove long horizontal and vertical lines from an image?

B&W images, so deleting simply means black.

Illustration:

illustration of required filter

I am currently doing this in Python, iterating over rows and columns of pixels and detecting ranges of consecutive pixels, removing those that are larger than N pixels. However, it is very slow compared to the OpenCV library, and if there is a way to do the same with the OpenCV functions, it will probably be an order of magnitude faster.

I suppose this can be done by convolution using a kernel that has a series of pixels (for horizontal lines), but it's hard for me to determine the exact operation that could do the trick.

+4
source share
5 answers

If your lines are really horizontal / vertical try this

import cv2
import numpy as np
img = cv2.imread('c:/data/test.png')
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
linek = np.zeros((11,11),dtype=np.uint8)
linek[5,...]=1
x=cv2.morphologyEx(gray, cv2.MORPH_OPEN, linek ,iterations=1)
gray-=x
cv2.imshow('gray',gray)
cv2.waitKey(0)    

Result

enter image description here

+6
source

How long does it last? Long, as in, strings whose length exceeds the length of the entire image or is longer than npixels?

If the latter, then you can simply use the n+1X media filter n+1or mode, and set the angular coefficients to zero, and you will get the desired effect.

, , memcmp() , , . , , , , .

, , , :

memcpy() memmove() , ?

, .

, , openCV, , . , , - 32-, .

+1

, , Hough OpenCV :

import cv2
import numpy as np
img = cv2.imread(img,0)
laplacian = cv2.Laplacian(img,cv2.CV_8UC1) # Laplacian Edge Detection
minLineLength = 900
maxLineGap = 100
lines = cv2.HoughLinesP(laplacian,1,np.pi/180,100,minLineLength,maxLineGap)
for line in lines:
    for x1,y1,x2,y2 in line:
        cv2.line(img,(x1,y1),(x2,y2),(255,255,255),1)
cv2.imwrite('Written_Back_Results.jpg',img)
0

javacv.

com.test11;

import org.opencv.core.*;
import org.opencv.imgproc.Imgproc;
import org.opencv.imgcodecs.Imgcodecs;

public class GetVerticalOrHorizonalLines {

    static{ System.loadLibrary(Core.NATIVE_LIBRARY_NAME); }

    public static void main(String[] args) {

        //Canny process before HoughLine Recognition

        Mat source = Imgcodecs.imread("src//data//bill.jpg");
        Mat gray = new Mat(source.rows(),source.cols(),CvType.CV_8UC1);
        Imgproc.cvtColor(source, gray, Imgproc.COLOR_BGR2GRAY);

        Mat binary = new Mat();
        Imgproc.adaptiveThreshold(gray, binary, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 15, -2);
        Imgcodecs.imwrite("src//data//binary.jpg", binary);

        Mat horizontal = binary.clone();
        int horizontalsize = horizontal.cols() / 30;
        int verticalsize = horizontal.rows() / 30;

        Mat horizontal_element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(horizontalsize,1));
        //Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(3,3));
        Imgcodecs.imwrite("src//data//horizontal_element.jpg", horizontal_element);

        Mat Linek = Mat.zeros(source.size(), CvType.CV_8UC1);
        //x =  Imgproc.morphologyEx(gray, dst, op, kernel, anchor, iterations);
        Imgproc.morphologyEx(gray, Linek,Imgproc.MORPH_BLACKHAT, horizontal_element);
        Imgcodecs.imwrite("src//data//bill_RECT_Blackhat.jpg", Linek);

        Mat vertical_element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(1,verticalsize));
        Imgcodecs.imwrite("src//data//vertical_element.jpg", vertical_element);

        Mat Linek2 = Mat.zeros(source.size(), CvType.CV_8UC1);
        //x =  Imgproc.morphologyEx(gray, dst, op, kernel, anchor, iterations);
        Imgproc.morphologyEx(gray, Linek2,Imgproc.MORPH_CLOSE, vertical_element);
        Imgcodecs.imwrite("src//data//bill_RECT_Blackhat2.jpg", Linek2);

            }
    }
-1

.

package com.test12;

import org.opencv.core.*;
import org.opencv.imgproc.Imgproc;
import org.opencv.imgcodecs.Imgcodecs;

public class ImageSubstrate {

    static{ System.loadLibrary(Core.NATIVE_LIBRARY_NAME); }

    public static void main(String[] args) {

           Mat source = Imgcodecs.imread("src//data//bill.jpg");

           Mat image_h = Mat.zeros(source.size(), CvType.CV_8UC1);
           Mat image_v = Mat.zeros(source.size(), CvType.CV_8UC1); 

           Mat output = new Mat();
           Core.bitwise_not(source, output);
           Mat output_result = new Mat();

           Mat kernel_h = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(20, 1));
           Imgproc.morphologyEx(output, image_h, Imgproc.MORPH_OPEN, kernel_h);
           Imgcodecs.imwrite("src//data//output.jpg", output);  

           Core.subtract(output, image_h, output_result);
           Imgcodecs.imwrite("src//data//output_result.jpg", output_result);    


           Mat kernel_v = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(1, 20));   
           Imgproc.morphologyEx(output_result, image_v, Imgproc.MORPH_OPEN, kernel_v);
           Mat output_result2 = new Mat();

           Core.subtract(output_result, image_v, output_result2);          
           Imgcodecs.imwrite("src//data//output_result2.jpg", output_result2);
    }
}
-1

All Articles