python - 在python中创建圆圈来掩盖图像并计算每个圆圈内的像素

标签 python image image-processing

在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) - 由于图像非常大。另外,你必须使 i 在范围阈值内(例如 10000)。这将起作用:

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

关于python - 在python中创建圆圈来掩盖图像并计算每个圆圈内的像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52762929/

相关文章:

javascript - 将鼠标悬停在效果上将图像向下推

python - 根据图像 python 内的边框裁剪图像

python - 从 URL 获取参数 slugs

python - 如何在 for 循环调用 Shell 脚本中转义 Python 名称错误?

javascript - 在 Django 中,如何在管理表单中显示站点缩写而不是完整的站点域?

javascript - 我如何在javascript中克隆图像

python - 如何在 Sklearn Pipeline 中进行 Onehotencoding

android - 如何在不改变( strip )的情况下在 Android 中添加 PNG 图像作为背景?

python-3.x - 如何使用pytesseract获得每一行的信心

图像色彩饱和度