Opencv 检测瞳孔,但我试图从在线库存图片中检测虹膜

如何解决Opencv 检测瞳孔,但我试图从在线库存图片中检测虹膜

highreseye

highresoutput

output input 我正在尝试隔离虹膜,但瞳孔被隔离成一个圆圈,我该如何更改它使其围绕虹膜而不是瞳孔。我也使用股票 jpeg 文件。我尝试了很多东西,但我对 opencv 和图像处理真的很陌生,所以任何帮助都会令人钦佩。此外,在某些图像中,它在非常奇怪的地方形成了一个圆圈,这让我觉得代码还有其他问题。


#import numpy
import cv2
import numpy as np

class pupil_detection():
    def __init__(self,image_path):
        '''
        initialize the class and set the class attributes
        '''
        self._img = None
        self._img_path = image_path
        self._pupil = None
        self._centroid = None

    def load_image(self):
        '''
        load the image based on the path passed to the class
        it should use the method cv2.imread to load the image
        it should also detect if the file exists
        '''
        self._img = cv2.imread(self._img_path)
        #self._img = cv2.resize(self._img,(300,300))
        # If the image doesn't exists or is not valid then imread returns None
        if type(self._img) == None:
            return False
        else:
            return True

    def show_image (self,img):
        cv2.imshow("Result",img)
        cv2.waitKey(0)

    def centroid (self):
        # convert image to grayscale image
        gray_image = cv2.cvtColor(self._img,cv2.COLOR_BGR2GRAY)
        # convert the grayscale image to binary image
        ret,thresh = cv2.threshold(gray_image,127,255,0)
        # calculate moments of binary image
        M = cv2.moments(thresh)
        # calculate x,y coordinate of center
        cX = int(M["m10"] / M["m00"])
        cY = int(M["m01"] / M["m00"])
        self._centroid = (cX,cY)
        cv2.circle(self._img,(cX,cY),5,(255,255),-1)

    def detect_pupil (self):
        dst = cv2.fastNlMeansDenoisingColored(self._img,None,10,7,21)
        blur = cv2.GaussianBlur(dst,(5,5),0)
        inv = cv2.bitwise_not(blur)
        thresh = cv2.cvtColor(inv,cv2.COLOR_BGR2GRAY)
        kernel = np.ones((2,2),np.uint8)
        erosion = cv2.erode(thresh,kernel,iterations = 1)
        ret,thresh1 = cv2.threshold(erosion,210,cv2.THRESH_BINARY)
        cnts,hierarchy = cv2.findContours(thresh1,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
        flag = 10000
        final_cnt = None
        for cnt in cnts:
            (x,y),radius = cv2.minenclosingCircle(cnt)
            distance = abs(self._centroid[0]-x)+abs(self._centroid[1]-y)
            if distance < flag :
                flag = distance
                final_cnt = cnt
            else:
                continue
        (x,radius = cv2.minenclosingCircle(final_cnt)
        center = (int(x),int(y))
        radius = int(radius)
        cv2.circle(self._img,center,radius,0),2)

        self._pupil = (center[0],center[1],radius)
        self.show_image(self._img)

    def start_detection(self):
        if(self.load_image()):
            self.centroid()
            self.detect_pupil()
        else:
            print('Image file "' + self._img_path + '" Could not be loaded.')

id = pupil_detection(r'rightlook2.jpg')
id.start_detection()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?