Migration to Opencv 4.2.0 85/229985/3 accepted/tizen/unified/20200417.152825 submit/tizen/20200407.083853 submit/tizen/20200410.044903 submit/tizen/20200414.011245
authorTae-Young Chung <ty83.chung@samsung.com>
Tue, 7 Apr 2020 04:40:32 +0000 (13:40 +0900)
committerTae-Young Chung <ty83.chung@samsung.com>
Tue, 7 Apr 2020 07:01:17 +0000 (16:01 +0900)
While migration, refactoring imgcv by changing
legacy opencv c api to c++.

Change-Id: I105315cda1aceee912a7576347de0466673af1e9
Signed-off-by: Tae-Young Chung <ty83.chung@samsung.com>
imgcv/include/mm_util_imgcv_internal.h
imgcv/mm_util_imgcv.cpp

index 1682cd4..d7e102a 100755 (executable)
@@ -23,6 +23,7 @@
 #include <string.h>
 #include <opencv2/core.hpp>
 #include <opencv2/imgproc.hpp>
+#include <opencv2/core/fast_math.hpp>
 
 #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;
 
index 0d04d59..b9b589e 100755 (executable)
 #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<uchar>(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);