Ask Your Question
0

complement 16 bit mat and then convert it to 8 bit

asked 2015-07-13 21:02:08 -0600

NabeelKhan gravatar image

updated 2015-07-13 21:06:30 -0600

I have 16 bit openCV Mat. I am looking to complement the MAT and then convert it into 8 bit openCV Mat. I have done it in MATLAB but don't know how to do it in openCV.

My MATLAB code is:

imComplement = imcomplement(image);

img8Bit = im2uint8(imComplement);

where image is 16 bit

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
1

answered 2015-07-14 01:55:19 -0600

LBerger gravatar image

updated 2015-08-15 14:05:13 -0600

invert every bit in an array

Converts an array to another data type with optional scaling.

Mat a=imread("lena.jpg",cv::IMREAD_UNCHANGED);

Mat b,c; bitwise_not(a,b); b.convertTo(c,CV_32F);

edit flag offensive delete link more
0

answered 2015-08-15 12:31:31 -0600

Mat imcomplement(Mat I)

{

/*Result the complement of a given image*/
if(I.channels()==3)
{
    for(int y = 0; y < I.rows; y++)  
    {  
        for ( int x = 0; x < I.cols; x++)  
        {  
            for(int i=0;i<3;i++)
                I.at<Vec3b>(y,x)[i]=255-I.at<Vec3b>(y,x)[i];
        }
    }
}

else
{
    for(int y = 0; y < I.rows; y++)  
    {  
        for ( int x = 0; x < I.cols; x++)  
        {  
            I.at<uchar>(y,x)=255-I.at<uchar>(y,x);
        }
    }
}
return I;

}

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-07-13 21:02:08 -0600

Seen: 2,926 times

Last updated: Aug 15 '15