Using the Haar Classifier for foraminifera images


In [1]:
import cv2                             
import matplotlib.pyplot as plt                     # Import modules
%matplotlib inline
import numpy as np

IMG= cv2.imread('Ngapali.jpg')                      # read the image 
IMG=cv2.cvtColor(IMG, cv2.COLOR_BGR2RGB)

                                                    # aply the classifier
Cascade = cv2.CascadeClassifier('_02_cascade_05_1000_625_19stages.xml')
Objects = Cascade.detectMultiScale(IMG)          

IMG2= cv2.imread('Ngapali.jpg')                     # read the image in second var
IMG2 = cv2.cvtColor(IMG2,cv2.COLOR_BGR2RGB)         # to mark the found objects on it

centres=[]
Radi=[]                                             # draw a rectangle around detected Objects
for (x, y, w, h) in Objects:                        
    cv2.rectangle(IMG2, (x, y), (x+w, y+h), (0, 255, 0), 2)
    centres.append( (int(x+w/2.),int(y+h/2.) ) )    # calculate centerpoints
    Radi.append( np.sqrt((w/2.)**2+(h/2.)**2 ))     # calculate radius
                
plt.figure(figsize=(20,10))                         # show the result
plt.imshow(IMG2,cmap='gray')    
plt.show()

print ' {} foram objekts detected'.format(len(Objects))


 50 foram objekts detected

Draw the centerpoints of the found objects on the image


In [2]:
IMG2=cv2.imread('Ngapali.jpg')
IMG2=cv2.cvtColor(IMG2, cv2.COLOR_BGR2RGB)

for i in range(len(centres)):
    cv2.circle(IMG2, centres[i], 3, (255, 0, 0), -1)

plt.figure(figsize=(20,10))        
plt.imshow(IMG2,cmap='gray')    
plt.show()


... further processing to get the shapes of the objects (e.g. region growing+findContours, snakes, ...)


In [ ]: