OpenCV Features of Contours

I am working in Bio-Informatics field. I am in deep of Pathological Myopia Detection and I have a fundus images dataset. One of my tasks is to extract Optic disk from the fundus images which I have done using the UNet model. Now my task was to extract the peripapillary atrophy(PPA) around the Optic disk. For this purpose, I did a lot of research on the Internet and I found many solutions to tackle this problem but the one I love the most is Contours Features. Following is the short intro to Contours features.

First of all, you need to install OpenCV. If you have already installed OpenCV then skip this step. To install OpenCV open your terminal and type the following command:

pip3 install opencv-python

Now, let’s move towards some coding stuff. First of import OpenCV and NumPy and read your desired image and also am resizing my image to 400×400 as follows.

import cv2
import numpy as np
img1 = cv2.imread('myFundImage.jpg',0)
img = cv2.resize(img1, (400,400))

mainImage

Optic disk mask which is extracted

Now let’s take a look at moments of images. Image moments help you to calculate some features like center of the object, area of the object etc.

retrn,thresh = cv2.threshold(img,100,255,0)
img2,contours,hierarchy = cv2.findContours(thresh, 1, 2)
contour = contours[0]
moment = cv2.moments(contour) # return a dictionary
print(moment)

In the moment dictionary you will get results like this:

{'m00': 7453.5, 'm10': 1388718.3333333333, 'm01': 1500503.6666666665,
 'm20': 261806340.41666666, 'm11': 281170749.625, 'm02': 309296104.4166666, 
 'm30': 49921283181.0, 'm21': 53300759774.35, 'm12': 58269216643.25, 'm03': 
 65174258363.8, 'mu20': 3063654.519288093, 'mu11': 1600487.1799890995, 'mu02': 
 7221742.884485662, 'mu30': 524998.2130661011, 'mu21': -1262836.5068426132, 
 'mu12': -2500589.735739231, 'mu03': 522250.7371826172, 'nu20': 0.05514666807803505,
 'nu11': 0.028809232477857424, 'nu02': 0.1299934621506166, 'nu30': 0.00010946043902924837,
 'nu21': -0.00026329735039260145, 'nu12': -0.0005213649180013221, 'nu03': 
  0.0001088875991434285}

From the moments dictionary, you can get useful information like area, centroid, etc.
Now let’s draw a circle around the thresholded area and find the circumcircle of an object using the OpenCV function cv2.minEnclosingCircle(). It will draw a circle which completely covers the object according to the value of the radius.

(x,y),radius = cv2.minEnclosingCircle(contour)
center = (int(x),int(y))
radius = int(radius)
circle_thik = 2
circle_color = (0,0,0) #black color of circle
final_image = cv2.circle(img,center,radius,circle_color,circle_thik)

Let’s show the final circled mask.

cv2.imshow('detected circles',final_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

cicleDraw

After drawing Contour Feature Cicle

Now I have some disturbing image of the optic disk and I want to fit an ellipse to that optic disk. It returns the rotated rectangle in which the ellipse is inscribed. Following is the code snippet to fit ellipse:

ellipse = cv2.fitEllipse(contour)
ellipse_thik = 2
ellipse_color = (0,0,0) #black color of ellipse
final_image = cv2.ellipse(img,ellipse,ellipse_color,ellipse_thik)

Following is the ellipse fitted image on the disturbed optic disk.

main

After drawing Contour Feature Ellipse

Now let’s try to adjust rectangles to our images. In OpenCV minAreaRect() is a function that is used for rectangles. It returns a 2D structure that contains the center, width, height, angle of rotation, etc. Now to draw the rectangle, four corners of the rectangle are required which is obtained by the function boxPoints(). Following is the code snippet to fit a rectangle:

rectangle = cv2.minAreaRect(contour)
box = cv2.boxPoints(rectangle)
box = np.int0(box)
rectangle_image = cv2.drawContours(img,[box],0,(0,0,0),2)

Following is the rectangle fitted image on the optic disk.

rectangle

After drawing Contour Feature Rectangle

There are many other functions like fitting a line, check convexity, convex hull, contour approximation, etc that you can try by yourself.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.