简体   繁体   中英

How to copy cv::Mat & Img to unsigned char* img?opencv

How to copy "Mat& imgSrc" to "unsigned char* imgSrc"?

void BGR2NV21( unsigned char* bgrData, int w, int h, unsigned char* nv21Data)
{.............}

int enhanceRGB( cv::Mat& fieldFaceImgSrc, int iWidth,int iHeight, cv::Mat& faceImgDst)
{
    cv::imwrite("enhanceInput.jpg", fieldFaceImgSrc); //it's ok

    cv::Mat faceBackupData;
    faceBackupData = fieldFaceImgSrc.clone();       //it's ok
    cv::imwrite("enhanceput.jpg", faceBackupData);  //it's ok

    unsigned char*pcfieldFaceDataNV21 = (unsigned char*)faceBackupData .data;

    BGR2NV21(pcfieldFaceDataNV21,                   //it's bad,pcfieldFaceDataNV21  is bad data;
             iWidth, iHeight, pcfieldFaceDataNV21);
}

Thanks for your help.

Is there an error message or is the image just "wrong"?

Two obvious things to check, have you passed image width and height the right way around? It's a constant source of pain in image between camera APIs (x,y) and image processing matrix APIs (row,col)

And opencv pads rows of an image to 32bit (4byte boundaries) see Stride on image using Opencv C++

One of the reason why it may failed is about the management of the channels. OpenCV's Mat object store the data continuously if they are not a submatrix. This storage follow the raw line ordering that mean:

The matrix : |1 2 3| |4 5 6| |7 8 9|

will be stored aa linear pointer in that order : 1,2,3,4,5,6,7,8,9.

That is also true for the channels ie let supose two pixel with the value [255,64,128] and [64,12,34] corresponding the the coordinate row = 0, col =0 and row =0 col = 1 they will stored in memory continuously (the six first values of the data will be 255,64,128,64,12,34)

Now the easiest way to copy the data pointer of Mat object to another data pointer can:

std::memcpy(dst,src,rows*cols*elemsize); or if you pass the Mat object:

if(obj.isContiguous())
  std::memcpy(dst,src.ptr(),src.rows*src.step); // if you data are of type unsigned char src.step == src.cols otherwise src.step = src.cols*src.elemSize()
else
   for(int r=0;r<src.rows;r++,dst+=src.step)
      std::memcpy(dst,src.ptr(r),src.step);

so sorry,it always some questions,code is :

int enhanceRGB( cv::Mat fieldFaceImgSrc, int iWidth,int iHeight, cv::Mat& faceImgDst)
{
cv::imwrite("enhanceInput.jpg", fieldFaceImgSrc); //it's ok
    cv::Mat faceBackupData;
    faceBackupData = fieldFaceImgSrc.clone();
    cv::imwrite("enhanceput.jpg", faceBackupData);   //it's ok

    unsigned char *testdata = (unsigned char*)malloc(iWidth * iHeight * 3 * sizeof(unsigned char));
    memcpy(testdata, faceBackupData.ptr(), iWidth * iHeight * 3);
    cv::Mat FaceData(iHeight , iWidth, CV_8UC3, testdata);
    cv::imwrite("testput.jpg", FaceData);           //it's bad
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM