OpenCV-Python系列·第二十五集:检测瞳孔

Tip:普通的轮廓检测。

# -*- 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()

 

  • 0
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值