Home > OS >  Subtracting a line from an image using OpenCV
Subtracting a line from an image using OpenCV

Time:08-05

So being new to OpenCV, I'm trying to detect a part of the image ( the thick line between the "PSSU" and "356750 / 22G1" characters ) and then subtract it from the original image. I want to have a clean final image that I can then put through OCR.

enter image description here

I've managed to detect the line ( red highlights ). The code for line detection is shown below :

enter image description here

import cv2
import numpy as np
 
# Reading the required image in
# which operations are to be done.
# Make sure that the image is in the same
# directory in which this python program is
img = cv2.imread('c:\\ml\\test.jpg')
 
# Convert the img to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 
# Apply edge detection method on the image
edges = cv2.Canny(gray, 50, 150, apertureSize=3)
 
# This returns an array of r and theta values
# 4th value ( 400 ) is threshold of how thick the line is thats to be detected.   Higher value = thicker line to be detected.
lines = cv2.HoughLines(edges, 1, np.pi/180, 400)
 
# The below for loop runs till r and theta values
# are in the range of the 2d array
for r_theta in lines:
    arr = np.array(r_theta[0], dtype=np.float64)
    r, theta = arr
    # Stores the value of cos(theta) in a
    a = np.cos(theta)
 
    # Stores the value of sin(theta) in b
    b = np.sin(theta)
 
    # x0 stores the value rcos(theta)
    x0 = a*r
 
    # y0 stores the value rsin(theta)
    y0 = b*r
 
    # x1 stores the rounded off value of (rcos(theta)-1000sin(theta))
    x1 = int(x0   1000*(-b))
 
    # y1 stores the rounded off value of (rsin(theta) 1000cos(theta))
    y1 = int(y0   1000*(a))
 
    # x2 stores the rounded off value of (rcos(theta) 1000sin(theta))
    x2 = int(x0 - 1000*(-b))
 
    # y2 stores the rounded off value of (rsin(theta)-1000cos(theta))
    y2 = int(y0 - 1000*(a))
 
    # cv2.line draws a line in img from the point(x1,y1) to (x2,y2).
    # (0,0,255) denotes the colour of the line to be
    # drawn. In this case, it is red.
    cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2)
 
# All the changes made in the input image are finally
# written on a new image houghlines.jpg
cv2.imwrite('linesDetected.jpg', img)

So how do I now subtract the line ( red highlights ) from the original image?

Thank you.

CodePudding user response:

Having (x1, y1) and (x2, y2) you can slice the image in two parts like:

img_left = img[0:x1, 0:y1]
img_right = img[0:x2, 0:y2]

And then join them back:

final_img = np.concatenate((img_left, img_right), axis=1)
  • Related