Search code examples
c++opencvmat

Convert vec3b into mat


I have color image in im, i want to get the pixel value of 3 channels image using vec3b using the following code

    for (int i = 0; i < im.rows; i++)
{
    for (int j = 0; j < im.cols; j++)
    {
        for (int k = 0; k < nChannels; k++)
        {
            zay[k] = im.at<Vec3b>(i, j)[k]; //get the pixel value and assign to new vec3b variable zay


        }

    }

}

After that, i want to multiply the following mat 3x3 filter with that vec3b in zay

    Filter= (Mat_<double>(3, 3) << 0, 0, 0,
                                0, 1, 0,
                                0, 0, 0);

How to convert vec3b into mat matrix so i can multiplied with mat Filter?? And vec3b is an array 3x1? Thanks


Solution

  • Didnt try but should work:

    cv::Mat DoubleMatFromVec3b(cv::Vec3b in)
    {
        cv::Mat mat(3,1, CV_64FC1);
        mat.at <double>(0,0) = in [0];
        mat.at <double>(1,0) = in [1];
        mat.at <double>(2,0) = in [2];
    
        return mat;
    };