4

I've implemented a GetSubImage function in order to extract a portion of an image. I use SetROI, Copy, and ResetROI. But this not works when Parallel Tasks are using the same image because of the SetROI is not parallelizable.

Any other way to extract portion of an image that can run concurrent?

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
J.CSF
  • 51
  • 4

2 Answers2

2

You can create a temporary matrix header that only points to a part of the matrix. Then you can use the copyTo() member function on that header. Example in C++:

Mat GetSubImage(const Mat& source, const Rect &region)
{
    Mat dest;
    Mat roi(source, region);
    roi.copyTo(dest);
    return dest;
}

This way, neither the data nor the header of source are affected by the operation, so you can perform it concurrently.

ypnos
  • 50,202
  • 14
  • 95
  • 141
  • Thanks. But I'm using OpenCvSharp library and the CvMat class does not have thins constructor... – J.CSF Sep 06 '16 at 10:56
  • What are you talking about? It is right there: http://shimat.github.io/opencvsharp/html/bf9a14ed-6ebb-704b-2372-7e104ef77196.htm – ypnos Sep 06 '16 at 10:58
1

Finally, to extract a portion of an image, I'm using this function. Used on C# and with the OpenCVSharp wraper:

    static public IplImage GetSubImage(IplImage ipl, CvRect r)
    {
        CvMat submat;
        CvMat mat = ipl.GetSubRect(out submat, r);
        IplImage i = new IplImage(r.Width, r.Height, ipl.Depth, ipl.NChannels);
        Cv.Copy(mat, i);

        mat.Dispose();
        submat.Dispose();           

        return i;
    }
J.CSF
  • 51
  • 4
  • Thank you for taking the time and posting the solution! That's the spirit! I can only suggest to you to switch to the modern API though. It was introduced in OpenCV 2.0 and it is 3.1 now already. – ypnos Sep 07 '16 at 14:40
  • Which API? I'm using this https://github.com/shimat/opencvsharp and it's very complete and perfect for me. – J.CSF Sep 07 '16 at 14:46
  • Yes OpenCVSharp comes with two APIs. The old OpenCV 1.x one and the new OpenCV 2.x, 3.x one. See https://github.com/shimat/opencvsharp#usage Do you See CvMat, IplImage there? No! Because it is the new API they are using in their example. Mat == new, CvMat == old. – ypnos Sep 07 '16 at 14:48
  • Ok great! Using 2.4 version here. I'll take a look to the 3.x version but it's not easy to update libraries and api's when you have soft all over the world. – J.CSF Sep 07 '16 at 15:07
  • The new API is already part of 2.4. You just need to stop using cvMat and start using Mat.. – ypnos Sep 07 '16 at 17:27