如何在图像处理中提高FPS

时间:2019-05-07 18:54:04

标签: android performance opencv4android

我正在Android上制作一个应用程序来进行图像处理,但是很遗憾,它太慢了。我从相机获取图像(作为RGBA垫),将其转换为HSV,对其应用阈值,然后将其重新转换为RGBA。现在,我的代码正在运行25-30 FPS。

现在,我需要检测图像中的一条线。为此,我从底部的底部开始检测线条的两个边缘。

// at this point, the first pixel of the line has already been found
while (!lFound || !rFound || !lineFound) {
    // left edge of the line
    if (!lFound) {
        // check all different possibilities for the next pixel of the line (there are 8 different possibilities)
        for (i = 0; i < 8; i++) {
            data1 = mat.get(lx + lBlackX[i], ly + lBlackY[i]);
            data2 = mat.get(lx + lWhiteX[i], ly + lWhiteY[i]);
            if (data1[0] == black[0] && data2[0] == white[0]) {
                lx += lBlackX[i];
                ly += lBlackY[i];
                drawing.put(lx, ly, green);
                lineXL.add(lx);
                lineYL.add(ly);
                i = 9;
            }
        }
    }

    // stop if you reach one of the edges of the image or no next pixel is found
    if (lx <= 1 || lx >= mWidth-1 || ly <= 0 || ly >= mHeight-1 || i < 9) {
        lFound = true;
    }
    // right edge of the image
    if (!rFound) {
        // check all different possibilities for the next pixel of the line
        for (i = 0; i < 8; i++) {
            data1 = mat.get(rx + rBlackX[i], ry + rBlackY[i]);
            data2 = mat.get(rx + rWhiteX[i], ry + rWhiteY[i]);
            if (data1[0] == black[0] && data2[0] == white[0]) {
                rx += rBlackX[i];
                ry += rBlackY[i];
                drawing.put(rx, ry, green);
                lineXR.add(rx);
                lineYR.add(ry);
                i = 9;
            }
        }
    }
    // stop if you reach one of the edges of the image
    if (rx <= 1 || rx >= mWidth-1 || ry <= 0 || ry >= mHeight-1 || i < 9) {
        rFound = true;
    }
    // stop if left edge and right edge of the line meet each other or left and right edge of the line have found the edge of the image
    if ((abs(lx-rx) <= 1 && abs(ly-ry) <= 1) || (rFound && lFound)) {
        lineFound = true;
    }
}

但是现在我仅以5-7 FPS的速度运行。正在处理的图像是480x220。该行的长度通常为+/- 500像素(因此循环运行500次)。我正在使用LG G6 ThinQ和android studio。

有没有办法使它更快?我从未使用过Android-NDK,是否可以期望性能提高?如果可以,提高多少?还是还有另一种更好的方式?

谢谢。

0 个答案:

没有答案