【问题标题】:How to mask circular area?如何掩盖圆形区域?
【发布时间】:2015-12-15 13:41:11
【问题描述】:

我正在尝试掩盖图像中的圆形区域。我将代码和输出图像放在下面。如您所见,我在虹膜周围画了一个圆圈。在此之后,我想将圆形区域之外的所有内容都涂黑。如何我需要继续吗?还有其他方法吗?

谢谢...

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

【问题讨论】:

  • 创建与原始图像大小相同的黑色图像。用cv2.circle 函数在你想要你的面具的地方画一个实心的白色圆圈。使用带有反向掩码或掩码的 .setTo 或 .copyTo 函数,如果这些函数存在于 python opencv 中。

标签: python opencv iris-recognition


【解决方案1】:

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

假设您的图像上只有一个圆圈,即虹膜。您可以遍历图像中的所有像素并检查它们是否靠近圆心。如果不是,可以将像素值改为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()

【讨论】:

  • math.hypot 我会使用 np.linalg.norm 因为你已经导入了 numpy....
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-19
  • 2023-03-18
  • 2020-07-30
  • 2012-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多