Reputation: 39
In python, I am trying to separate an image into circles and calculate the number of black pixels in each circle.
For example, I have an image captured with a fisheye lens (a hemispherical image) (drawn example below) and I want to divide the image into small circles capturing part of the image from a small circle in the middle to the whole of the image.
I would like to split the image into x number of circles capturing part of the image each time (see images below)
Once I have the circle images I can calculate the number of pixels in each image.
I tried:
Image=Image.new("RGB", (2000,2000))
draw = ImageDraw.Draw(image)
draw.ellipse((20,20,1800,1800),fill(255,255,255)
Then created a mask from this, however regardless of how I change the numbers int the draw.ellipse the circle only ever captures the whole of the image but makes the image itself smaller.
Any ideas or recommendations on how to fix this would be really appreciated!
Upvotes: 1
Views: 5050
Reputation: 2831
You should look at OpenCV for such tasks. You can transform the circle to whole contour and calculate the radius of the circle. Then you can draw circle and draw them on a mask and perform cv2.bitwise_and
to make the circle ROI over the image. You can iterate and multply with an integer of your choice (in my case 10) the radius of the ROI circle. Hope it helps. Cheers!
Example code:
import cv2
import numpy as np
img = cv2.imread('circle.png')
h, w = img.shape[:2]
mask = np.zeros((h, w), np.uint8)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
kernel = np.ones((10,10),np.uint8)
opening = cv2.morphologyEx(thresh,cv2.MORPH_CLOSE,kernel, iterations = 2)
_, contours, hierarchy = cv2.findContours(opening,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
cnt = max(contours, key=cv2.contourArea)
extLeft = tuple(cnt[cnt[:, :, 0].argmin()][0])
extRight = tuple(cnt[cnt[:, :, 0].argmax()][0])
radius = (extRight[0] - extLeft[0])/2
print(extRight[0], extLeft[0])
print(radius)
M = cv2.moments(cnt)
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])
print(cx, cy)
for i in range(1,30):
if i*10<radius:
print(i*10)
cv2.circle(mask,(cx,cy), i*10, 255, -1)
res = cv2.bitwise_and(img, img, mask=mask)
pixels = np.sum(res == 255)
cv2.putText(res,'Pixel count: '+str(pixels),(30,30), cv2.FONT_HERSHEY_COMPLEX, 0.5, (255,255,255), 1, cv2.LINE_AA)
cv2.imshow('img', res)
cv2.waitKey(0)
cv2.destroyAllWindows()
else:
res = cv2.bitwise_and(img, img, mask=opening)
pixels = np.sum(res == 255)
cv2.putText(img,'Pixel count: '+str(pixels),(30,30), cv2.FONT_HERSHEY_COMPLEX, 0.5, (255,255,255), 1, cv2.LINE_AA)
cv2.imshow('img', res)
cv2.waitKey(0)
cv2.destroyAllWindows()
break
Result:
EDIT:
Try with a different way to calculate the middle
import cv2
import numpy as np
img = cv2.imread('circle.png')
h, w = img.shape[:2]
mask = np.zeros((h, w), np.uint8)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
kernel = np.ones((10,10),np.uint8)
opening = cv2.morphologyEx(thresh,cv2.MORPH_CLOSE,kernel, iterations = 2)
_, contours, hierarchy = cv2.findContours(opening,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
cnt = max(contours, key=cv2.contourArea)
cv2.imshow('img22', opening)
extLeft = tuple(cnt[cnt[:, :, 0].argmin()][0])
extRight = tuple(cnt[cnt[:, :, 0].argmax()][0])
radius = (extRight[0] - extLeft[0])/2
x,y,w,h = cv2.boundingRect(cnt)
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
cx = int(x+(w/2))
cy = int(y+h/2)
for i in range(1,30):
if i*10<radius:
print(i*10)
cv2.circle(mask,(cx,cy), i*10, 255, -1)
res = cv2.bitwise_and(img, img, mask=mask)
pixels = np.sum(res == 255)
cv2.putText(res,'Pixel count: '+str(pixels),(30,30), cv2.FONT_HERSHEY_COMPLEX, 0.5, (255,255,255), 1, cv2.LINE_AA)
cv2.imshow('img', res)
cv2.waitKey(0)
cv2.destroyAllWindows()
else:
res = cv2.bitwise_and(img, img, mask=opening)
pixels = np.sum(res == 255)
cv2.putText(img,'Pixel count: '+str(pixels),(30,30), cv2.FONT_HERSHEY_COMPLEX, 0.5, (255,255,255), 1, cv2.LINE_AA)
cv2.imshow('img', res)
cv2.waitKey(0)
cv2.destroyAllWindows()
break
Edit 2:
Okay so my assumption from your first example image was that your image will be almost a circle from the get go. Because it is not you have to calculate the center differently (like from my first edit - from bounding box) and make a bigger kernel (40,40) - due to the fact that the image is very large. Plus you have to make for i in range threshold (like 10000). This will work:
import cv2
import numpy as np
img = cv2.imread('circleroi.jpg')
h, w = img.shape[:2]
mask = np.zeros((h, w), np.uint8)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
kernel = np.ones((40,40),np.uint8)
opening = cv2.morphologyEx(thresh,cv2.MORPH_CLOSE,kernel, iterations = 2)
_, contours, hierarchy = cv2.findContours(opening,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
cnt = max(contours, key=cv2.contourArea)
extLeft = tuple(cnt[cnt[:, :, 0].argmin()][0])
extRight = tuple(cnt[cnt[:, :, 0].argmax()][0])
radius = (extRight[0] - extLeft[0])/2
x,y,w,h = cv2.boundingRect(cnt)
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
cx = int(x+(w/2))
cy = int(y+h/2)
for i in range(1,10000):
if i*10<radius:
cv2.circle(mask,(cx,cy), i*10, 255, -1)
res = cv2.bitwise_and(img, img, mask=mask)
pixels = np.sum(res == 255)
cv2.putText(res,'Pixel count: '+str(pixels),(30,30), cv2.FONT_HERSHEY_COMPLEX, 0.5, (255,255,255), 1, cv2.LINE_AA)
cv2.imshow('img', res)
cv2.waitKey(0)
cv2.destroyAllWindows()
else:
res = cv2.bitwise_and(img, img, mask=opening)
pixels = np.sum(res == 255)
cv2.putText(img,'Pixel count: '+str(pixels),(30,30), cv2.FONT_HERSHEY_COMPLEX, 0.5, (255,255,255), 1, cv2.LINE_AA)
cv2.imshow('img', res)
cv2.waitKey(0)
cv2.destroyAllWindows()
break
Upvotes: 2
Reputation: 5460
This is mostly straightforward to do in pure numpy if you're familiar enough with the library:
# Create some fake data
np.random.seed(100)
fake_im_arr = np.random.randint(low=0, high=2, size=(2000,2000))
# Function definition for creating masks
def create_circle_mask(X_arr, Y_arr, center, radius):
c_x, c_y = center
dists_sqrd = (X_arr - c_x)**2 + (Y_arr - c_y)**2
return dists_sqrd <= radius**2
# Using the two together:
center, radius = (1000, 1000), 5
size_x, size_y = fake_im_arr.shape
mask = create_circle_mask(*np.ogrid[0:size_x, 0:size_y], center=center, radius=radius)
n_black_in_circle = ((fake_im_arr == 1) & mask).sum() # This is your answer (39 in this case)
To see what the various arrays look like:
fake_im_arr[center[0] - radius:center[0] + (radius + 1),
center[1] - radius:center[1] + (radius + 1)]
array([[0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1],
[0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1],
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1],
[1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1],
[1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0],
[0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1],
[1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0],
[0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0],
[1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0]])
mask[center[0] - radius:center[0] + (radius + 1),
center[1] - radius:center[1] + (radius + 1)].astype('int')
array([[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0]])
np.where(mask, fake_im_arr, 0)[center[0] - radius:center[0] + (radius + 1),
center[1] - radius:center[1] + (radius + 1)]
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0],
[1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1],
[0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0],
[0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0],
[0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0],
[0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0]])
Upvotes: 1