From: Tae-Young Chung Date: Tue, 7 Apr 2020 04:40:32 +0000 (+0900) Subject: Migration to Opencv 4.2.0 X-Git-Tag: accepted/tizen/unified/20200417.152825^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=32b300d41e7c96df44ad4e6506ecc251f4b757e5;p=platform%2Fcore%2Fmultimedia%2Flibmm-utility.git Migration to Opencv 4.2.0 While migration, refactoring imgcv by changing legacy opencv c api to c++. Change-Id: I105315cda1aceee912a7576347de0466673af1e9 Signed-off-by: Tae-Young Chung --- diff --git a/imgcv/include/mm_util_imgcv_internal.h b/imgcv/include/mm_util_imgcv_internal.h index 1682cd4..d7e102a 100755 --- a/imgcv/include/mm_util_imgcv_internal.h +++ b/imgcv/include/mm_util_imgcv_internal.h @@ -23,6 +23,7 @@ #include #include #include +#include #ifdef __cplusplus extern "C" { @@ -30,7 +31,7 @@ typedef struct _mm_util_imgcv_s { - IplImage *inImg; + cv::Mat inImg; int hBins; /**< Number of bins of Hue channle */ int sBins; /**< Number of bins of Saturation channle */ @@ -40,8 +41,6 @@ typedef struct _mm_util_imgcv_s { float sRanges[2]; /**< Range of Saturation */ float vRanges[2]; /**< Range of Value */ - int sizeOfHist[3]; /**< array of bins; hBins,sBins,vBins */ - int width; int height; diff --git a/imgcv/mm_util_imgcv.cpp b/imgcv/mm_util_imgcv.cpp index 0d04d59..b9b589e 100755 --- a/imgcv/mm_util_imgcv.cpp +++ b/imgcv/mm_util_imgcv.cpp @@ -36,25 +36,24 @@ #define DEFAULT_RANGE_SATURATION 256 #define DEFAULT_RANGE_VALUE 256 -static int __mm_util_imgcv_init(mm_util_imgcv_s *handle, int width, int height) +static int __mm_util_imgcv_init(mm_util_imgcv_s *handle, int width, int height, void *image_buffer) { mm_util_fenter(); handle->width = width; handle->height = height; - handle->inImg = cvCreateImageHeader(cvSize(width, height), IPL_DEPTH_8U, RGB_COLOR_CHANNELS); - if (handle->inImg == NULL) + handle->inImg = cv::Mat(cv::Size(width, height), CV_8UC(RGB_COLOR_CHANNELS), image_buffer); + mm_util_debug("buffer [%p]", image_buffer); + if (handle->inImg.empty()) { + mm_util_error("inImg is empty [%p]", handle->inImg.data); return MM_UTIL_ERROR_INVALID_OPERATION; + } handle->hBins = DEFAULT_NUM_HBINS; handle->sBins = DEFAULT_NUM_SBINS; handle->vBins = DEFAULT_NUM_VBINS; - handle->sizeOfHist[0] = handle->hBins; - handle->sizeOfHist[1] = handle->sBins; - handle->sizeOfHist[2] = handle->vBins; - handle->hRanges[0] = 0; handle->hRanges[1] = DEFAULT_RANGE_HUE; handle->sRanges[0] = 0; @@ -71,46 +70,21 @@ static void __mm_util_imgcv_uninit(mm_util_imgcv_s *handle) { mm_util_fenter(); - if (handle->inImg != NULL) { - cvReleaseImageHeader(&handle->inImg); - handle->inImg = NULL; - } - - mm_util_fleave(); -} - -static int __mm_util_imgcv_set_buffer(mm_util_imgcv_s *handle, void *image_buffer) -{ - mm_util_fenter(); - - unsigned char *buffer = (unsigned char *)image_buffer; - - mm_util_debug("image_buffer [%p], width [%d]", buffer, handle->width); - - cvSetData(handle->inImg, buffer, RGB_COLOR_CHANNELS*(handle->width)); - if (handle->inImg == NULL) - return MM_UTIL_ERROR_INVALID_OPERATION; - mm_util_fleave(); - - return MM_UTIL_ERROR_NONE; } static void _convert_hsv_to_rgb(int hVal, int sVal, int vVal, float *rVal, float *gVal, float *bVal) { mm_util_fenter(); - CvMat *mat1 = cvCreateMat(1, 1 , CV_8UC3); - cvSet2D(mat1, 0, 0, cvScalar((double)hVal, (double)sVal, (double)vVal, 0.0)); - - CvMat *mat2 = cvCreateMat(1, 1, CV_8UC3); + cv::Mat mat1(cv::Size(1,1), CV_8UC3, cv::Scalar((double)hVal, (double)sVal, (double)vVal)); + cv::Mat mat2; + cv::cvtColor(mat1, mat2, cv::COLOR_HSV2BGR); - cvCvtColor(mat1, mat2, CV_HSV2BGR); - - CvScalar bgr = cvGet2D(mat2, 0, 0); - *bVal = (float)bgr.val[0]; - *gVal = (float)bgr.val[1]; - *rVal = (float)bgr.val[2]; + uchar* bgrVal = mat2.ptr(0); + *bVal = (float)bgrVal[0]; + *gVal = (float)bgrVal[1]; + *rVal = (float)bgrVal[2]; mm_util_debug("from HSV[%f, %f, %f]", (float)hVal, (float)sVal, (float)vVal); mm_util_debug("to BGR[%f, %f, %f]", *bVal, *gVal, *rVal); @@ -119,10 +93,6 @@ static void _convert_hsv_to_rgb(int hVal, int sVal, int vVal, float *rVal, float static int __mm_util_imgcv_calculate_hist(mm_util_imgcv_s *handle, unsigned char *rgb_r, unsigned char *rgb_g, unsigned char *rgb_b) { - int nh = 0; - int ns = 0; - int nv = 0; - int hVal = 0; int sVal = 0; int vVal = 0; @@ -131,70 +101,41 @@ static int __mm_util_imgcv_calculate_hist(mm_util_imgcv_s *handle, unsigned char float gVal = 0.f; float bVal = 0.f; - unsigned int maxBinVal = 0; - int max_bin_idx[3] = {-1, -1, -1}; - - mm_util_fenter(); + double maxVal = 0.0f; + int maxIdx[] = {0, 0, 0}; - IplImage *hsvImg = cvCreateImage(cvSize(handle->width, handle->height), IPL_DEPTH_8U, HSV_COLOR_CHANNELS); - IplImage *hImg = cvCreateImage(cvSize(handle->width, handle->height), IPL_DEPTH_8U, 1); - IplImage *sImg = cvCreateImage(cvSize(handle->width, handle->height), IPL_DEPTH_8U, 1); - IplImage *vImg = cvCreateImage(cvSize(handle->width, handle->height), IPL_DEPTH_8U, 1); + cv::Mat hist; + cv::Mat hsvImg; - if (!hsvImg || !hImg || !sImg || !vImg) { - mm_util_error("fail to cvCreateImage()"); - return MM_UTIL_ERROR_INVALID_OPERATION; - } + mm_util_fenter(); - IplImage *planes[] = {hImg, sImg, vImg}; + /* convert colorspace to HSV */ + cv::cvtColor(handle->inImg, hsvImg, cv::COLOR_RGB2HSV); - cvCvtColor(handle->inImg, hsvImg, CV_RGB2HSV); - cvSplit(hsvImg, hImg, sImg, vImg, NULL); + const float *ranges[] = {handle->hRanges, handle->sRanges, handle->vRanges}; + int histSize[] = {handle->hBins, handle->sBins, handle->vBins}; - float *ranges[] = {handle->hRanges, handle->sRanges, handle->vRanges}; - int histsize[] = {handle->sizeOfHist[0], handle->sizeOfHist[1], handle->sizeOfHist[2]}; + int channels[] = {0, 1, 2}; - /* create histogram*/ - CvHistogram *hist = cvCreateHist(HISTOGRAM_CHANNELS, histsize, CV_HIST_ARRAY, ranges, 1); - if (hist == NULL) - return MM_UTIL_ERROR_INVALID_OPERATION; + /* calculate historgram */ + cv::calcHist(&hsvImg, 1, channels, cv::Mat(), hist, 3, histSize, ranges, true, false); - cvCalcHist(planes, hist, 0, NULL); - - for (nh = 0; nh < (handle->hBins); nh++) { - for (ns = 0; ns < (handle->sBins); ns++) { - for (nv = 0; nv < (handle->vBins); nv++) { - unsigned int binVal = (unsigned int)cvGetReal3D((hist)->bins, nh, ns, nv); - if (binVal > maxBinVal) { - maxBinVal = binVal; - max_bin_idx[0] = nh; - max_bin_idx[1] = ns; - max_bin_idx[2] = nv; - } - } - } - } + /* find an idx indicating max value in the histogram */ + cv::minMaxIdx(hist, NULL, &maxVal, NULL, maxIdx); - rVal = gVal = bVal = 0.0f; - hVal = cvRound((max_bin_idx[0]+1)*(handle->hRanges[1]/(float)handle->hBins)); - sVal = cvRound((max_bin_idx[1]+1)*(handle->sRanges[1]/(float)handle->sBins)); - vVal = cvRound((max_bin_idx[2]+1)*(handle->vRanges[1]/(float)handle->vBins)); + hVal = cvRound((maxIdx[0]+1)*(handle->hRanges[1]/(float)handle->hBins)); + sVal = cvRound((maxIdx[1]+1)*(handle->sRanges[1]/(float)handle->sBins)); + vVal = cvRound((maxIdx[2]+1)*(handle->vRanges[1]/(float)handle->vBins)); + /* convert hsv value to rgb */ _convert_hsv_to_rgb(hVal, sVal, vVal, &rVal, &gVal, &bVal); - mm_util_debug("nh[%d], ns[%d], nv[%d]", max_bin_idx[0], max_bin_idx[1], max_bin_idx[2]); + mm_util_debug("nh[%d], ns[%d], nv[%d]", maxIdx[0], maxIdx[1], maxIdx[2]); mm_util_debug("h[%d], s[%d], v[%d]", hVal, sVal, vVal); *rgb_r = rVal; *rgb_g = gVal; *rgb_b = bVal; - cvReleaseImage(&hsvImg); - cvReleaseImage(&hImg); - cvReleaseImage(&sImg); - cvReleaseImage(&vImg); - - cvReleaseHist(&hist); - mm_util_fleave(); return MM_UTIL_ERROR_NONE; @@ -205,22 +146,15 @@ int mm_util_cv_extract_representative_color(void *image_buffer, int width, int h mm_util_fenter(); mm_util_retvm_if(!image_buffer, MM_UTIL_ERROR_INVALID_PARAMETER, "invalid image_buffer"); - mm_util_imgcv_s *handle = (mm_util_imgcv_s *)calloc(1, sizeof(mm_util_imgcv_s)); mm_util_retvm_if(!handle, MM_UTIL_ERROR_OUT_OF_MEMORY, "fail to create handle"); - int ret = __mm_util_imgcv_init(handle, width, height); + int ret = __mm_util_imgcv_init(handle, width, height, image_buffer); if (ret != MM_UTIL_ERROR_NONE) { mm_util_error("#ERROR#: Fail to __mm_util_imgcv_init: ret=%d", ret); goto ERROR; } - ret = __mm_util_imgcv_set_buffer(handle, image_buffer); - if (ret != MM_UTIL_ERROR_NONE) { - mm_util_error("#ERROR#: Fail to __mm_util_imgcv_set_buffer: ret=%d", ret); - goto ERROR; - } - ret = __mm_util_imgcv_calculate_hist(handle, r_color, g_color, b_color); if (ret != MM_UTIL_ERROR_NONE) { mm_util_error("#ERROR#: Fail to __mm_util_imgcv_calculate_hist: ret=%d", ret);