Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 同心环覆盆子皮_Python_Image Processing_Raspberry Pi_Buffer - Fatal编程技术网

Python 同心环覆盆子皮

Python 同心环覆盆子皮,python,image-processing,raspberry-pi,buffer,Python,Image Processing,Raspberry Pi,Buffer,我期待着创建一个简单的代码,将允许我分析使用raspberry pi相机拍摄的图像 我想使用python或raspbarian将图像分割为同心环/环形,然后分析每个环中黑色像素的数量 任何建议都很好 我对Rapberry Pi不太了解,但我假设您可以从其摄像头中以普通图像文件的形式访问图像,并且我假设您可以安装PIL 如果是这样,您可以打开图像,迭代其像素,并根据每个像素与中心的距离将其分类为一个波段 from PIL import Image from collections import C

我期待着创建一个简单的代码,将允许我分析使用raspberry pi相机拍摄的图像

我想使用python或raspbarian将图像分割为同心环/环形,然后分析每个环中黑色像素的数量


任何建议都很好

我对Rapberry Pi不太了解,但我假设您可以从其摄像头中以普通图像文件的形式访问图像,并且我假设您可以安装PIL

如果是这样,您可以打开图像,迭代其像素,并根据每个像素与中心的距离将其分类为一个波段

from PIL import Image
from collections import Counter
import math

RING_RADIUS = 15

img = Image.open("image.png")

center_x = img.size[0] / 2
center_y = img.size[1] / 2

results = Counter()
pix = img.load()
for i in range(img.size[0]):
    for j in range(img.size[1]):
        d = math.hypot(center_x - i, center_y - j)
        band = int(d / RING_RADIUS)
        if pix[i,j] == (0,0,0):
            results[band] += 1

for k,v in sorted(results.items()):
    print(f"Number of black pixels in ring #{k}: {v}")
让我们试着在这个示例图像上运行它:

结果:

Number of black pixels in ring #0: 145
Number of black pixels in ring #1: 150
Number of black pixels in ring #2: 150
Number of black pixels in ring #3: 150
Number of black pixels in ring #4: 453
Number of black pixels in ring #5: 337
Number of black pixels in ring #6: 613
Number of black pixels in ring #7: 489
Number of black pixels in ring #8: 1711
Number of black pixels in ring #9: 1460
Number of black pixels in ring #10: 1223
Number of black pixels in ring #11: 1505
Number of black pixels in ring #12: 1199
Number of black pixels in ring #13: 1120
Number of black pixels in ring #14: 1104
Number of black pixels in ring #15: 608
Number of black pixels in ring #16: 278
Number of black pixels in ring #17: 168
Number of black pixels in ring #18: 153
Number of black pixels in ring #19: 249
Number of black pixels in ring #20: 77
由于您使用的是网络摄像头数据,您可能需要修改
if pix[i,j]==(0,0,0):
行,因为完美的黑色像素将非常罕见。你可以检查你的像素和纯黑色之间的距离是否相当小。有几种计算方法,但欧几里德距离是最简单的

def color_diff(a, b):
    return math.sqrt(
        (a[0]-b[0])**2 + 
        (a[1]-b[1])**2 + 
        (a[2]-b[2])**2
    )

#later in the code...

if color_diff(pix[i,j], (0,0,0)) < some_tolerance_value_goes_here:
        results[band] += 1
def color_diff(a、b):
return math.sqrt(
(a[0]-b[0])**2+
(a[1]-b[1])**2+
(a[2]-b[2])**2
)
#在代码的后面。。。
如果颜色_diff(pix[i,j],(0,0,0))
大家好,欢迎来到Stack Overflow。你的问题太宽泛了。你应该阅读文档,尝试一些东西,如果你没有成功,那么就带着一个精确的问题回来。我现在试图将每个环中的所有黑色像素数相加,但发现sum(v)不起作用。如果可能的话,你能推荐怎么做吗?比如,把每个环中的所有计数加起来<代码>求和(results.values())。最后一个问题是,如果我只想将前5个带相加,我会怎么做?再次感谢您的帮助
sum(范围(5)内的i的结果[i])
应该可以做到。太好了!如果我想知道第三环中的金额等怎么办?