在python中创建圆圈以遮盖图像并计算每个圆圈内的像素

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在python中创建圆圈以遮盖图像并计算每个圆圈内的像素相关的知识,希望对你有一定的参考价值。

在python中,我试图将图像分成圆圈并计算每个圆圈中的黑色像素数。

例如,我有一个用鱼眼镜头拍摄的图像(一个半球形图像)(下面的绘制示例),我想将图像分成小圆圈,从中间的一个小圆圈到整个图像捕捉图像的一部分。 picture of a captured image

我想将图像分成x个圆圈,每次捕捉图像的一部分(见下图)circle 1 circle 2

一旦我有圆形图像,我就可以计算每个图像中的像素数。

我试过了:Image=Image.new("RGB", (2000,2000)) draw = ImageDraw.Draw(image) draw.ellipse((20,20,1800,1800),fill(255,255,255)

然后从中创建了一个蒙版,但无论我如何更改draw.ellipse中的数字,圆圈只会捕获整个图像,但会使图像本身变小。

如何解决这个问题的任何想法或建议将非常感谢!

答案

你应该看看OpenCV来完成这些任务。您可以将圆转换为整个轮廓并计算圆的半径。然后,您可以绘制圆形并在蒙版上绘制它们并执行cv2.bitwise_and以在图像上创建圆形ROI。您可以使用您选择的整数(在我的情况下为10)迭代并乘以ROI圆的半径。希望能帮助到你。干杯!

示例代码:

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

结果:

enter image description here

enter image description here

enter image description here

编辑:

尝试用不同的方法计算中间值

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

编辑2:

好吧,所以我从你的第一个示例图像中得到的假设是,你的图像几乎就是一个圆圈。因为不是你必须以不同方式计算中心(比如从我的第一次编辑 - 来自边界框)并制作更大的内核(40,40) - 因为图像非常大。另外,你必须在范围阈值(如10000)中制作i。这将有效:

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
另一答案

如果你对图书馆足够熟悉,这在纯粹的numpy中几乎是直截了当的:

# 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)

要查看各种数组的外观:

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],

以上是关于在python中创建圆圈以遮盖图像并计算每个圆圈内的像素的主要内容,如果未能解决你的问题,请参考以下文章

如何在浮点数数组中创建一个由 1 组成的圆圈

MATLAB / Octave:从图像中切出很多圆圈

在 Leaflet 中创建具有不同圆圈大小的图例

pygame:屏蔽非图像类型的表面

圆圈内的可移动图像

带三角形带的圆圈