duancong6937 2016-09-21 01:35
浏览 84

如何获得golang读取jpeg并获得与Python / C相同的unit8值?

I have a test jpeg I'm using for some image processing code & I'm trying to get the same array of uint8 values that opencv in python and C and Pillow (via scipy) in Python, which all match the values I get when I open the jpeg using gimp.

I tried shifting the color values 8 bits, but this does not convert the values accurately - it looks like there's some rounding that I don't quite understand. I know gimp and opencv and Pillow/scipy all use libjpeg, so I tried using https://github.com/pixiv/go-libjpeg and converting the images to 8 bits using

func Convert(img image.Image) *image.RGBA {
   b := img.Bounds()
    rgba := image.NewRGBA(b)
    for y := b.Min.Y; y < b.Max.Y; y++ {
        for x := b.Min.X; x < b.Max.X; x++ {
            r32, g32, b32, _ := img.At(x, y).RGBA()
            c := color.RGBA{uint8(r32>>8), uint8(g32>>8), uint8(b32>>8), 255}
            rgba.SetRGBA(x, y, c)
        }
    }
    return rgba
}

but this still does not match. How can I get the same results?

  • 写回答

1条回答 默认 最新

  • doucitan2544 2016-09-21 13:31
    关注

    The JPEG specification doesn't have a pixel exact definition for a decoder, so different implementations will have slightly different output. Even libjpeg on it's own has 4 different Discrete Cosine Transform implementations, using integer or floating point math.

    As for your code, if img is already an image.RGBA, the values are all stored as uint8 and you're just shifting the bits back and forth doing nothing. If the image is an image.YCbCr, then you also depend on the color conversion algorithm, and the YCbCr.RGBA() method differs slightly from the color.YCbCrToRGB function. The former tries to maintain more precision, and you need to properly round the values rather than truncating them with a simple right shift. The latter is closer to the spec recommendation, but doesn't use floating point constants which again may differ from other implementations.

    Finally, the image itself may have an embedded ICC profile, and depending on the software used to decode the image, the profile may or may not have been applied when viewing the raw pixel data.

    评论

报告相同问题?

悬赏问题

  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思