I'm having issues getting C# Emgu.NET (opencv) to produce the same results as similar C++ code. It is pretty simple, I am just opening an image as a 16UC1 and converting to 32FC1. Here's the C++ code:
inImg = imread(inPath.c_str(), -1);
cv::Mat fltImg;
inImg.convertTo(fltImg, CV_32FC1 );
I'm saving the raw data bytes to a file like this:
std::ofstream fout;
fout.open(filename, std::ios::binary | std::ios::out);
fout.write((char*)img.data, img.rows * img.cols * bytesPerPixel);
fout.close();
I'm saving both inImg and fltImg for debugging purposes (2 and 4 bytesPerPixel respectively)
On the Emgu.NET side:
var handle = GCHandle.Alloc(pixelValues, GCHandleType.Pinned);
try
{
var ptr = handle.AddrOfPinnedObject();
var mat = new Mat(new Size(height, width), DepthType.Cv16U,
1, ptr, width * 2);
var convMat = new Mat();
mat.ConvertTo(convMat, DepthType.Cv32F);
}
finally
{
if (handle.IsAllocated)
{
handle.Free();
}
}
I'm saving the raw files int the following way:
private void SaveDebugImage(string filename, Mat img, int depth)
{
byte[] data = MatData(img, depth);
File.WriteAllBytes(filename, data);
}
private byte[] MatData(Mat img, int pixelBytes)
{
var ptr = img.DataPointer;
int size = img.Rows * img.Cols * pixelBytes;
var outBytes = new byte[size];
Marshal.Copy(ptr, outBytes, 0, size);
return outBytes;
}
Comparing the first (16 bpp) images, they are identical, but the ones after the call to convertTo result in different images:
C++ C++ output
C# C# output
Any ideas on what could be going wrong?
Thanks!