gpt4 book ai didi

python - 如何遮盖圆形区域?

转载 作者:太空宇宙 更新时间:2023-11-03 22:53:51 24 4
gpt4 key购买 nike

我试图遮盖图像中的圆形区域。我把代码和输出图像放在下面。如你所见,我在虹膜周围画了一个圆圈。在这之后,我想把圆形区域外的所有东西都涂黑。如何我需要继续吗?还有其他方法吗?

谢谢...

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

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

关于python - 如何遮盖圆形区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34290781/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com