英文:
How to access YCbCr color attributes in Go
问题
我正在迭代图像的像素,尝试获取各个颜色值并求平均值。当我这样做时:
bounds := img.Bounds()
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
fmt.Println(reflect.TypeOf(img.At(x, y)))
}
}
我得到了一亿次的 color.YCbCr
。如果我不使用 reflect.TypeOf
打印,我会得到如下结果:
{154 135 124}
{153 135 124}
{152 135 124}
{152 135 124}
{151 135 124}
{149 135 124}
{147 135 124}
我需要能够访问单独的 Y、Cb 和 Cr 颜色,但是当我尝试 img.At(x, y).Cb
或 img.At(x, y)['Y']
,甚至是 img.At(x, y)[0]
时,我得到各种编译时错误,告诉我 color.Color
没有这些方法或不支持索引。
英文:
I'm iterating over an image's pixels trying to get the individual color values and average them out. When I do this:
bounds := img.Bounds()
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
fmt.Println(reflect.TypeOf(img.At(x, y)))
}
}
I get color.YCbCr
a billion times. If I print it without reflect.TypeOf
, I get a result like this:
{154 135 124}
{153 135 124}
{152 135 124}
{152 135 124}
{151 135 124}
{149 135 124}
{147 135 124}
I need to be able to access the individual Y, Cb and Cr colors, but when I try img.At(x, y).Cb
or img.At(x, y)['Y']
or even img.At(x, y)[0]
, I get various compile-time errors telling me that color.Color
doesn't have those methods or doesn't support indexing.
答案1
得分: 3
以下是翻译好的内容:
为了以后参考,访问底层的color.YCbCr
,你只需要对值进行断言,例如:
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
if c, ok := img.At(x, y).(color.YCbCr); ok {
fmt.Println(c.Y, c.Cb, c.Cr)
} else {
fmt.Println(reflect.TypeOf(img.At(x, y)))
}
}
}
英文:
Adding this for future reference, but to access the underling color.YCbCr
you just need to type assert the value, example:
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
if c, ok := img.At(x, y).(color.YCbCr); ok {
fmt.Println(c.Y, c.Cb, c.Cr)
} else {
fmt.Println(reflect.TypeOf(img.At(x, y)))
}
}
}
答案2
得分: 0
原文已翻译如下:
原来我需要的方法是img.At(x, y).RGBA()
,它分别返回这些值。
英文:
It turns out the method I needed was img.At(x, y).RGBA()
, this returns those values respectively.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论