英文:
Convert []float64 pixel slice to an Image
问题
我正在尝试将图像调整大小并转换为浮点数的灰度切片(以便可以对浮点数进行一些转换),然后再转换回图像,但我不知道如何将 []float64 转换回 RGB 或其他可以转换为图像的格式。
到目前为止,我有以下代码:
// colorToGrayScaleFloat64 将颜色转换为灰度近似值。
func colorToGrayScaleFloat64(c color.Color) float64 {
r, g, b, _ := c.RGBA()
return 0.299*float64(r) +
0.587*float64(g) +
0.114*float64(b)
}
func getFloatPixels(filePath string) {
size := 32
f, err := os.Open(filePath)
if err != nil {
log.Fatalf("打开文件失败:%s", err.Error())
}
defer f.Close()
image, _, err := image.Decode(f)
if err != nil {
log.Fatalf("解码文件失败:%s", err.Error())
}
// 将图像调整为 32X32
im := imaging.Resize(image, size, size, imaging.Lanczos)
// 将图像转换为灰度浮点数组
vals := make([]float64, size*size)
for i := 0; i < size; i++ {
for j := 0; j < size; j++ {
vals[size*i+j] = colorToGrayScaleFloat64(im.At(i, j))
}
}
fmt.Printf("像素值:%+v\n", vals)
}
这会产生以下输出:
像素值:[40315.076 48372.797 48812.780999999995 47005.557 ... 25129.973999999995 24719.287999999997]
我该如何将这个像素的 []float64 转换回图像?
英文:
I'm attempting to resize and convert an image into a grayscale slice of float64 (so I can do some transformations on the floats) and then back into an image, but I'm not sure how to convert a []float64 back to RGB or something I can turn into an image.
So far I have:
// colorToGrayScaleFloat64 reduces rgb
// to a grayscale approximation.
func colorToGrayScaleFloat64(c color.Color) float64 {
r, g, b, _ := c.RGBA()
return 0.299*float64(r) +
0.587*float64(g) +
0.114*float64(b)
}
func getFloatPixels(filePath string) {
size := 32
f, err := os.Open(filePath)
if err != nil {
log.Fatalf("FAILED TO OPEN FILE: %s", err.Error())
}
defer f.Close()
image, _, err := image.Decode(f)
if err != nil {
log.Fatalf("FAILED TO DECODE FILE: %s", err.Error())
}
// Resize image to 32X32
im := imaging.Resize(image, size, size, imaging.Lanczos)
// Convert image to grayscale float array
vals := make([]float64, size*size)
for i := 0; i < size; i++ {
for j := 0; j < size; j++ {
vals[size*i+j] = colorToGrayScaleFloat64(im.At(i, j))
}
}
fmt.Printf("pixel vals %+v\n", vals)
}
Which produces the output:
pixel vals [40315.076 48372.797 48812.780999999995 47005.557 ... 25129.973999999995 24719.287999999997]
How can I convert this pixel []float64 back to an image?
答案1
得分: 1
所以基本上你有灰色像素的亮度,并且你想要一个表示它的color.Color
值。
这很简单:有一个color.Gray
类型和一个更高精度的color.Gray16
,它们用亮度来模拟颜色,所以只需创建这些类型的值即可。它们实现了color.Color
接口,因此你可以使用它们来设置图像的像素。
col := color.Gray{uint8(lum / 256)}
还要注意,你的colorToGrayScaleFloat64()
函数已经存在于标准库中。image/color
包中有几个转换器作为color.Model
的实现。使用color.GrayModel
或color.Gray16Model
将color.Color
转换为color.Gray
或color.Gray16
类型的值,它们直接存储灰色的亮度。
例如:
gray := color.Gray16Model.Convert(img.At(x, y))
lum := float64(gray.(color.Gray16).Y)
进行测试:
c := color.RGBA{111, 111, 111, 255}
fmt.Println("original:", c)
gray := color.Gray16Model.Convert(c)
lum := float64(gray.(color.Gray16).Y)
fmt.Println("lum:", lum)
col := color.Gray{uint8(lum / 256)}
r, g, b, a := col.RGBA()
a >>= 8
fmt.Println("lum to col:", r/a, g/a, b/a, a)
fmt.Println()
这将输出(在Go Playground上尝试一下):
original: {111 111 111 255}
lum: 28527
lum to col: 111 111 111 255
还要注意,如果你想创建一个全是灰色像素的图像,可以使用image.Gray
和image.Gray16
类型,这样在绘制这些颜色时就不需要进行颜色转换。它们还有指定的Gray.SetGray()
和Gray16.SetGray16()
方法,直接接受这些类型的颜色。
参考链接:
英文:
So basically you have the luminosity of the gray pixel, and you want to have a color.Color
value representing it.
This is quite simple: there is a color.Gray
type, and a higher precision color.Gray16
which model the color with its luminosity, so simply create a value of those. They implement color.Color
, so you can use them to set pixels of an image.
col := color.Gray{uint8(lum / 256)}
Also note that your colorToGrayScaleFloat64()
function is already present in the standard lib. There are several converters in the image/color
package as implementations of color.Model
. Use the color.GrayModel
or color.Gray16Model
to convert a color.Color
to a value of type color.Gray
or color.Gray16
which directly store the luminosity of the gray color.
For example:
gray := color.Gray16Model.Convert(img.At(x, y))
lum := float64(gray.(color.Gray16).Y)
Testing it:
c := color.RGBA{111, 111, 111, 255}
fmt.Println("original:", c)
gray := color.Gray16Model.Convert(c)
lum := float64(gray.(color.Gray16).Y)
fmt.Println("lum:", lum)
col := color.Gray{uint8(lum / 256)}
r, g, b, a := col.RGBA()
a >>= 8
fmt.Println("lum to col:", r/a, g/a, b/a, a)
fmt.Println()
This will output (try it on the Go Playground):
original: {111 111 111 255}
lum: 28527
lum to col: 111 111 111 255
Also note that if you want to create an image full of gray pixels, you may use the image.Gray
and image.Gray16
types so when drawing these colors on them, no color conversion will be needed. They also have designated Gray.SetGray()
and Gray16.SetGray16()
methods that directly take colors of these types.
See related:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论