Rmagick直方图问题

发布于 2024-11-29 12:12:35 字数 637 浏览 2 评论 0原文

我在访问 color_histogram() 返回的哈希时遇到问题。我的图像中只有黑白像素,这非常令人沮丧。

当我

puts hist.inspect() # returns "{red=0, green=0, blue=0, opacity=0=>779753, red=65535, green=65535, blue=65535, opacity=0=>6679}"

根据文档进行操作时,关键是 Pixel 对象,因此我构造了

black = Magick::Pixel.from_color('black')
white = Magick::Pixel.from_color('white')

puts black.inspect # red=0, green=0, blue=0, opacity=0
puts white.inspect # red=65535, green=65535, blue=65535, opacity=0


puts hist[white] # raises exception: `[]': can't convert Magick::Pixel into Integer (TypeError)

如何轻松访问直方图的任何想法?

谢谢

I'm having problems accessing the hash returned by color_histogram(). There are only black and white pixels in my image, and this is very frustrating.

When I do

puts hist.inspect() # returns "{red=0, green=0, blue=0, opacity=0=>779753, red=65535, green=65535, blue=65535, opacity=0=>6679}"

According to the documentation, the key is a Pixel object, so I construct

black = Magick::Pixel.from_color('black')
white = Magick::Pixel.from_color('white')

puts black.inspect # red=0, green=0, blue=0, opacity=0
puts white.inspect # red=65535, green=65535, blue=65535, opacity=0


puts hist[white] # raises exception: `[]': can't convert Magick::Pixel into Integer (TypeError)

Any ideas how I can access the histogram easily?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲念泪 2024-12-06 12:12:35

您正在 color_histogram 之间的某个位置更改 hist 调用和您的 hist[white]。这对我来说适用于一个简单的黑白 bw.png

> i = Magick::Image.read('bw.png').first
=> bw.png PNG 100x100 100x100+0+0 DirectClass 8-bit 466b 
> h = i.color_histogram
=> {red=0, green=0, blue=0, opacity=0=>2342, red=65535, green=65535, blue=65535, opacity=0=>7658} 
> black = Magick::Pixel.from_color('black')
=> red=0, green=0, blue=0, opacity=0 
> h[black]
=> 2342 

并且您可以使用数组轻松重现“无法将 Magick::Pixel 转换为整数(TypeError)”错误:

> a = [ ]
=> [] 
> a[black]
TypeError: can't convert Magick::Pixel into Integer

哈希永远不会通过简单的访问产生 TypeError。

You're changing hist somewhere between your color_histogram call and your hist[white]. This works for me with a simple black and white bw.png:

> i = Magick::Image.read('bw.png').first
=> bw.png PNG 100x100 100x100+0+0 DirectClass 8-bit 466b 
> h = i.color_histogram
=> {red=0, green=0, blue=0, opacity=0=>2342, red=65535, green=65535, blue=65535, opacity=0=>7658} 
> black = Magick::Pixel.from_color('black')
=> red=0, green=0, blue=0, opacity=0 
> h[black]
=> 2342 

And you "can't convert Magick::Pixel into Integer (TypeError)" error is easily reproduced using an Array:

> a = [ ]
=> [] 
> a[black]
TypeError: can't convert Magick::Pixel into Integer

A Hash will never produce that TypeError from a simple access.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文