Ask Your Question

Tumelo M. Motloutsi's profile - activity

2019-03-29 03:04:51 -0600 received badge  Famous Question (source)
2017-05-30 11:29:33 -0600 received badge  Student (source)
2017-02-26 11:52:03 -0600 received badge  Notable Question (source)
2016-12-06 21:53:45 -0600 received badge  Famous Question (source)
2016-10-24 09:04:20 -0600 received badge  Popular Question (source)
2016-05-30 12:44:50 -0600 received badge  Notable Question (source)
2016-02-23 16:31:07 -0600 received badge  Popular Question (source)
2015-02-24 00:58:26 -0600 commented question Convert DLL IntPtr to byte[] to bitmap

@berak I tried that, thanks! but nothing loaded in the PictureBox. I debugged the code and the value ptr changes to indicate that it received something. I've been doing some more reading. . .is it not because char * in C++ is unmanaged and cannot be interpreted by C#?

2015-02-23 08:38:46 -0600 commented question Convert DLL IntPtr to byte[] to bitmap

@berak thanks for your response, the Bitmap constructor does not take IntPtr, it takes a System.Drawing.Image type. I tried to cast it but no luck @Eduardo I need to do this exercise because later I am going to read the image using emgucv and send it C++ for processing using opencv. Your next question might be to just use Emgucv since its equivalent to opencv and has most methods wrapped. Well I might just do that . . .port everything to C# Emgucv and compromise performance.

2015-02-23 03:45:24 -0600 asked a question Convert DLL IntPtr to byte[] to bitmap

Initially I thought to send an image from C++ opencv to C# required converting the opencv mat to an equivalent emgucv object. I have come to learn that it just requires unpacking the mat object to a type that C# can understand, which is uchar . . .apparently same as byte in C#

I have a C++ program that reads and an image and converts it to uchar (which is what a Mat object is anyway).

void DisplayImage::ReadImage()
{
    cv::Mat image = cv::imread("2015-02-05.jpg");
    image_rows = image.rows;
    image_cols = image.cols;
    image_type  = image.type();
    //image_uchar is data member of uchar*
    image_uchar = MatToBytes(image);
}

uchar *DisplayImage::MatToBytes(cv::Mat image)
{
    int image_size = image.total() * image.elemSize();
    uchar * image_uchar = new uchar[image_size];
    //image_uchar is a class data member of uchar*
    std::memcpy(image_uchar,image.data,image_size * sizeof(uchar));
    return image_uchar;
}

cv::Mat DisplayImage::BytesToMat()
{
    cv::Mat img(image_rows,image_cols,image_type,image_uchar,cv::Mat::AUTO_STEP);
    return img;
}

In the main program I assign a Mat object to the returned value from BytesToMat() and image looks fine.

My DLL looks like below

DisplayImage displayImage = DisplayImage();

extern "C" __declspec(dllexport) void SetImage()
{
    displayImage.ReadImage();   
}

extern "C" __declspec(dllexport) uchar *GetImage()
{
    return displayImage.image_uchar;
}

Just by the way; I firstly started by assigning the pointer to uchar (in C++) with a simple text and displayed it C# by Marshaling returned IntPtr (from dll) to PtrToStringAnsi and it worked.

[DllImport(@".\WrapperForPlayer.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
unsafe public static extern IntPtr GetImage();

[DllImport(@".\WrapperForPlayer.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
    public static extern void SetImage();

static void Main(string[] args)
{
    //byte[] image_byte  = new byte[500];
    SetImage();
    IntPtr intPtr = GetImage();
    unsafe
    {
        byte* image_byte = (byte*)intPtr;

    }
    Console.Read();
}

The only difference now is that the returned IntPtr contains address to image data which I want to turn to byte[] and then it will be easy to convert to Bitmap. How can I do that?

2015-02-23 01:32:04 -0600 received badge  Scholar (source)
2015-02-23 01:32:01 -0600 commented question c++ convert uchar * to opencv mat

Thanks, the link was 'slots' helpful although there parts I find confusing there but I will read some more as I am still leaerning

2015-02-23 01:28:45 -0600 commented answer c++ convert uchar * to opencv mat

Thanks for your response. I have made modifications as below

uchar *DisplayImage::MatToBytes(cv::Mat image)
{
        //class data members of ints
    image_rows = image.rows;
    image_cols = image.cols;
    image_type  = image.type();

    int image_size = image.total() * image.elemSize();
    uchar * image_uchar = new uchar[image_size];
    std::memcpy(image_uchar,image.data,image_size * sizeof(uchar));
    return image_uchar;
}

cv::Mat DisplayImage::BytesToMat()
{
    cv::Mat img(image_rows,image_cols,image_type,image_uchar,cv::Mat::AUTO_STEP);
    return img;
}
2015-02-22 15:28:59 -0600 asked a question c++ convert uchar * to opencv mat

I have converted a cv::Mat object to uchar *

uchar *DisplayImage::MatToBytes(cv::Mat image)
{
    //class data members
image_rows = image.rows;
image_cols = image.cols;
image_type  = image.type();

    int image_size = image.total() * image.elemSize();
    uchar * image_uchar = new uchar[image_size];

    std::vector<uchar> v_char;
    for(int i = 0; i < image.rows; i++)
    {
        for(int j = 0; j < image.cols; j++)
        {
            v_char.push_back(*(uchar*)(image.data+ i + j));
        }
    }
    //image_uchar is a class data member
    image_uchar = &v_char[0];

    //cvWaitKey(5000);
    return image_uchar;
}

I now want to convert the uchar* back to Mat object. I tried using the Mat clone function but I don't really understand all the parameters for the default Mat constructor. And then I read somewhere that I can just use the default Mat constructor but I don't know what that last parameter (size_t step) means.

cv::Mat DisplayImage::BytesToMat()
{
    cv::Mat img =   cv::Mat(image_rows,image_cols,CV_16UC1,image_byte,0); //I  am not sure about the last parameter here

    cv::namedWindow("MyWindow");
    cv::imshow("MyWindow",img);
    cvWaitKey(500);
    return img;
}

How do you convert uchar * back to Mat object? The image is a colour image by the way