如何遮盖圆形区域?

How to mask circular area?

我试图在image.I中屏蔽圆形区域把代码和输出图像below.As你可以看到,我在iris.After这一点周围画了一个圆圈,我想将通告外的所有内容涂黑 area.How 我需要继续吗?还有其他方法吗?

谢谢...

import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('i1.jpg',0)
img = cv2.medianBlur(img,5)
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,120,
                    param1=50,param2=50,minRadius=30,maxRadius=0)
circles = np.uint16(np.around(circles))
for i in circles[0,:]:
     # draw the outer circle
     cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
     # draw the center of the circle
     cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)
cv2.imwrite("iris.jpg",cimg)
plt.imshow(cimg, cmap = 'gray', interpolation = 'bicubic')
plt.xticks([]), plt.yticks([])  # to hide tick values on X and Y axis
plt.show()

enter image description here

首先,HoughCircles函数returns一系列恰好在图像上的圆圈。在您的情况下,您的图像只有一个圆圈,因此您的代码仅显示虹膜周围的一个圆圈。你必须首先决定哪个圆圈对应虹膜。

假设您的图片上只有一个圆圈,它是虹膜。您可以遍历图像中的所有像素并检查它们是否靠近圆心。如果不是,你可以将像素值改为0,对应黑色。

import cv2
import numpy as np
from matplotlib import pyplot as plt
from math import hypot

img = cv2.imread('asd.png',0)
img = cv2.medianBlur(img,5)

circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,120,param1=50,param2=50,minRadius=30,maxRadius=0)
circles = np.uint16(np.around(circles))

x, y, r = circles[0,:][0]
rows, cols = img.shape

for i in range(cols):
    for j in range(rows):
        if hypot(i-x, j-y) > r:
            img[j,i] = 0

cv2.imwrite("iris.jpg",img)
plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')
plt.xticks([]), plt.yticks([])
plt.show()