Ask Your Question

Revision history [back]

As mentioned in the comments it is recommended to use LUT for this purpose! Here is a Sample implementation Hope This helps!

  #include "stdafx.h"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{    
    Mat mSrc_Bgr(5, 5, CV_8UC3),mDst_Bgr;    
    randu(mSrc_Bgr, Scalar::all(0), Scalar::all(255));

    cout << "Input Mat = " << endl << format(mSrc_Bgr,3) << endl << endl;


    Mat mLUT(1, 256, CV_8UC3);
    for (int i=0; i<256; i++)
    {
        mLUT.at<Vec3b>(i)[0]= 256/32;       // Blue channel  (B)= Val /32
        mLUT.at<Vec3b>(i)[1]= i;            // Green channel  No Change
        mLUT.at<Vec3b>(i)[2]= i;            // Red channel  No Change
    }

    LUT(mSrc_Bgr,mLUT,mDst_Bgr);

    cout << "Output Mat = " << endl << format(mSrc_Bgr,3) << endl << endl;

    return 0;
}

As mentioned in the comments it is recommended to use LUT for this purpose! Here is a Sample implementation Hope This helps!

  #include "stdafx.h"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{    
    Mat mSrc_Bgr(5, 5, CV_8UC3),mDst_Bgr;    
    randu(mSrc_Bgr, Scalar::all(0), Scalar::all(255));

    cout << "Input Mat = " << endl << format(mSrc_Bgr,3) << endl << endl;


    Mat mLUT(1, 256, CV_8UC3);
    for (int i=0; i<256; i++)
    {
        mLUT.at<Vec3b>(i)[0]= 256/32;       // Blue channel  (B)= Val /32
        mLUT.at<Vec3b>(i)[1]= i;            // Green channel  No Change
        mLUT.at<Vec3b>(i)[2]= i;            // Red channel  No Change
    }

    LUT(mSrc_Bgr,mLUT,mDst_Bgr);

    cout << "Output Mat = " << endl << format(mSrc_Bgr,3) format(mDst_Bgr,3) << endl << endl;

    return 0;
}