我如何优化代码以缩短执行时间

时间:2019-06-11 14:48:34

标签: opencv image-processing kotlin

我正在实现一种称为BM3D的图像处理算法,并且我已经实现了将灰度图像去噪的结果,但事实是它太慢了,即使使用436 x 436灰度图像也是如此。

我已经尝试找到方法来加快我对数组和列表所做的工作,但是并没有得到很多改进

val img = imread("files/image.png", 0)
val img3= Mat(img.rows(),img.cols(),img.type())

val listaBlocos = mutableListOf(Pair(0.0, Pair(0,0)))
val tamanhoBloco = 3 //Block Size
val tamanhoJanela = 9 //Window Size
val mediaPorBloco = DoubleArray(16)
var sum = 0.0
listaBlocos.clear()
val stats_file = File("files/tempos436x436.txt")
val test = 10
for (x in 0 until test){
    val timeelapsed = measureTimeMillis {
        for (col in 20 ..img.width() - 20) {
            for (row in 20 ..img.height() - 20) {
                val block1 = generateBlock(img, row, col, tamanhoBloco)
                for (c in -tamanhoJanela..tamanhoJanela) {
                    for (l in -tamanhoJanela..tamanhoJanela) {
                        val  block2 = generateBlock(img, row + l, col + c, tamanhoBloco)
                        val  d = distBlock(block1, block2)
                        val  par = Pair(d, Pair(row + l, col + c))
                        listaBlocos.add(par)
                    }
                }
                val listaBlocosOrdenada = listaBlocos.sortedWith(compareBy { it.first })
                listaBlocos.clear()
                for (k in 0..15) {
                    sum = 0.0
                    val c2 = listaBlocosOrdenada[k].second.second
                    val l2 = listaBlocosOrdenada[k].second.first

                    for (c in 0..tamanhoBloco - 1) {
                        for (l in 0..tamanhoBloco - 1) {
                            sum += img.get(l2 - l, c2 - c)[0]
                        }
                    }
                    mediaPorBloco[k] = sum / 4
                }
                val v = mediaPorBloco.average()
                img3.put(row,col,v)

            }
        }
    }
imwrite("files/resultado.png", img3)
stats_file.appendText("teste$x 100X200 $timeelapsed\n")

}

实际图像去噪的结果很好,但是可能需要15分钟才能对436 x 436图像进行去噪。我目前正在使用具有Ubuntu和4核,4 GB Ram的虚拟机

0 个答案:

没有答案