# Remove Grid in the Floor Plan

**URL:** <https://forum.opencv.org/t/remove-grid-in-the-floor-plan/12720>\
**Category:** Python\
**Tags:** imgproc\
**Created:** [April 7, 2023, 8:01pm UTC](https://forum.opencv.org/t/remove-grid-in-the-floor-plan/12720 "2023-04-07T20:01:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![RonV](https://avatars.discourse-cdn.com/v4/letter/r/c77e96/32.png) [@RonV](https://forum.opencv.org/u/RonV)\
**Post date:** [April 7, 2023, 8:01pm UTC](https://forum.opencv.org/t/remove-grid-in-the-floor-plan/12720/1 "2023-04-07T20:01:49Z")

</div>

Hi all,

I’m trying to remove all the grid lines from this floorplan to extract the text on the floor plan such as RPP A11, A12. I have tried this code so far:

import cv2  
import numpy as np  
import sys  
import pickle  
import pytesseract

image = cv2.imread(‘images/test.jpg’)  
image1 = cv2.cvtColor(image,cv2.COLOR\_BGR2GRAY)  
#image = cv2.cvtColor(image1,cv2.COLOR\_BGR2HSV)  
ret,thresh = cv2.threshold(image1,130,240,0)

Any ideas or thoughts would be appreciated.

Additional Info: I tried findcontours function and return thousand of contours. What would be the best way to select the contours that I’m interested among thousand of them?

 ![test](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/2/2e5091ed2adc5caf65cd43ee6cca71d8a6cede53.jpeg)

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [April 7, 2023, 9:50pm UTC](https://forum.opencv.org/t/remove-grid-in-the-floor-plan/12720/2 "2023-04-07T21:50:32Z")

</div>

[crosspost](https://meta.stackoverflow.com/a/266159):

> <https://stackoverflow.com/questions/75955486/opencv-remove-gridline-and-read-text-in-the-images>

---

<div class="post-metadata">

**Author:** ![RonV](https://avatars.discourse-cdn.com/v4/letter/r/c77e96/32.png) [@RonV](https://forum.opencv.org/u/RonV)\
**Post date:** [April 7, 2023, 10:42pm UTC](https://forum.opencv.org/t/remove-grid-in-the-floor-plan/12720/3 "2023-04-07T22:42:35Z")

</div>

UPDATED: I’m able to remove horizontal and vertical lines but seem like it is not reading correct on the text.

Here is the Updated Code:

```auto
import cv2
import numpy as np
import sys
import pickle
import pytesseract
image = cv2.imread('images/test.jpg')
image1 = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
gray = cv2.bitwise_not(image1)
bw = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, \
                                cv2.THRESH_BINARY, 19, -11)
horizontal = np.copy(bw)
vertical = np.copy(bw)
cols = horizontal.shape[1]
rows = vertical.shape[0]

verticalsize = rows // 30
horizontal_size = cols // 30

horizontalStructure = cv2.getStructuringElement(cv2.MORPH_RECT, (horizontal_size, 1))

detected_horizontal_lines = cv2.morphologyEx(bw, cv2.MORPH_OPEN, horizontalStructure, iterations=2)
cnts = cv2.findContours(detected_horizontal_lines, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    cv2.drawContours(image, [c], -1, (255,255,255), 2)

verticalStructure = cv2.getStructuringElement(cv2.MORPH_RECT, (1, verticalsize))
detected_vertical_lines = cv2.morphologyEx(bw, cv2.MORPH_OPEN, verticalStructure, iterations=2)
cnts = cv2.findContours(detected_vertical_lines, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    cv2.drawContours(image, [c], -1, (255,255,255), 2)

pytesseract.pytesseract.tesseract_cmd = ('C:\Program Files\Tesseract-OCR\/tesseract.exe')
text = pytesseract.image_to_string(image)

print(text)
cv2.imshow('net',image)
cv2.waitKey(0)

```

Here is the result:

 ![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/8/8a64609f767df03b37bfc5dbd9bffc7a31b56bc4.jpeg)
