matrices as function arguments

asked 2016-08-20 07:53:40 -0600

updated 2016-08-20 08:27:42 -0600

I'm dealing with c and cpp files in my project. I've some data in matrix form in .cpp file now and I want to use this matrix data in .c file. As .c files don't use matrices but only .cpp files use... so, matrices can't be passed accross functions in this case.

How should I deal with such situation in openCV?

Example:

I've the following function in .cpp file.

extern "C"
{
    unsigned char *img_four_regions(unsigned char *img1);
}


unsigned char *img_four_regions(unsigned char *img1)
{
    unsigned char *top_left;
top_left = img1;

cv::Mat TempMat = cv::Mat(138, 640, CV_8UC1, top_left);

Mat temp_top_right;
Mat temp_top_left;
Mat temp_bottom_right;
Mat temp_bottom_left;

TempMat(Range(0, 69), Range(0, 320)).copyTo(temp_top_right);
imshow("top right", temp_top_right);
waitKey(0);

TempMat(Range(0, 69), Range(321, 640)).copyTo(temp_top_left);
imshow("top right", temp_top_left);
waitKey(0);

TempMat(Range(70, 138), Range(0, 320)).copyTo(temp_bottom_right);
imshow("top right", temp_bottom_right);
waitKey(0);

TempMat(Range(70, 138), Range(321, 640)).copyTo(temp_bottom_left);
imshow("top right", temp_bottom_left);
waitKey(0);

Now I want to use the matrices named "temp_bottom_right, temp_bottom_left, temp_top_right, temp_top_left" in .c file by calling this function.

edit retag flag offensive close merge delete

Comments

sorry, but atm. this is too broad. can you specify your exact needs, and give an example ?

berak gravatar imageberak ( 2016-08-20 08:07:18 -0600 )edit
1

please check the edited question now.

Ayesha Siddique gravatar imageAyesha Siddique ( 2016-08-20 08:28:58 -0600 )edit

You can't use the cv::Mat object, but you can access the memory if you want.

To do that, have four output pointers each pointing to the amount of memory needed for that section. Then construct temp_top_right and the others like you did TempMat and do the copyTo just like you do. Now those pointers contain just the quarter of the image.

Tetragramm gravatar imageTetragramm ( 2016-08-20 13:41:49 -0600 )edit
1

your current "C" concept has some flaws:

  • if you give a pointer for input, or return another as result : "who owns the memory ? who is responsible for allocating, cleaning up ?
  • just a pointer might be not enough. you also need width/height, also, there might be "non-continuous" Mat's (e.g. from a ROI)
berak gravatar imageberak ( 2016-08-22 10:12:00 -0600 )edit

non-continuous mats meaning?

Ayesha Siddique gravatar imageAyesha Siddique ( 2016-08-22 14:30:03 -0600 )edit

?????????????

Ayesha Siddique gravatar imageAyesha Siddique ( 2016-08-23 05:51:45 -0600 )edit

like a roi, think of this:

-----------------------------------
|                                  |
|                                  |
|            --------------        |
|            |             |       |
|            |             |       |
|            --------------        |
|                                  |
|                                  |
-----------------------------------
berak gravatar imageberak ( 2016-08-23 05:55:45 -0600 )edit