dongqin1861 2017-07-21 00:06
浏览 211
已采纳

Golang图片ColorModel()

I am teaching myself Go. I decided to try some computer vision stuff. First things first I was going to make an image histogram. I'm trying to get the color model so I know the intensity range of the pixels. When I print image.ColorModel() it gives me a cryptic hexidecimal output: color model: &{0x492f70} I couldn't find any explanation in the docs. I was expecting some sort of enum type thing that would map to a color model like, NRGBA, RGBA, etc.

What does that hexidecimal mean? What does the ampersand curly braces &{...} mean? Also what is the "N" in NRGBA I can't find anything about it.

  • 写回答

3条回答 默认 最新

  • duansanzi5265 2017-07-21 07:32
    关注

    To extend putu's answer, comparing the returned color model to the "prepared" models of the image package only works if one of those models is used, else all comparison will result in false. Also it is quite inconvenient to list and compare to all possible models.

    Instead to find out a talkative form of the color model, we may use this little trick: try to convert any color using the color model of the image. A concrete color model converts all color values (implementations) to the color type / implementation used by the image. Printing the type of the resulting color will tell you what you are looking for.

    Example:

    col := color.RGBA{} // This is the "any" color we convert
    var img image.Image
    
    img = &image.NRGBA{}
    fmt.Printf("%T
    ", img.ColorModel().Convert(col))
    
    img = &image.Gray16{}
    fmt.Printf("%T
    ", img.ColorModel().Convert(col))
    
    img = &image.NYCbCrA{}
    fmt.Printf("%T
    ", img.ColorModel().Convert(col))
    
    img = &image.Paletted{}
    fmt.Printf("%T
    ", img.ColorModel().Convert(col))
    

    Output (try it on the Go Playground):

    color.NRGBA
    color.Gray16
    color.NYCbCrA
    <nil>
    

    As can be seen, an image of type *image.NRGBA models colors using color.NRGBA, an image of type *image.Gray16 models colors using color.Gray16 etc. As a last example I used *image.Paletted, where the result was nil, because the image's palette was empty.

    To quickly fix the nil palette, let's provide an initial palette:

    img = &image.Paletted{Palette: []color.Color{color.Gray16{}}}
    fmt.Printf("%T
    ", img.ColorModel().Convert(col))
    

    Now the output will be (try this on the Go Playground):

    color.Gray16
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 复现论文:matlab仿真代码编写
  • ¥15 esp32驱动GC9A01循环播放视频
  • ¥15 惠普360g9的最新bios
  • ¥30 这个功能用什么软件发合适?
  • ¥60 微信小程序,取消订单,偶尔订单没有改变状态
  • ¥15 用pytorch实现PPO算法
  • ¥15 关于调制信号的星座图?
  • ¥30 前端传参时,后端接收不到参数
  • ¥15 这是有什么问题吗,我检查许可证了但是显示有呢
  • ¥15 机器学习预测遇到的目标函数问题