1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| int width = mat.cols;
int height = mat.rows;
int channels = mat.channels();
CImage CI;
CI.Destroy();
CI.Create(width, height, 8*channels );
RGBQUAD* ColorTable;
int MaxColors=256;
ColorTable = new RGBQUAD[MaxColors];
CI.GetColorTable(0,MaxColors,ColorTable);
for (int i=0; i<MaxColors; i++)
{
ColorTable[i].rgbBlue = (BYTE)i;
ColorTable[i].rgbGreen = (BYTE)i;
ColorTable[i].rgbRed = (BYTE)i;
}
CI.SetColorTable(0,MaxColors,ColorTable);
delete []ColorTable;
uchar* ps;
uchar* pimg = (uchar*)CI.GetBits();
int step = CI.GetPitch();
for (int i = 0; i < height; ++i)
{
ps = (mat.ptr<uchar>(i));
for ( int j = 0; j < width; ++j )
{
if ( channels == 1 )
{
*(pimg + i*step + j) = ps[j];
}
else if ( channels == 3 )
{
for (int k = 0 ; k < 3; ++k )
{
*(pimg + i*step + j*3 + k ) = ps[j*3 + k];
}
}
}
} |
Partager