Exemple #1
0
void ofxImagePreProcess::update(const cv::UMat & _src, cv::UMat & dst){
    
    cv::UMat src = _src.clone();
    // - - - - convert into Gray
    cvtColor( src, src, COLOR_BGR2GRAY );
    
    // - - - - filter
    //cv::bilateralFilter(camerauMat, mUMat, 7, 100, 7);
    cv::Size kernel = cv::Size(5,5);
    cv::boxFilter(src, mUMat, -1, kernel);

    //- - - - - get "detials image"
    cv::absdiff(src, mUMat, difMat);
    
    //- - - multiply
    cv::multiply(difMat, 20, difMat);
    
    // - - - -temporal filtering
    if(isInitial)
    {
        tempFilteredDifMat = difMat.clone();
        cv::multiply(tempFilteredDifMat, 1, tempFilteredDifMat);
        isInitial = false;
    }else{
        cv::addWeighted(tempFilteredDifMat,0.7,difMat,0.7,0.0, tempFilteredDifMat);
    }
    
    //- - - - then blur it.
    cv::Size Blurkernel = cv::Size(55,55);
    cv::blur(tempFilteredDifMat, regionMask, Blurkernel);
    
    // - - - then Thresh it.
    cv::threshold(regionMask, regionMask, 50, 255, THRESH_BINARY_INV);
    
    // - - - create masked image from src
    cv::UMat mask;
    mMaskOperated = src.clone();
    cv::multiply(regionMask, 1.0/255.0, mask);
    cv::multiply(mMaskOperated, mask, mMaskOperated);
    
    // - - - expand dynamic range
    // TODO parameters
    cv::subtract(mMaskOperated, 200, mMaskOperatedAndDym);
    cv::multiply(mMaskOperatedAndDym, 255.0 / (255.0 - 200) , mMaskOperatedAndDym);
    
    cv::subtract(src, mMaskOperatedAndDym, dst);
   
}
cv::UMat UBMS::getAttentionMap(const cv::UMat& bm, int dilation_width_1, bool toNormalize, bool handle_border) {
	using namespace std::chrono;

	cv::UMat ret = bm.clone();

	int jump;
	if (handle_border) {

		cv::Mat cpuRet = ret.getMat(cv::ACCESS_FAST);

		for (int i = 0; i < bm.rows; i++) {

			jump = BMS_RNG.uniform(0.0, 1.0)>0.99 ? BMS_RNG.uniform(5, 25) : 0;
			if (cpuRet.at<uchar>(i, 0 + jump) != 1) {
				cv::floodFill(ret, cv::Point(0 + jump, i), cv::Scalar(1), 0, cv::Scalar(0), cv::Scalar(0), 8);
				//cpuRet = ret.getMat(cv::ACCESS_READ);
			}

			jump = BMS_RNG.uniform(0.0, 1.0)>0.99 ? BMS_RNG.uniform(5, 25) : 0;
			if (cpuRet.at<uchar>(i, bm.cols - 1 - jump) != 1) {
				cv::floodFill(ret, cv::Point(bm.cols - 1 - jump, i), cv::Scalar(1), 0, cv::Scalar(0), cv::Scalar(0), 8);
				//cpuRet = ret.getMat(cv::ACCESS_READ);
			}

		}

		for (int j = 0; j < bm.cols; j++) {

			jump = BMS_RNG.uniform(0.0, 1.0)>0.99 ? BMS_RNG.uniform(5, 25) : 0;
			if (cpuRet.at<uchar>(0 + jump, j) != 1) {
				cv::floodFill(ret, cv::Point(j, 0 + jump), cv::Scalar(1), 0, cv::Scalar(0), cv::Scalar(0), 8);
				//cpuRet = ret.getMat(cv::ACCESS_READ);
			}

			jump = BMS_RNG.uniform(0.0, 1.0)>0.99 ? BMS_RNG.uniform(5, 25) : 0;
			if (cpuRet.at<uchar>(bm.rows - 1 - jump, j) != 1) {
				cv::floodFill(ret, cv::Point(j, bm.rows - 1 - jump), cv::Scalar(1), 0, cv::Scalar(0), cv::Scalar(0), 8);
				//cpuRet = ret.getMat(cv::ACCESS_READ);
			}
		}
	}
	else {

		cv::Mat cpuRet = ret.getMat(cv::ACCESS_FAST);
		for (int i = 0; i < bm.rows; i++) {

			if (cpuRet.at<uchar>(i, 0) != 1) {
				cv::floodFill(ret, cv::Point(0, i), cv::Scalar(1), 0, cv::Scalar(0), cv::Scalar(0), 8);
				//cpuRet = ret.getMat(cv::ACCESS_READ);
			}

			if (cpuRet.at<uchar>(i, bm.cols - 1) != 1) {
				cv::floodFill(ret, cv::Point(bm.cols - 1, i), cv::Scalar(1), 0, cv::Scalar(0), cv::Scalar(0), 8);
				//cpuRet = ret.getMat(cv::ACCESS_READ);
			}

		}

		for (int j = 0; j < bm.cols; j++) {

			if (cpuRet.at<uchar>(0, j) != 1) {
				cv::floodFill(ret, cv::Point(j, 0), cv::Scalar(1), 0, cv::Scalar(0), cv::Scalar(0), 8);
				//cpuRet = ret.getMat(cv::ACCESS_READ);
			}


			if (cpuRet.at<uchar>(bm.rows - 1, j) != 1) {
				cv::floodFill(ret, cv::Point(j, bm.rows - 1), cv::Scalar(1), 0, cv::Scalar(0), cv::Scalar(0), 8);
				//cpuRet = ret.getMat(cv::ACCESS_READ);
			}

		}
	}

	

	cv::bitwise_xor(ret, cv::Scalar(1), ret);
	cv::UMat map1, map2;

	cv::bitwise_and(ret, bm, map1);
	cv::UMat notBM;

	cv::subtract(cv::Scalar(1), bm, notBM);
	cv::bitwise_and(ret, notBM, map2);

	if (dilation_width_1 > 0) {
		for (int k = 0; k < (dilation_width_1 - 1) / 2; ++k) {
			dilate(map1, map1, cv::UMat(), cv::Point(-1, -1), 3);
			dilate(map2, map2, cv::UMat(), cv::Point(-1, -1), 3);
		}
	}

	map1.convertTo(map1, CV_32FC1);
	map2.convertTo(map2, CV_32FC1);

	bmsNormalize(map1);
	bmsNormalize(map2);

	cv::add(map1, map2, map1);

	return map1;

}