UIImage的环通像素非常低​​效? [英] UIImage Loop Through Pixel Highly Inefficient?

查看:189
本文介绍了UIImage的环通像素非常低​​效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我使用这种方法来遍历每个像素,和值插入基于RGB值的三维阵列。我需要这个数组我的程序的其他部分,但它是非常缓慢的。当在一个50×50的画面​​运行,这几乎是即时的,但只要你开始进入百点¯x数百它需要很长的时间到应用程序是无用的地步。任何人对如何加快我的方法什么想法?

Currently I am using this method to loop through every pixel, and insert a value into a 3D array based upon RGB values. I need this array for other parts of my program, however it is extraordinarily slow. When run on a 50 x 50 picture, it is almost instant, but as soon as you start getting into the hundreds x hundreds it takes a long time to the point where the app is useless. Anyone have any ideas on how to speed up my method?

@IBAction func convertImage(sender: AnyObject) {
    if let image = myImageView.image {

        var pixelData = CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage))
        var data: UnsafePointer<UInt8> = CFDataGetBytePtr(pixelData)

        let height = Int(image.size.height)
        let width = Int(image.size.width)
        var zArry = [Int](count:3, repeatedValue: 0)
        var yArry = [[Int]](count:width, repeatedValue: zArry)
        var xArry = [[[Int]]](count:height, repeatedValue: yArry)

        for (var h = 0; h < height; h++) {
            for (var w = 0; w < width; w++) {
                 var pixelInfo: Int = ((Int(image.size.width) * Int(h)) + Int(w)) * 4
                var rgb = 0
                xArry[h][w][rgb] = Int(data[pixelInfo])
                rgb++
                xArry[h][w][rgb] = Int(data[pixelInfo+1])
                rgb++
                xArry[h][w][rgb] = Int(data[pixelInfo+2])
                }
        }
        println(xArry[20][20][1])
    }
}

也许有一种方法来的UIImage转换为不同类型的图像,并创建像素阵列。我向所有人开放的建议。谢谢!

Maybe there is a way to convert the UIImage to a different type of image and create an array of pixels. I am open to all suggestions. Thanks!

目标:目标是使用数组修改所有像素的RGB值,并且与修改后的像素创建新图像。我想简单的通过所有像素的循环而不存储它们,并把它们修改到一个新的数组来创建图像,但得到了同样的性能问题。

GOAL: The goal is to use the array to modify the RGB values of all pixels, and create a new image with the modified pixels. I tried simply looping through all of the pixels without storing them, and modifying them into a new array to create an image, but got the same performance issues.

推荐答案

更新:

无数次尝试后,我意识到,我在调试配置,使得我的测试。

After countless tries I realized I was making my tests on debug configuration.

切换到发布,现在是如此之快。

Switched to release, and now it's so much faster.

斯威夫特似乎是在调试配置要慢许多倍。

Swift seems to be many times slower on the debug configuration.

现在您的code和我的优化版本之间的区别是快好几倍。

The difference now between your code and my optimized version is several times faster.

看来你有使用一个大的减速 image.size.width 而不是局部变量宽度

It seems as you have a big slowdown from using image.size.width instead of the local variable width.

原始

我试图优化它了一下,拿出这样的:

I tried to optimize it a bit and come up with this:

@IBAction func convertImage () {

    if let image = UIImage(named: "test") {

        let pixelData = CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage))
        let data: UnsafePointer<UInt8> = CFDataGetBytePtr(pixelData)

        let height = Int(image.size.height)
        let width = Int(image.size.width)
        let zArry = [Int](count:3, repeatedValue: 0)
        let yArry = [[Int]](count:width, repeatedValue: zArry)
        let xArry = [[[Int]]](count:height, repeatedValue: yArry)

        for (index, value) in xArry.enumerate() {
            for (index1, value1) in value.enumerate() {
                for (index2, var value2) in value1.enumerate() {

                    let pixelInfo: Int = ((width * index) + index1) * 4 + index2

                    value2 = Int(data[pixelInfo])
                }
            }
        }
    }
}

然而,在我的测试中,这是几乎快15%。你需要的是更快地级订单。

However in my tests this is barely 15% faster. What you need is orders of magnitude faster.

另一个IDEEA是直接使用数据对象当你需要它,而无需创建数组是这样的:

Another ideea is use the data object directly when you need it without creating the array like this:

let image = UIImage(named: "test")!

let pixelData = CGDataProviderCopyData(CGImageGetDataProvider(image.CGImage))
let data: UnsafePointer<UInt8> = CFDataGetBytePtr(pixelData)

let width = Int(image.size.width)

// value for [x][y][z]
let value = Int(data[((width * x) + y) * 4 + z])

您没说你如何使用这个数组中你的应用程序,但我觉得即使你找到一种方式来获得这个数组创建更快,当您尝试使用它,你会得到另一个问题,因为它会需要很长的时间太长。

You didn't say how you use this array in your app, but I feel that even if you find a way to get this array created much faster, you would get another problem when you try to use it, as it would take a long time too..

这篇关于UIImage的环通像素非常低​​效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆