Detect or Remove Lines From Image Using OpenCV With Spring Boot

Hein Htet Zaw
3 min readNov 19, 2020

In this tutorial, we are going to build a spring boot application that can remove or detect grid lines(horizontal,vertical lines) from image file for extracting data with tesseract(Tesseract doesn’t work with table) or something like that.. You can see result at the end of post.To achieve this goal, we are going to follow these steps:

Step 1 : Create a Spring Boot Application

Step 2 :Apply OpenCv To Uploaded Image

2.1 Load the image

2.2 Convert RGB to Gray image

2.3 Detect and get points of horizontal and vertical lines

2.4 Overwrite lines on the image

Step 3 : Test Application

Step 1: Create a Spring Boot Application

Go to the Spring Initializr website and download the project.

Dependencies we are going to use:

  1. OpenCV

opencv can be used to modify our image. To use OpenCV package we need to have libopencv_javaxx0.so in your java library folder like /usr/lib. To get this so file you need to install opencv on your machine.

sudo pacman -S opencv 

Then you can check your library.

sudo ls -la /usr/lib/libopencv_java*

In some case when you got error like that you need to link that library.

sudo ln -s /usr/lib/libopencv_java450.so /usr/lib/libopencv_java430.so

2. Spring Boot Starter Web

spring-boot-starter-web, will import all packages and classes to handle the web project (as an API rest).

Finally… let’s start coding…

Let’s open our Spring Boot project that you downloaded using VSCode. In this tutorial we are going to use VSCode as IDE, so make sure that you have Spring Boot support in vs code or else please follow this Link.

1. Firstly we need to add above dependencies in our pom.xml file.

2. Now we will create a new controller class that will handle our REST requests:

In this class, we create post routes /api/image/getLinesRemovedImage that will return base64 String of lines removed image and /api/image/getLinesDetecedImage that will return base64 string of lines detected image. Both routes will accept a image file with param name imagefile. Then we pass the uploaded file to a service, called Morphology_3Run class that we will implement in next steps.

Step 2: Apply OpenCv To Uploaded Image

2.1 Load the image

2.2 Convert RGB to Gray image

2.3 Detect and get points of horizontal and vertical lines

2.4 Overwrite lines on the image

Complete code is as below

In this method, we perform require operations for lines removing and detection. Line color should change according to your image color and kernels’ size also should change according to your lines size.

Step 3: Test the application

You can test the application with a POST call to the following endpoint uploading an image file:

http://localhost:8080/api/image/getLinesRemovedImage
http://localhost:8080/api/image/getLinesDetecedImage

You can use Postman, SoapUI or the Rest client you prefer.

Note: Install and link commands are examples of arch linux. These will depend on your operating system.

If you are looking for that kind of program in python, you can check here.

That’s all folks!

--

--