dsifjgogw48491752 2017-01-15 16:53
浏览 254

Golang:如何将image.image转换为uint16

I am trying to use the go-skeltrack library with some depth images I have (Not using freenect). For that I need to modify the provided example by replacing the kinect images by my own. For that I have to read an image and convert it later to an []uint16 variable. The code which I tried is:

file, err := os.Open("./images/4.png")
if err != nil {
    fmt.Println("4.png file not found!")
    os.Exit(1)
}
defer file.Close()

fileInfo, _ := file.Stat()
var size int64 = fileInfo.Size()
bytes := make([]byte, size)

// read file into bytes 
buffer := bufio.NewReader(file)
_, err = buffer.Read(bytes)  

integerImage := binary.BigEndian.Uint16(bytes)

onDepthFrame(integerImage)

Where onDepthFrame is a function which has the form

func onDepthFrame(depth []uint16).

But I am getting the following error while compiling:

./skeltrackOfflineImage.go:155: cannot use integerImage (type uint16) as type []uint16 in argument to onDepthFrame

Which of course refers to the fact that I generated a single integer instead of an array. I am quite confused about the way that Go data types conversion works. Please help!

Thanks in advance for your help. Luis

  • 写回答

2条回答 默认 最新

  • dtjo51649 2017-01-15 21:28
    关注

    binary.BigEndian.Uint16 converts two bytes (in a slice) to a 16-bit value using big endian byte order. If you want to convert bytes to a slice of uint16, you should use binary.Read:

    // This reads 10 uint16s from file.
    slice := make([]uint16, 10)
    err := binary.Read(file, binary.BigEndian, slice)
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度