在Go中获取图像的子图像时遇到了问题。

huangapple go评论142阅读模式
英文:

Trouble getting a SubImage of an Image in Go

问题

我正在使用Go进行一些图像处理,并尝试获取图像的SubImage。

import (
  "image/jpeg"
  "os"
)
func main(){
  image_file, err := os.Open("somefile.jpeg")
  my_image, err := jpeg.Decode(image_file)
  my_sub_image := my_image.SubImage(Rect(j, i, j+x_width, i+y_width)).(*image.RGBA)
}

当我尝试编译时,我得到了.\img.go:8: picture.SubImage undefined (type image.Image has no field or method SubImage)的错误。

有什么想法吗?

英文:

I'm doing some image processing in Go, and I'm trying to get the SubImage of an Image.

import (
  "image/jpeg"
  "os"
)
func main(){
  image_file, err := os.Open("somefile.jpeg")
  my_image, err := jpeg.Decode(image_file)
  my_sub_image := my_image.SubImage(Rect(j, i, j+x_width, i+y_width)).(*image.RGBA)
}

When I try to compile that, I get .\img.go:8: picture.SubImage undefined (type image.Image has no field or method SubImage).

Any thoughts?

答案1

得分: 18

这里有一种替代方法 - 使用类型断言来断言my_image具有SubImage方法。这适用于任何具有SubImage方法的图像类型(除了快速扫描中的Uniform之外的所有类型)。这将返回另一个未指定类型的Image接口。

package main

import (
	"fmt"
	"image"
	"image/jpeg"
	"log"
	"os"
)

func main() {
	image_file, err := os.Open("somefile.jpeg")
	if err != nil {
		log.Fatal(err)
	}
	my_image, err := jpeg.Decode(image_file)
	if err != nil {
		log.Fatal(err)
	}

	my_sub_image := my_image.(interface {
		SubImage(r image.Rectangle) image.Image
	}).SubImage(image.Rect(0, 0, 10, 10))

	fmt.Printf("bounds %v\n", my_sub_image.Bounds())

}

如果你想经常这样做,那么你可以创建一个具有SubImage接口的新类型并使用它。

type SubImager interface {
	SubImage(r image.Rectangle) image.Image
}

my_sub_image := my_image.(SubImager).SubImage(image.Rect(0, 0, 10, 10))

通常的类型断言注意事项适用 - 如果你不想发生恐慌,请使用,ok形式。

英文:

Here is an alternative approach - use a type assertion to assert that my_image has a SubImage method. This will work for any image type which has the SubImage method (all of them except Uniform at a quick scan). This will return another Image interface of some unspecified type.

package main

import (
	"fmt"
	"image"
	"image/jpeg"
	"log"
	"os"
)

func main() {
	image_file, err := os.Open("somefile.jpeg")
	if err != nil {
		log.Fatal(err)
	}
	my_image, err := jpeg.Decode(image_file)
	if err != nil {
		log.Fatal(err)
	}

	my_sub_image := my_image.(interface {
		SubImage(r image.Rectangle) image.Image
	}).SubImage(image.Rect(0, 0, 10, 10))

	fmt.Printf("bounds %v\n", my_sub_image.Bounds())

}

If you wanted to do this a lot then you would create a new type with the SubImage interface and use that.

type SubImager interface {
	SubImage(r image.Rectangle) image.Image
}

my_sub_image := my_image.(SubImager).SubImage(image.Rect(0, 0, 10, 10))

The usual caveats with type assertions apply - use the ,ok form if you don't want a panic.

答案2

得分: 8

因为image.Image没有SubImage方法。你需要使用类型断言来获取适当的image.*类型。

rgbImg := img.(image.RGBA)
subImg := rgbImg.SubImage(...)
英文:

Because image.Image doesn't have a method SubImage. You need to use a type assertion to get the appropriate image.* type.

rgbImg := img.(image.RGBA)
subImg := rgbImg.SubImage(...)

huangapple
  • 本文由 发表于 2013年4月18日 09:15:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/16072910.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定