OpenCV-Python Series · Episode 25: Detection of the nose hole

Tip: Ordinary contour detection.

# -*- coding: utf-8 -*-
"""
Created on Tue Sep 4 20:25:59 2018

@author: Miracle
"""
#检测瞳孔
Import cv2
Import math
#开图片
Img = cv2.imread('../data/lena.jpg')
#缩缩尺寸
Scaling_factor = 0.85

Img = cv2.resize(img,None,
               Fx = scaling_factor,
               Fy = scaling_factor,
               Interpolation = cv2.INTER_AREA)

Cv2.imshow('Original Image',img)
Gray = cv2.cvtColor(~img,cv2.COLOR_BGR2GRAY)
#cv2.imshow('Gray Image',gray)

Rett,thresh_gray = cv2.threshold(gray,150,255,cv2.THRESH_BINARY)
#cv2.imshow('thresh_gray Image',thresh_gray)
Thresh_gray, contours, hierarchy = cv2.findContours(thresh_gray,
                                      cv2.RETR_EXTERNAL,
                                      cv2.CHAIN_APPROX_NONE)
#cv2.imshow('thresh_gray image',thresh_gray)

For contour in contours:
    Area = cv2.contourArea(contour)
    Rect = cv2.boundingRect(contour)
    x,y,width,height = rect
    Radius = 0.25*(width+height)
    
    Area_condition = (100 <= area <= 200)
    Symetry_condition = (abs(1-float(width)/float(height))<=0.2)
    Fill_condition = (
            Abs(1-(area/(math.pi*math.pow(radius,2.0))))))<=0.3)
    
    If area_condition and symetry_condition and fill_condition:
        Cv2.circle(img,
                   (int(x+radius), int(y+radius)),
                   Int(1.3*radius),
                   (0,0,255),
                   -1)
        
Cv2.imshow('Pupil',img)
If cv2.waitKey(0) == 27:
    Cv2.destroyAllWindows()