【问题标题】:Drawing rectangles given X and Y coords around specific brightness points- python在特定亮度点周围绘制给定 X 和 Y 坐标的矩形 - python
【发布时间】:2021-07-11 01:07:37
【问题描述】:

我开始编写代码以帮助我研究球状星团和星系类型之间的相关性。因此,我的代码能够找到距离星系中所有可能的球状星团并返回 X 和 Y 坐标。现在,我需要弄清楚如何真正能够在 X 和 Y 坐标周围绘制小框,这样我就可以直观地看到分布。代码如下:

from PIL import Image
from math import sqrt
imag = Image.open("Centaurus_A-DeNoiseAI-denoise.jpg")
imag = imag.convert ('RGB')
x=[]
y=[]
for i in range(3008):
    X,Y = i,i
    (R,G,B) = imag.getpixel((X,Y))
    brightness = sum([R,G,B])/3
    if(94<brightness<124):
        print(X,Y)
        x.append(X)
        y.append(Y)

【问题讨论】:

  • cv2.rectangle(imag, x-10, x+10, (你是 BGR 中的颜色), (厚度))
  • 这会抛出错误:TypeError: Expected Ptr<:umat> for argument 'img',这是什么意思?如何绕过它?
  • 啊,我刚刚看到你用 PIL 初始化了你的图像,我让你用 opencv 绘图。让我写答案。
  • 您的代码似乎只查看对角线上的像素,其中 X==Y,是您想要的吗?
  • 是的,我修复了我在 for 循环中放置了一个 for 循环,以便它可以逐行运行

标签: python image cv2 astronomy


【解决方案1】:

将此代码添加到您现有的代码中

from PIL import Image, ImageDraw

#Your code from before here

with imag as im:
    delta = 5
    draw = ImageDraw.Draw(im)
    for i in range(len(x)):
        draw.rectangle([x[i-delta],y[i-delta],x[i-delta],y[i-delta]], fill=(255,0,0))

    im.save("your_image","PNG")

根据您想要在该矩形上的填充量相应地调整 delta。

【讨论】:

  • 谢谢,代码有效,但我认为我有一个系统错误,可能没有安装库?但是代码抛出了这个错误:AttributeError: module 'PIL.Image' has no attribute 'Draw'
  • 对不起,我打错了。
  • 不,问题我解决了,最后一件事。所以它运行然后抛出最终错误 TypeError: can only concatenate list (not "int") to list 但我在代码中创建了一个 x 和 y 列表来保存所有 x 和 y 值,我错过了什么?很抱歉打扰你,我的项目只需要这个:)
  • 嘿,有什么办法可以把盒子的颜色改成红色吗?
  • 修正了一个错误。可以通过draw.rectangle([x[i]-delta,y[i]-delta,x[i]-delta,x[y]-delta], fill=rgb(255,0,0)更改颜色
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-01
  • 1970-01-01
  • 2012-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-03
相关资源
最近更新 更多