简体   繁体   English

同一图像的 RGB 值在手机和 mac 或模拟器之间是不同的

[英]RGB values for same image is different between phone and mac or simulator

I'm working on a task where given an image file stored locally (png/ jpg), I have to extract the rgb pixel values and input that to a different function. The problem I have faced is, the rgb values I get from ios simulator environment and on ios device is different resulting the output from next function to be very different as well.我正在处理给定本地存储的图像文件 (png/jpg) 的任务,我必须提取 rgb 像素值并将其输入到不同的 function。我遇到的问题是,我从 ios 获得的 rgb 值模拟器环境和 ios 设备不同导致 output 与下一个 function 也非常不同。 Has anyone faced similar issue?有没有人遇到过类似的问题? What could be the problem for this strange behaviour?这种奇怪的行为可能是什么问题?

I have used swiftimage library and another different method to extract the rgb values and they both product same output on each device (but different between across each devices)我使用了 swiftimage 库和另一种不同的方法来提取 rgb 值,它们在每个设备上都产生相同的 output(但每个设备之间不同)

Using swiftimage library this is how I exract rgbs (from github.com/koher/swift-image)使用 swiftimage 库这就是我提取 rgbs 的方式(来自 github.com/koher/swift-image)

extension UIImage {
    func extractrgbValues() -> [Float] {
        let swImage = Image<RGB<Float>>(uiImage: self)
        let width = swImage.width
        let height = swImage.height
        
        var reds = [[Float]](repeating: [Float](repeating: 0, count: width), count: height)
        var greens = [[Float]](repeating: [Float](repeating: 0, count: width), count: height)
        var blues = [[Float]](repeating: [Float](repeating: 0, count: width), count: height)
        
        // data is stored columnwise and we have to flip i,j to reconstruct it row-wise
        for i in 0..<width {
            for j in 0..<height {
                let pixel = swImage[i,j]
                reds[j][i] = pixel.red
                greens[j][i] = pixel.green
                blues[j][i] = pixel.blue
            }
        }
        return [reds, greens, blues].flatMap { $0 }.flatMap { $0 }
    }
}

Other reference I've tried is an answer from this post Get Pixel color of UIImage我试过的其他参考是这篇文章的答案Get Pixel color of UIImage

For the very same image, pixel values on pc/ android environment are almost identical.对于相同的图像,pc/android 环境中的像素值几乎相同。 But on iOS both device and simulator produce very different outcomes and neither is close to pc/android output.但是在 iOS 上,设备和模拟器都会产生非常不同的结果,而且两者都不接近 pc/android output。

I would expect given this line that it's using a device-calibrated RGB colorspace.我希望这条线使用设备校准的 RGB 色彩空间。 When you decode the image, it adjusts it to display the correct, calibrated colors on this device's screen.当您解码图像时,它会调整它以在该设备的屏幕上显示正确的、校准的 colors。 If you want the pixel data to match other platforms, you need to decode it using the same colorspace they do.如果您希望像素数据与其他平台匹配,则需要使用与它们相同的色彩空间对其进行解码。 For PC and Android, I would guess this to be either kCGColorSpaceSRGB or kCGColorSpaceLinearSRGB .对于 PC 和 Android,我猜这是kCGColorSpaceSRGBkCGColorSpaceLinearSRGB

(Note that if you then display the image locally, the colors will not match other calibrated displays.) (请注意,如果您随后在本地显示图像,则 colors 将不会匹配其他已校准的显示器。)

You can set the colorspace in CIContext using createCGImage(_:from:format:colorSpace:) .您可以使用createCGImage(_:from:format:colorSpace:)在 CIContext 中设置色彩空间。

If you already have the CGImage, you can make a new CGImage in a different colorspace using copy(colorSpace:) .如果您已经拥有 CGImage,则可以使用copy(colorSpace:)在不同的色彩空间中制作一个新的 CGImage。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM