Search code examples
androidopencvcamerasetpixel

OpenCV - Android - low latency Camera Preview in setting pixel value


I'm working with OpenCV with Android version, it's a RGB Mat and when I'm trying to set new value pixel with 2 'for' to browse the camera preview. The camera display is lagging.

My goal is to change pixels value near to Red colors with a standard Blue color for example. When i tried to focus on the problem, I noticed that it's the browsing of display which is lagging the result.

Above, my browsing code with these for :

for (int i=0; i<480; i++)
        {
            for (int j=0; j<720; j++)
            {          

                int red = (int) rgba.get(j,i)[0];
                int green = (int) rgba.get(j,i)[1];
                int blue = (int) rgba.get(j,i)[2];

                Log.v(TAG, "RED=" + red);
                if(red <= 50){
                    rgba.put(j, i, 0, 0, 255, 0);
                }

            }
        }

// THEN THE RESULT LAGS !

Is there an other method to browse without latency or an issue ?

Thanks guys!


Solution

  • This is a C++ implementation. You could write one yourself in Java or use native code.

    #include "opencv2/highgui/highgui.hpp"
    #include <opencv2/imgproc/imgproc.hpp>
    #include <stdio.h>
    
    using namespace cv;
    using namespace std;
    
    int main(int argc, char* argv[]) {
        Mat img = imread(argv[1]);
        Mat lut(1, 256, CV_8UC3);
    
        for(int i=0; i<256; i++) {
            if (i <= 50) {
                lut.at<Vec3b>(i) = Vec3b(255, 0, 0); // bgr
            } else {
                lut.at<Vec3b>(i) = Vec3b(i, i, i);
            }
        }
    
        Mat retVal;
        LUT(img, lut, retVal);
    
        imshow("img", img);
        imshow("retVal", retVal);
    
        waitKey(0);
    }