Chameera Subasinghe
Chameera Subasinghe

Reputation: 61

Applying pixelwise operations are slow

@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {

    Mat rgb=inputFrame.rgba();
    for(int y=0;y<rgb.cols();++y){
        for(int x=0;x<rgb.rows();++x){
            double[]pixelValues=rgb.get(x,y);
            double redVal=pixelValues[0];
            double greenVal=pixelValues[1];
            double blueVal=pixelValues[2];
            double alphaVal=pixelValues[3];
            double gryVal=(redVal+greenVal+blueVal)/3;
            rgb.put(x,y,gryVal,gryVal,gryVal,alphaVal);
        }
    }
    return rgb;
}

This is my code to change the pixel values of a cameraStream displayed inside a JavaCameraView component. The thing is this is very slow. less than 3 fps. I know there is a faster way to get gray scale images {.rgba() => .gray() or using Improc.cvt()}. Though i need the freedom to handle pixels by my self. my ultimate goal is to get something which i can adjust red, green, blue colors as i like.

Is there is a way to make this atleast get 30fps (smooth framerates)?

Upvotes: 1

Views: 371

Answers (1)

hariprasad
hariprasad

Reputation: 858

Get and put operations for every pixel will surely results in poor fps because of overhead of native calls.Instead ,you should call get and put operations only once.i.e outside the for loop.Single call to get/put them into java primitive arrays and do operations on them.This way you can do your own pixel operations and it will boost performance greatly(more closer to imgproc.cvt()). http://answers.opencv.org/question/5/how-to-get-and-modify-the-pixel-of-mat-in-java/ This will show you an example of how to make single get/put calls(Not only the actual answer but also see in comments).

Upvotes: 1

Related Questions