Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Closing small holes in the binary image with opencv

'''Closing small holes in Opencv can be done using morphological 
operation method, cv2.morphologyEx() with cv2.MORPH_CLOSE'''
import cv2 
img = cv2.imread("myimage.png")
'''Assuming image "myimage.png" has small holes in it, such as the
example in this Link: https://freesvg.org/june-25-black-white-circles;
the black and whites circles are the foreground and the rest is the 
background.'''
kernelSize = (7,7) # as the kernetsize increase more holes are closed.
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, kernelSize)
'''kernelSize is the filter size (must be odd) and cv2.MORPH_RECT
specifies that the shape of the kernel should be a rectangle (other shapes
are available). The getStructuringElement() generate the kernel.'''
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) # convert to gray scale
BinryImg = cv2.threshold(gray,20,255,cv2.THRESH_BINARY)[1]# convert to-
#binary image.
closing = cv2.morphologyEx(converToBinaryImg, cv2.MORPH_CLOSE, kernel)
cv2.imshow("outputmyimg",closing)
Comment

PREVIOUS NEXT
Code Example
Python :: replace python enter number of characters 
Python :: scale just one column pandas 
Python :: find mean of list python 
Python :: image name validate using regex python 
Python :: change between two python 3 version in raspberrry pi 
Python :: how to choose a random key from a dictionary in python 
Python :: pandas set index integer not float 
Python :: hover 777-286 
Python :: pydrive download file 
Python :: create canvas for signature flutter 
Python :: rebuild database from zero django postgres 
Python :: iptc text classification example 
Python :: site:github.com python ssh 
Python :: discord.py clear 
Python :: python script superuser 
Python :: na.kalman in python 
Python :: 7616*75 
Python :: Empty a variable without destroying it 
Python :: dorp ligne in df where values equal zeros 
Python :: Return a new RDD containing the distinct elements in this RDD. 
Python :: Filters rows using the given condition 
Python :: numpy stack in new dimension 
Python :: how to pairwise permute in python 
Python :: ejercicios con def en python 
Python :: how to find all the installed packages in python 
Python :: python run subprocess and get output 
Python :: urllib.error.HTTPError: HTTP Error 502 docker redis 
Python :: introduction to sets python3 
Python :: saaaaaaaaaaaaa 
Python :: response.url SSL warning python 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =