This answer was inspired by this excellent post.
import numpy as np
import cv2
if __name__ == '__main__':
image = cv2.imread('image.png',cv2.IMREAD_UNCHANGED)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret,binary = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY)
binary = cv2.bitwise_not(binary)
H = cv2.Sobel(binary, cv2.CV_8U, 0, 2)
V = cv2.Sobel(binary, cv2.CV_8U, 2, 0)
rows,cols = image.shape[:2]
_,contours,_ = cv2.findContours(V, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
(x,y,w,h) = cv2.boundingRect(cnt)
# rows/3 is the threshold for length of line
if h > rows/3:
cv2.drawContours(V, [cnt], -1, 255, -1)
cv2.drawContours(binary, [cnt], -1, 255, -1)
else:
cv2.drawContours(V, [cnt], -1, 0, -1)
_,contours,_ = cv2.findContours(H, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
(x,y,w,h) = cv2.boundingRect(cnt)
# cols/3 is the threshold for length of line
if w > cols/3:
cv2.drawContours(H, [cnt], -1, 255, -1)
cv2.drawContours(binary, [cnt], -1, 255, -1)
else:
cv2.drawContours(H, [cnt], -1, 0, -1)
kernel = cv2.getStructuringElement(shape=cv2.MORPH_RECT, ksize=(3,3))
H = cv2.morphologyEx(H, cv2.MORPH_DILATE, kernel,iterations = 3)
V = cv2.morphologyEx(V, cv2.MORPH_DILATE, kernel, iterations = 3)
cross = cv2.bitwise_and(H, V)
_,contours,_ = cv2.findContours(cross,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
centroids = []
for cnt in contours:
mom = cv2.moments(cnt)
(x,y) = mom['m10']/mom['m00'], mom['m01']/mom['m00']
cv2.circle(image,(int(x),int(y)),4,(0,255,0),-1)
centroids.append((x,y))
centroids.sort(key = lambda x: x[0], reverse = False)
centroids.sort(key = lambda x: x[1], reverse = False)
dx = int(centroids[1][0] - centroids[0][0])
centroids = np.array(centroids, dtype = np.float32)
(x,y,w,h) = cv2.boundingRect(centroids)
if x-dx > -5: x = max(x-dx,0)
if h+dx <= rows+5: h = min(h+dx,rows)
if w+dx <= cols+5: w = min(w+dx,cols)
cv2.rectangle(image, (x,y), (x+w,y+h), (0,255,0))
roi = binary[y:y+h,x:x+w]
roi = cv2.morphologyEx(roi, cv2.MORPH_OPEN, kernel,iterations = 1)
cv2.imshow('image', image)
cv2.imshow('roi', roi)
cv2.waitKey(0)
cv2.destroyAllWindows()




OpenCV
docs.opencv.org › 4.13.0 › d9 › d61 › tutorial_py_morphological_ops.html
OpenCV: Morphological Transformations
void morphologyEx(InputArray src, OutputArray dst, int op, InputArray kernel, Point anchor=Point(-1,-1), int iterations=1, int borderType=BORDER_CONSTANT, const Scalar &borderValue=morphologyDefaultBorderValue())
Python Geeks
pythongeeks.org › python geeks › learn opencv › morphological operations in opencv
Morphological Operations in OpenCV - Python Geeks
February 14, 2023 - # Importing OpenCV import cv2 # Importing numpy import numpy as np # Reading the image img = cv2.imread(r"C:\Users\tushi\Downloads\PythonGeeks\dots.jpg", 0) # Displaying the image cv2.imshow('Original',img) cv2.waitKey(0) cv2.destroyAllWindows() # Defining the kernel kernel = np.ones((5,5),np.uint8) # Apply the opening morphological operation using cv2.morphologyEx() function img_opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel) # Displaying the image after the opening morphological operation cv2.imshow('Opening',img_opening) cv2.waitKey(0) cv2.destroyAllWindows()
TutorialsPoint
tutorialspoint.com › opencv › opencv_morphological_operations.htm
OpenCV - Morphological Operations
In addition to these two, OpenCV has more morphological transformations. The morphologyEx() of the method of the class Imgproc is used to perform these operations on a given image.
Python Programming
pythonprogramming.net › morphological-transformation-python-opencv-tutorial
Morphological Transformations OpenCV Python Tutorial
cap = cv2.VideoCapture(1) while(1): _, frame = cap.read() hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) lower_red = np.array([30,150,50]) upper_red = np.array([255,255,180]) mask = cv2.inRange(hsv, lower_red, upper_red) res = cv2.bitwise_and(frame,frame, mask= mask) kernel = np.ones((5,5),np.uint8) opening = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel) closing = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel) cv2.imshow('Original',frame) cv2.imshow('Mask',mask) cv2.imshow('Opening',opening) cv2.imshow('Closing',closing) k = cv2.waitKey(5) & 0xFF if k == 27: break cv2.destroyAllWindows() cap.release()
EDUCBA
educba.com › home › software development › software development tutorials › programming languages tutorial › opencv morphology
OpenCV Morphology | How to work Morphology function in OpenCV?
April 18, 2023 - We make use of the operation MORPH_OPEN in morphologyEx() function to perform opening morphological operations on a given image.
Call +917738666252
Address Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
Master Data Science
datahacker.rs › 006-morphological-transformations-with-opencv-in-python
#006 Morphological transformations with OpenCV in Python
August 15, 2020 - # Performing opening and closing opening_1 = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel_1) opening_2 = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel_2) opening_3 = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel_3) closing_1 = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel_1) closing_2 = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel_2) closing_3 = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel_3)
Tottori-u
labs.eecs.tottori-u.ac.jp › sd › Member › oyamada › OpenCV › html › py_tutorials › py_imgproc › py_morphological_ops › py_morphological_ops.html
モルフォロジー変換 — OpenCV-Python Tutorials 1 documentation
opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
GitHub
github.com › opencv › opencv › issues › 10571
Python: Incorrect result in cv2.morphologyEx() with cv2.MORPH_CLOSE · Issue #10571 · opencv/opencv
January 10, 2018 - res_img = (cv2.morphologyEx(img.copy(), cv2.MORPH_CLOSE, np.ones((4,4), np.uint8))) * filled_img
Author opencv



