Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I had same @Chris's code, w/out using MorphologyEx and substraction. But never 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)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output: bottles

horizontalStructure = cv2.getStructuringElement(cv2.MORPH_RECT, (1,3)) Output; bottles2

I had same @Chris's code, w/out image. W/out using MorphologyEx and substraction. But never 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)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output: bottles

horizontalStructure = cv2.getStructuringElement(cv2.MORPH_RECT, (1,3)) Output; bottles2

I had same @Chris's image. W/out using MorphologyEx and substraction. But never 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)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output: bottles

horizontalStructure = cv2.getStructuringElement(cv2.MORPH_RECT, (1,3)) Output; bottles2

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)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output: bottles

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

Output; bottles2