I am trying to copy 16 bit monochrome values (16UC1) to the blue and green channels of an 8UC3 mat.
The commented lines will do what I want but involve several math operations per pixel. It should be faster to directly copy the 16 bit value to the two (adjacent in memory) 8 bit color channels.
The line: “pdst[j] = psrc[j];” compiles without warnings or errors but only seems to copy one byte. Is there a way to apply a static_cast or other mechanism to trick the compiler into doing what I want? This is getting into C++ that is way over my head.
cv::Mat BGRframe(grayframe.size(),CV_8UC3); // Create output frame, same size as input but with 3 8-bit channels
// work out geometry of roi in memory
int nRows = grayframe.rows;
int nCols = grayframe.cols;
if (grayframe.isContinuous()) {
nCols *= nRows;
nRows = 1;
}
// iterate through the input frame, pixel by pixel, copying each word into 2 channels
for (int i = 0; i < nRows; ++i) {
cv::Vec3b* pdst = BGRframe.ptr<cv::Vec3b>(i); //pointer to a row of the output frame
const unsigned short * psrc = grayframe.ptr<unsigned short>(i); //pointer to a row of input
for (int j = 0; j < nCols; ++j) {
// pdst[j].val[0] = (psrc[j]/256) & 0x00FF; // Put upper 8 bits of each 16 bit pixel in Blue Channel
// pdst[j].val[1] = psrc[j] & 0x00FF; // Put lower 8 bits of each 16 bit pixel in Green Channel
pdst[j] = psrc[j]; // Put 16 bits into adjacent Blue and Green Channels
pdst[j].val[2] = 0; // Set Red channel to 0 to minimize compressed size. (Could use for meta data?)
}
}
``