我刚接触Android和OpenCV,目前正在使用this项目进行实时图像处理。我正在使用项目中的native code cpp来实现所需的算法,该算法涉及浮点数的数学运算,并对每个像素的RGB通道应用修改。因此,我认为将 CV_32FC4 用于第一个矩阵是适当的。我在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);

实现算法后,由于这是必要条件,因此需要将矩阵转换为BGRA,因此我将使用 CV_8UC4 。但是当我运行程序时,显示出现问题:link for actual image

android - Android OpenCV convertTo()从CV_32FC4到CV_8UC4-LMLPHP

右侧的白色对象似乎是所显示内容毁损版本的多个实例。原始代码Ca​​nny Edge Detection没什么可比的,所以我认为这对我的设备来说不是问题。可能是什么问题?

最佳答案

  • 您正在处理4通道浮点矩阵,因此应使用Vec4f访问它。
  • 通常,您不需要初始化OpenCV函数的输出矩阵。因此,仅使用Mat mResult;,而cvtColor将负责正确创建它。
  • 您不需要使用Point访问像素,只需传递行和cols坐标即可。

  • 因此,代码变为:
    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);
    

    10-08 09:11
    查看更多