1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
class PeopleCounting{
// Class variables
Ptr<cv::BackgroundSubtractorMOG2> pMOG2 = cv::createBackgroundSubtractorMOG2(500, 16);
Mat maskBackgroundSubtracted = Mat(resizeDimension.height, resizeDimension.width, CV_8UC1);
// Thread creation code below, code called from main.
//Create thread
pthread_t threads;
pthread_attr_t attr;
void *status;
// Initialize and set thread joinable
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
// Creating thread data and initializing it
BackgroundSubstractionThreadData threadData = {CamImage, maskBackgroundSubtracted, pMOG2};
int rc;
rc = pthread_create(&threads, NULL, performBackgroundSubstraction, (void *)&threadData);
if (rc)
{
__android_log_print(ANDROID_LOG_ERROR, APPNAME, "Error: peopleCountingMainMono unable to create thread - %d",rc);
}
// free attribute and wait for the other threads
pthread_attr_destroy(&attr);
// ************** Do something else in main thread **************
// Join thread i.e. wait till completion of thread
rc = pthread_join(threads, &status);
if (rc)
{
__android_log_print(ANDROID_LOG_ERROR, APPNAME, "Error: peopleCountingMainMono unable to join - %d",rc);
}
// Using class variable **maskBackgroundSubtracted** and **pMOG2** for later use. **CamImage** (opencv mat) usually gets released automatically in general due to smart pointer implementation, not sure if it is the source of leak
}
// Note: Outside class
void *performBackgroundSubstraction(void *threadarg)
{
struct BackgroundSubstractionThreadData *my_data;
my_data = (struct BackgroundSubstractionThreadData *)threadarg;
Mat fgMask;
my_data->pMOG2F->apply(my_data->leftCamImage, fgMask, 0.002);
morphologyEx(fgMask, fgMask, MORPH_OPEN, getStructuringElement(MORPH_RECT, Size(3, 3)),Point(-1,-1),1);
morphologyEx(fgMask, fgMask, MORPH_CLOSE, getStructuringElement(MORPH_RECT, Size(11, 11)),Point(-1,-1),1);
threshold(fgMask, my_data->dst, 128, 255, THRESH_BINARY);
pthread_exit(NULL);
};
| |