Skip to content Skip to sidebar Skip to footer

How To Mask Circular Area?

I am trying to mask circular area in image.I put the code and the output image below.As you can see,I draw the circle around iris.After this point,I want to black out everything ou

Solution 1:

First of all, HoughCircles function returns a series of circles that happens to be on image. In your case, your image has only one cirle, therefore your code shows only one circle around the iris. You have to decide which circle corresponds to the iris in the first place.

Let's assume there is only one circle, which is iris, on your image. You can traverse all the pixels in your image and check if they are close to the center of circle. If it doesn't, you can change pixel value by 0, which corresponds to black.

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 inrange(cols):
    for j inrange(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()

Post a Comment for "How To Mask Circular Area?"