Ask Your Question

Process image to get rid of line between two components

asked 2019-04-19 09:59:13 -0500

Equintox gravatar image

Hello guys, currently i work on a project that uses image processing to crop out multiple bottles out of one image. In some of the images, the bottles are connected by a line (it is the edge of the table).

Sample image:

image description

Does anyone have a idea how i could get rid of this horizontal line between each bottle? My goal is to get only get the outline of each bottle.

I am thankful for any help :)

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2019-04-19 10:35:34 -0500

If I do an open morph operation with MorphologyEx using a rect kernel that is 12 x 1, I get this image

image description

If I subtract this from the original I get

image description

which is close. Before subtracting, you could find all the contours in the morphed image and filter to only include the table edge by looking at the y of the contour in the image and maybe the aspect ratio of the contour. After filtering, you could then dilate the remaining contours a little before subtracting.

edit flag offensive delete link more


Thank you a lot for your help ! i got it working now :)

Equintox gravatar imageEquintox ( 2019-05-15 08:21:36 -0500 )edit

answered 2019-04-21 10:10:12 -0500

supra56 gravatar image

updated 2019-04-21 10:17:06 -0500

I had same @Chris's image. W/out using MorphologyEx and substraction. But never used it. Here is code:

#!/usr/bin/env python3
#OpenCV 4.0.1
#Date: 20th April, 2019

import cv2
import numpy as np

img = cv2.imread("bottles.png")
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
res = cv2.resize(gray, (640, 480))

adapt_thresh = cv2.adaptiveThreshold(res, 255, cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,15,-2)
horizontal = adapt_thresh.copy()

horizontalStructure = cv2.getStructuringElement(cv2.MORPH_RECT, (1,2))
horizontal = cv2.erode(horizontal, horizontalStructure,  (-1, -1))
horizontal = cv2.dilate(horizontal, horizontalStructure,  (-1, -1))

numpy_horizontal = np.hstack((res, horizontal))
numpy_horizontal_concat = np.concatenate((res, horizontal), axis=1)

cv2.imshow("bottles", numpy_horizontal_concat)
cv2.imwrite("bottles2.jpg", numpy_horizontal_concat)

Output: bottles

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

Output; bottles2

edit flag offensive delete link more


The first image bottle 1 is not straight line. The rest are straight lines

supra56 gravatar imagesupra56 ( 2019-04-21 10:12:45 -0500 )edit

thanks you so much ! i got it working now :)

Equintox gravatar imageEquintox ( 2019-05-15 08:21:14 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2019-04-19 09:59:13 -0500

Seen: 225 times

Last updated: Apr 21 '19