-
Notifications
You must be signed in to change notification settings - Fork 0
/
morphology.py
33 lines (24 loc) · 908 Bytes
/
morphology.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# -*- coding: utf-8 -*-
"""q1.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1YqwCaSC-X_rxbMQZ54J7r4LQODV1M_Ss
"""
import cv2
import numpy as np
img = cv2.imread('Q1/Q1image.png')
cv2.imshow('image is', img)
cv2.waitKey()
cv2.destroyAllWindows()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
(_, binary_img) = cv2.threshold(gray, 100, 255, cv2.THRESH_BINARY)
"""Creating and elliptical kernel (circle is not available)"""
kern = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(40,40))
'''Applying opening morphology on the image using the elleptical kernelccv'''
BW2 = cv2.morphologyEx(binary_img, cv2.MORPH_OPEN, kern)
cv2.imshow('output', BW2)
cv2.waitKey()
cv2.destroyAllWindows()
"""Counting the number of circles using inbuilt function"""
count, C = cv2.connectedComponents(BW2)
print("Total number of circles are:",count-1)