How to Remove Grid Lines on Image in Python

how to remove grid lines on image in python?

Apparently something in the background changes the style. I have no experience whatsoever with google colab ti judge whether this can be responsible for the observed difference in displayed image.

In any case it should be possible to manually turn the grid lines off on a per notebook basis.

%matplotlib inline
from matplotlib import pyplot as plt
plt.rcParams["axes.grid"] = False

# rest of code

Removing grid from scanned image

I have a solution using OpenCV.

First, I inverted the image:

ret,thresh2 = cv2.threshold(img,127,255,cv2.THRESH_BINARY_INV)

Sample Image

Now I performed morphological opening operation:

opening = cv2.morphologyEx(thresh2, cv2.MORPH_OPEN, k2)
cv2.imshow('opening', opening)

Sample Image

You can see that the grid lines have disappeared. But there are some gaos in some of the characters as well. So to fill the gaps I performed morphological dilation operation:

dilate = cv2.morphologyEx(opening, cv2.MORPH_DILATE, k1)
cv2.imshow('dilation', dilate)

Sample Image

You can check out THIS LINK for more morphological operations and kernels used.

How to remove a particular grid line?

We can catch all gridlines with get_ygridlines(), then access individual gridlines as Line2D objects to modify them:

from matplotlib import pyplot as plt
from matplotlib.ticker import AutoMinorLocator

plt.rcParams['font.family'] = 'Arial'
fig, ax = plt.subplots(figsize=(14.78, 9.84))
plt.xlim(0, 105)
plt.ylim(0, 10)
ax.xaxis.set_minor_locator(AutoMinorLocator(2))
ax.yaxis.set_minor_locator(AutoMinorLocator(2))

ax.spines['bottom'].set_linewidth(1.5)
ax.spines['left'].set_linewidth(1.5)
ax.spines['top'].set_linewidth(0)
ax.spines['right'].set_linewidth(0)
# Grid setting
plt.grid(True, color='#0100FF', which="major", ls="-")
plt.grid(True, color='#0BC904', which="minor", ls="-")

#this part is added
#set the last horizontal gridline invisible
ygridlines = ax.get_ygridlines()
gridline_of_interest = ygridlines[-1]
gridline_of_interest.set_visible(False)

plt.xlabel("Indicator Amplitude, %FSH", fontsize=28, labelpad=15)
plt.ylabel("Function Generator Output, V", fontsize=28, labelpad=15)
# Axis setting
plt.tick_params(which="major", labelsize=22, length=10, pad=10, width=1.5)
plt.tick_params(which="minor", length=8, width=1.5)
# Plot scatter & line
FSH_axis = [10, 40, 100]

plt.plot(FSH_axis, [1, 3, 2], color='black', marker='^', linewidth=1.5, markersize=8, label="40 dB")
plt.plot(FSH_axis, [2, 2, 3], color='red', marker='o', linewidth=1.5, markersize=8, label="60 dB")
plt.plot(FSH_axis, [2, 1, 1], color='blue', marker='v', linewidth=1.5, markersize=8, label="80 dB")

plt.legend(loc=(1 / 16, 58 / 90), ncol=1, fontsize=20, frameon=True, framealpha=1, edgecolor="black")
plt.show()

Sample output:
Sample Image

Of course, the corresponding get_xgridlines() also exists.

Removing horizontal and vertical grids in sudoku image


    1. Detect the line, using fastLineDetector




    1. Set length threshold




    1. Draw the line the same as the background.

Output:



  • Sample Image

Code:



import cv2

gray = cv2.imread("gray.png", cv2.IMREAD_GRAYSCALE)
lines = cv2.ximgproc.createFastLineDetector(_length_threshold=15).detect(gray)

if lines is not None:
for line in lines:
(x_start, y_start, x_end, y_end) = line[0]
cv2.line(gray, (x_start, y_start), (x_end, y_end), (172, 172, 172), thickness=4)

cv2.imwrite("gray_result.png", gray)
cv2.imshow("result", gray)
cv2.waitKey(0)
cv2.destroyAllWindows()

We first check if the lines are detected:

if lines is not None:

If the lines are detected, then get the coordinates:

(x_start, y_start, x_end, y_end) = line[0]

Then draw line:

cv2.line(gray, (x_start, y_start), (x_end, y_end), (172, 172, 172), thickness=4)

You can change the thickness of the line, For instance, if you set the thickness to 10.

cv2.line(gray, (x_start, y_start), (x_end, y_end), (172, 172, 172), thickness=10)

Output:

Sample Image

Filter out small grid lines from an image using python

You can use the opening or closing of your image (depending if you are using the normal or inverted image). Opening will first erode your image and then dilate it. This will remove small/thin objects assuming bright objects over black background.

For example, in the case of your inverted image, use

out = cv2.morphologyEx(src, MORPH_OPEN) 

For more information check out this tutorial

How to hide axes and gridlines in Matplotlib (python)

# Hide grid lines
ax.grid(False)

# Hide axes ticks
ax.set_xticks([])
ax.set_yticks([])
ax.set_zticks([])

Note, you need matplotlib>=1.2 for set_zticks() to work.



Related Topics



Leave a reply



Submit