1

I'm very new at C++ and I'm trying to create a DLL which uses OpenCV library.

My DLL gets a raw image from other application and creates a MAT from the application's memory buffer. I send the buffer's address, which has a raw image, to the DLL and get raw image to OpenCV. This part works. But after processing image with OpenCV, I can't write raw image to same memory address.

This is the code snippet that I've tried:

fn_export double createImage(char* address  double width, double height) {
  
  unsigned char* pBuffer = (unsigned char*)address;
  memcpy(&pBuffer,&address, sizeof(pBuffer));
  cv::Mat img = cv::Mat(height,width, CV_8UC4, pBuffer);
  cv::imshow("Original", img);

  memcpy(&address, &img.data[0], sizeof(address));

  return 1;
}

char* address is memory address from my application. Other application's buffer doesn't change this way. Anybody has any advice about this situation?

stateMachine
  • 5,227
  • 4
  • 13
  • 29
Mb B
  • 49
  • 1
  • 5
  • In memcpy() when you take sizeof(address), this is incorrect because this is just copying the pointer not the actual data at the location where the pointer points to. Also &address[0] and &address are the same thing. –  Jul 28 '20 at 10:36
  • Thank you for your comment. Yes, you are right. I realized that some definitions in my code are unnecessary. – Mb B Aug 02 '20 at 18:07

1 Answers1

2

Ok. I solved this issue ;

    Mat img = Mat(height, width, CV_8UC4, address);
cv::imshow("Image from GM", img);
// same image copy to buffer back;
memcpy(&address[0], &img.data[0], width*height*4.);
Mb B
  • 49
  • 1
  • 5