mmuncada
mmuncada

Reputation: 538

Android OpenCV convertTo() from CV_32FC4 to CV_8UC4

I'm just new to Android & OpenCV and currently I'm using this project for real-time image processing. I am using the native code cpp from the project to implement the algorithm that I need which involves mathematical operations with float numbers applying modifications for the RGB channels for each pixel. Therefore I think it's just appropriate to use CV_32FC4 for the first matrix. Adjustments I did in the cpp:

Mat mFrame(height, width, CV_32FC4, (unsigned char *)pNV21FrameData);
Mat mResult(height, width, CV_8UC4, (unsigned char *)poutPixels);

for(int y = 0 ; y < height ; y++){
    for(int x = 0 ; x < width ; x++){
        Vec3b BGR = mFrame.at<Vec3b>(Point(x,y));
        // BGR Pixel Manipulations
        mFrame.at<Vec3b>(Point(x,y)) =  BGR;
    }
}

mFrame.convertTo(mResult, CV_8UC4, 1/255.0);

After implementing the algorithm, I'll need to convert the matrix to BGRA since it is the requirement so I'll used CV_8UC4. But when I run the program there is a problem with the display: link for actual image

Output Image

The white objects in the right side seems to be multiple instances of ruined version of what is displayed. There's nothing like that with the original code that is Canny Edge Detection so I suppose it's not a problem with my device. What could possibly be the problem?

Upvotes: 0

Views: 1055

Answers (1)

Miki
Miki

Reputation: 41765

  • You're working on a 4 channel float matrix, so you should access it with Vec4f.
  • You don't need to initialize the output matrix from a OpenCV function in general. So just use Mat mResult; and cvtColor will take care of creating it correctly.
  • You don't need to access a pixel with Point, simply pass the rows and cols coordinates.

So the code becomes:

Mat mFrame(height, width, CV_32FC4, (unsigned char *)pNV21FrameData);

for(int y = 0 ; y < height ; y++){
    for(int x = 0 ; x < width ; x++){
        Vec4f BGRA = mFrame.at<Vec4f>(y,x);
        // BGRA Pixel Manipulations
        mFrame.at<Vec4f>(y,x) = BGRA;
    }
}

Mat mResult;
mFrame.convertTo(mResult, CV_8UC4, 1.0/255.0);

Upvotes: 1

Related Questions