Java Code Examples for org.opencv.core.Core#add()

The following examples show how to use org.opencv.core.Core#add() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FusionEnhance.java    From OptimizedImageEnhance with MIT License 7 votes vote down vote up
public static Mat enhance (Mat image, int level) {
	// color balance
	Mat img1 = Filters.SimplestColorBalance(image, 5);
	img1.convertTo(img1, CvType.CV_8UC1);
	// Perform sRGB to CIE Lab color space conversion
	Mat LabIm1 = new Mat();
	Imgproc.cvtColor(img1, LabIm1, Imgproc.COLOR_BGR2Lab);
	Mat L1 = new Mat();
	Core.extractChannel(LabIm1, L1, 0);
	// apply CLAHE
	Mat[] result = applyCLAHE(LabIm1, L1);
	Mat img2 = result[0];
	Mat L2 = result[1];
	// calculate normalized weight
	Mat w1 = calWeight(img1, L1);
	Mat w2 = calWeight(img2, L2);
	Mat sumW = new Mat();
	Core.add(w1, w2, sumW);
	Core.divide(w1, sumW, w1);
	Core.divide(w2, sumW, w2);
	// merge image1 and image2
	return ImgDecompose.fuseTwoImage(w1, img1, w2, img2, level);
}
 
Example 2
Source File: EnhanceFunc.java    From ImageEnhanceViaFusion with MIT License 6 votes vote down vote up
private static Mat calWeight(Mat img, Mat L) {
	Core.divide(L, new Scalar(255.0), L);
	L.convertTo(L, CvType.CV_32F);
	// calculate laplacian contrast weight
	Mat WL = WeightCalculate.LaplacianContrast(L);
	WL.convertTo(WL, L.type());
	// calculate Local contrast weight
	Mat WC = WeightCalculate.LocalContrast(L);
	WC.convertTo(WC, L.type());
	// calculate the saliency weight
	Mat WS = WeightCalculate.Saliency(img);
	WS.convertTo(WS, L.type());
	// calculate the exposedness weight
	Mat WE = WeightCalculate.Exposedness(L);
	WE.convertTo(WE, L.type());
	// sum
	Mat weight = WL.clone();
	Core.add(weight, WC, weight);
	Core.add(weight, WS, weight);
	Core.add(weight, WE, weight);
	return weight;
}
 
Example 3
Source File: DetectionHelper.java    From ml-authentication with Apache License 2.0 6 votes vote down vote up
private static double getImageBrightness(Mat img){
    Mat temp = new Mat();
    List<Mat> color = new ArrayList<Mat>(3);
    Mat lum = new Mat();
    temp = img;

    Core.split(temp, color);

    if(color.size() > 0){
        Core.multiply(color.get(0), new Scalar(0.299), color.get(0));
        Core.multiply(color.get(1), new Scalar(0.587), color.get(1));
        Core.multiply(color.get(2), new Scalar(0.114), color.get(2));

        Core.add(color.get(0),color.get(1),lum);
        Core.add(lum, color.get(2), lum);

        Scalar sum = Core.sumElems(lum);

        return sum.val[0]/((1<<8 - 1)*img.rows() * img.cols()) * 2;
    } else {
        return 1;
    }
}
 
Example 4
Source File: ALTMRetinex.java    From OptimizedImageEnhance with MIT License 6 votes vote down vote up
private static List<Mat> globalAdaptation(Mat b, Mat g, Mat r, int rows, int cols) {
	// Calculate Lw & maximum of Lw
	Mat Lw = new Mat(rows, cols, r.type());
	Core.multiply(r, new Scalar(rParam), r);
	Core.multiply(g, new Scalar(gParam), g);
	Core.multiply(b, new Scalar(bParam), b);
	Core.add(r, g, Lw);
	Core.add(Lw, b, Lw);
	double LwMax = Core.minMaxLoc(Lw).maxVal; // the maximum luminance value
	// Calculate log-average luminance and get global adaptation result
	Mat Lw_ = Lw.clone();
	Core.add(Lw_, new Scalar(0.001), Lw_);
	Core.log(Lw_, Lw_);
	double LwAver = Math.exp(Core.sumElems(Lw_).val[0] / (rows * cols));
	Mat Lg = Lw.clone();
	Core.divide(Lg, new Scalar(LwAver), Lg);
	Core.add(Lg, new Scalar(1.0), Lg);
	Core.log(Lg, Lg);
	Core.divide(Lg, new Scalar(Math.log(LwMax / LwAver + 1.0)), Lg); // Lg is the global adaptation
	List<Mat> list = new ArrayList<>();
	list.add(Lw);
	list.add(Lg);
	return list;
}
 
Example 5
Source File: TransmissionEstimate.java    From OptimizedImageEnhance with MIT License 6 votes vote down vote up
public static Mat transEstimate(Mat img, int patchSz, double[] airlight, double lambda, double fTrans, 
		int r, double eps, double gamma) {
	int rows = img.rows();
	int cols = img.cols();
	List<Mat> bgr = new ArrayList<>();
	Core.split(img, bgr);
	int type = bgr.get(0).type();
	// calculate the transmission map
	Mat T = computeTrans(img, patchSz, rows, cols, type, airlight, lambda, fTrans);
	// refine the transmission map
	img.convertTo(img, CvType.CV_8UC1);
	Mat gray = new Mat();
	Imgproc.cvtColor(img, gray, Imgproc.COLOR_BGR2GRAY);
	gray.convertTo(gray, CvType.CV_32F);
	Core.divide(gray, new Scalar(255.0), gray);
	T = Filters.GuidedImageFilter(gray, T, r, eps);
	Mat Tsmooth = new Mat();
	Imgproc.GaussianBlur(T, Tsmooth, new Size(81, 81), 40);
	Mat Tdetails = new Mat();
	Core.subtract(T, Tsmooth, Tdetails);
	Core.multiply(Tdetails, new Scalar(gamma), Tdetails);
	Core.add(Tsmooth, Tdetails, T);
	return T;
}
 
Example 6
Source File: MotionDetector.java    From opencv-fun with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * @return true if motion was detected compared to the last frame
 */
public boolean detect(Mat frame) {
	if(lastImage == null) {
		lastImage = frame.clone();
		return true;
	}
	
	Mat diff = new Mat();
	Core.absdiff(lastImage, frame, diff);
	Imgproc.threshold(diff, diff, 35, 255, Imgproc.THRESH_BINARY);
	
	// extract color channels and merge them to single bitmask
	Mat r = ColorSpace.getChannel(diff, 2);
	Mat g = ColorSpace.getChannel(diff, 1);
	Mat b = ColorSpace.getChannel(diff, 0);

	mask = r.clone();
	Core.add(mask, g, mask);
	Core.add(mask, b, mask);
	
	float changes = Core.countNonZero(mask) / (float)( frame.cols() * frame.rows());
	r.release();
	g.release();
	b.release();
	lastImage.release();
	lastImage = frame.clone();
	return thresholdPercentage < changes;
}
 
Example 7
Source File: Recognition.java    From classchecks with Apache License 2.0 5 votes vote down vote up
public static Mat subspaceReconstruct(Mat W, Mat mean, Mat src) {
	int n = src.rows();
	int d = src.cols();
	Mat X = new Mat();
	Mat Y = new Mat();
	src.convertTo(Y, W.type());
	Core.gemm(Y, W, 1.0, new Mat(), 0.0, X, 2);
	if(!mean.empty()) {
		for(int i = 0; i < n; i ++) {
			Mat r_i = X.row(i);
			Core.add(r_i, mean.reshape(1, 1), r_i);
		}
	}
	return X;
}
 
Example 8
Source File: BackgroundSubtractor.java    From opencv-fun with GNU Affero General Public License v3.0 5 votes vote down vote up
public Mat createMask(Mat camera) {				
	// copy as we are going to destruct those images maybe
	Mat camBlur= camera.clone();
	Mat backgroundBlur = calib.getBackgroundImage().clone();

	// remove noise
	Imgproc.blur(backgroundBlur, backgroundBlur, new Size(calib.getBlurSize(), calib.getBlurSize()));
	Imgproc.blur(camBlur, camBlur, new Size(calib.getBlurSize(), calib.getBlurSize()));

	// take abs diff and create binary image in all 3 channels
	Mat diff = new Mat();
	Core.absdiff(backgroundBlur, camBlur, diff);
	Imgproc.threshold(diff, diff, calib.getSubtractionThreshold(), 255, Imgproc.THRESH_BINARY);

	// extract color channels and merge them to single bitmask
	Mat r = ColorSpace.getChannel(diff, 2);
	Mat g = ColorSpace.getChannel(diff, 1);
	Mat b = ColorSpace.getChannel(diff, 0);

	Mat mask = r.clone();
	Core.add(mask, g, mask);
	Core.add(mask, b, mask);
	
	// dilate to remove some black gaps within balls
	Imgproc.dilate(mask, mask, Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(calib.getMorphSize(), calib.getMorphSize())));

	return mask;
}
 
Example 9
Source File: Pyramid.java    From ImageEnhanceViaFusion with MIT License 5 votes vote down vote up
public static Mat PyramidReconstruct(Mat[] pyramid) {
	int level = pyramid.length;
	for (int i = level - 1; i > 0; i--) {
		Mat tmpPyr = new Mat();
		Imgproc.resize(pyramid[i], tmpPyr, pyramid[i - 1].size(), 0, 0, Imgproc.INTER_LINEAR);
		Core.add(pyramid[i - 1], tmpPyr, pyramid[i - 1]);
	}
	return pyramid[0];
}
 
Example 10
Source File: WeightCalculate.java    From ImageEnhanceViaFusion with MIT License 5 votes vote down vote up
public static Mat Saliency(Mat img) {
	// blur image with a 3x3 or 5x5 Gaussian filter
	Mat gfbgr = new Mat();
	Imgproc.GaussianBlur(img, gfbgr, new Size(3, 3), 3);
	// Perform sRGB to CIE Lab color space conversion
	Mat LabIm = new Mat();
	Imgproc.cvtColor(gfbgr, LabIm, Imgproc.COLOR_BGR2Lab);
	// Compute Lab average values (note that in the paper this average is found from the
	// un-blurred original image, but the results are quite similar)
	List<Mat> lab = new ArrayList<Mat>();
	Core.split(LabIm, lab);
	Mat l = lab.get(0);
	l.convertTo(l, CvType.CV_32F);
	Mat a = lab.get(1);
	a.convertTo(a, CvType.CV_32F);
	Mat b = lab.get(2);
	b.convertTo(b, CvType.CV_32F);
	double lm = Core.mean(l).val[0];
	double am = Core.mean(a).val[0];
	double bm = Core.mean(b).val[0];
	// Finally compute the saliency map
	Mat sm = Mat.zeros(l.rows(), l.cols(), l.type());
	Core.subtract(l, new Scalar(lm), l);
	Core.subtract(a, new Scalar(am), a);
	Core.subtract(b, new Scalar(bm), b);
	Core.add(sm, l.mul(l), sm);
	Core.add(sm, a.mul(a), sm);
	Core.add(sm, b.mul(b), sm);
	return sm;
}
 
Example 11
Source File: RemoveBackScatter.java    From OptimizedImageEnhance with MIT License 5 votes vote down vote up
private static Mat dehazeProcess(Mat img, Mat trans, double[] airlight) {
	Mat balancedImg = Filters.SimplestColorBalance(img, 5);
	Mat bCnl = new Mat();
	Core.extractChannel(balancedImg, bCnl, 0);
	Mat gCnl = new Mat();
	Core.extractChannel(balancedImg, gCnl, 1);
	Mat rCnl = new Mat();
	Core.extractChannel(balancedImg, rCnl, 2);
	// get mean value
	double bMean = Core.mean(bCnl).val[0];
	double gMean = Core.mean(gCnl).val[0];
	double rMean = Core.mean(rCnl).val[0];
	// get transmission map for each channel
	Mat Tb = trans.clone();
	Core.multiply(Tb, new Scalar(Math.max(bMean, Math.max(gMean, rMean)) / bMean * 0.8), Tb);
	Mat Tg = trans.clone();
	Core.multiply(Tg, new Scalar(Math.max(bMean, Math.max(gMean, rMean)) / gMean * 0.9), Tg);
	Mat Tr = trans.clone();
	Core.multiply(Tr, new Scalar(Math.max(bMean, Math.max(gMean, rMean)) / rMean * 0.8), Tr);
	// dehaze by formula
	// blue channel
	Mat bChannel = new Mat();
	Core.subtract(bCnl, new Scalar(airlight[0]), bChannel);
	Core.divide(bChannel, Tb, bChannel);
	Core.add(bChannel, new Scalar(airlight[0]), bChannel);
	// green channel
	Mat gChannel = new Mat();
	Core.subtract(gCnl, new Scalar(airlight[1]), gChannel);
	Core.divide(gChannel, Tg, gChannel);
	Core.add(gChannel, new Scalar(airlight[1]), gChannel);
	// red channel
	Mat rChannel = new Mat();
	Core.subtract(rCnl, new Scalar(airlight[2]), rChannel);
	Core.divide(rChannel, Tr, rChannel);
	Core.add(rChannel, new Scalar(airlight[2]), rChannel);
	Mat dehazed = new Mat();
	Core.merge(new ArrayList<>(Arrays.asList(bChannel, gChannel, rChannel)), dehazed);
	return dehazed;
}
 
Example 12
Source File: RemoveBackScatter.java    From OptimizedImageEnhance with MIT License 5 votes vote down vote up
private static Mat pyramidFuse(Mat w1, Mat w2, Mat img1, Mat img2, int level) {
	// Normalized weight
	Mat sumW = new Mat();
	Core.add(w1, w2, sumW);
	Core.divide(w1, sumW, w1);
	Core.multiply(w1, new Scalar(2.0), w1);
	Core.divide(w2, sumW, w2);
	Core.multiply(w2, new Scalar(2.0), w2);
	// Pyramid decomposition and reconstruct
	return ImgDecompose.fuseTwoImage(w1, img1, w2, img2, level);
}
 
Example 13
Source File: OptimizedContrastEnhance.java    From OptimizedImageEnhance with MIT License 5 votes vote down vote up
private static Mat dehaze(Mat img, Mat T, double airlight) {
	// J = (img - airlight) ./ T + airlight;
	Core.subtract(img, new Scalar(airlight), img);
	Core.divide(img, T, img);
	Core.add(img, new Scalar(airlight), img);
	return img;
}
 
Example 14
Source File: FeatureWeight.java    From OptimizedImageEnhance with MIT License 5 votes vote down vote up
public static Mat Saliency(Mat img) {
	// blur image with a 3x3 or 5x5 Gaussian filter
	Mat gfbgr = new Mat();
	Imgproc.GaussianBlur(img, gfbgr, new Size(3, 3), 3);
	// Perform sRGB to CIE Lab color space conversion
	Mat LabIm = new Mat();
	Imgproc.cvtColor(gfbgr, LabIm, Imgproc.COLOR_BGR2Lab);
	// Compute Lab average values (note that in the paper this average is found from the
	// un-blurred original image, but the results are quite similar)
	List<Mat> lab = new ArrayList<>();
	Core.split(LabIm, lab);
	Mat l = lab.get(0);
	l.convertTo(l, CvType.CV_32F);
	Mat a = lab.get(1);
	a.convertTo(a, CvType.CV_32F);
	Mat b = lab.get(2);
	b.convertTo(b, CvType.CV_32F);
	double lm = Core.mean(l).val[0];
	double am = Core.mean(a).val[0];
	double bm = Core.mean(b).val[0];
	// Finally compute the saliency map
	Mat sm = Mat.zeros(l.rows(), l.cols(), l.type());
	Core.subtract(l, new Scalar(lm), l);
	Core.subtract(a, new Scalar(am), a);
	Core.subtract(b, new Scalar(bm), b);
	Core.add(sm, l.mul(l), sm);
	Core.add(sm, a.mul(a), sm);
	Core.add(sm, b.mul(b), sm);
	return sm;
}
 
Example 15
Source File: BlkTransEstimate.java    From OptimizedImageEnhance with MIT License 5 votes vote down vote up
private static Mat preDehaze(Mat img, double a, double nTrans) {
	// nOut = ( (blkIm - a) * nTrans + 128 * a ) / 128;
	Core.subtract(img, new Scalar(a), img);
	Core.multiply(img, new Scalar(nTrans), img);
	Core.add(img, new Scalar(128.0 * a), img);
	Core.divide(img, new Scalar(128.0), img);
	return img;
}
 
Example 16
Source File: ALTMRetinex.java    From OptimizedImageEnhance with MIT License 4 votes vote down vote up
public static Mat enhance(Mat image, int r, double eps, double eta, double lambda, double krnlRatio) {
	image.convertTo(image, CvType.CV_32F);
	// extract each color channel
	List<Mat> bgr = new ArrayList<>();
	Core.split(image, bgr);
	Mat bChannel = bgr.get(0);
	Mat gChannel = bgr.get(1);
	Mat rChannel = bgr.get(2);
	int m = rChannel.rows();
	int n = rChannel.cols();
	// Global Adaptation
	List<Mat> list = globalAdaptation(bChannel, gChannel, rChannel, m, n);
	Mat Lw = list.get(0);
	Mat Lg = list.get(1);
	// Local Adaptation
	Mat Hg = localAdaptation(Lg, m, n, r, eps, krnlRatio);
	Lg.convertTo(Lg, CvType.CV_32F);
	// process
	Mat alpha = new Mat(m, n, rChannel.type());
	Core.divide(Lg, new Scalar(Core.minMaxLoc(Lg).maxVal / eta), alpha);
	//Core.multiply(alpha, new Scalar(eta), alpha);
	Core.add(alpha, new Scalar(1.0), alpha);
	//alpha = adjustment(alpha, 1.25);
	Mat Lg_ = new Mat(m, n, rChannel.type());
	Core.add(Lg, new Scalar(1.0 / 255.0), Lg_);
	Core.log(Lg_, Lg_);
	double beta = Math.exp(Core.sumElems(Lg_).val[0] / (m * n)) * lambda;
	Mat Lout = new Mat(m, n, rChannel.type());
	Core.divide(Lg, Hg, Lout);
	Core.add(Lout, new Scalar(beta), Lout);
	Core.log(Lout, Lout);
	Core.normalize(alpha.mul(Lout), Lout, 0, 255, Core.NORM_MINMAX);
	Mat gain = obtainGain(Lout, Lw, m, n);
	// output
	Core.divide(rChannel.mul(gain), new Scalar(Core.minMaxLoc(rChannel).maxVal / 255.0), rChannel); // Red Channel
	Core.divide(gChannel.mul(gain), new Scalar(Core.minMaxLoc(gChannel).maxVal / 255.0), gChannel); // Green Channel
	Core.divide(bChannel.mul(gain), new Scalar(Core.minMaxLoc(bChannel).maxVal / 255.0), bChannel); // Blue Channel
	// merge three color channels to a image
	Mat outval = new Mat();
	Core.merge(new ArrayList<>(Arrays.asList(bChannel, gChannel, rChannel)), outval);
	outval.convertTo(outval, CvType.CV_8UC1);
	return outval;
}
 
Example 17
Source File: EnhanceFunc.java    From ImageEnhanceViaFusion with MIT License 4 votes vote down vote up
public static void main(String[] args) {
	String imgPath = "images/5.jpg";
	Mat image = Imgcodecs.imread(imgPath, Imgcodecs.CV_LOAD_IMAGE_COLOR);
	new ImShow("original").showImage(image);
	// color balance
	Mat img1 = ColorBalance.SimplestColorBalance(image, 5);
	img1.convertTo(img1, CvType.CV_8UC1);
	// Perform sRGB to CIE Lab color space conversion
	Mat LabIm1 = new Mat();
	Imgproc.cvtColor(img1, LabIm1, Imgproc.COLOR_BGR2Lab);
	Mat L1 = new Mat();
	Core.extractChannel(LabIm1, L1, 0);
	// apply CLAHE
	Mat[] result = applyCLAHE(LabIm1, L1);
	Mat img2 = result[0];
	Mat L2 = result[1];
	// calculate normalized weight
	Mat w1 = calWeight(img1, L1);
	Mat w2 = calWeight(img2, L2);
	Mat sumW = new Mat();
	Core.add(w1, w2, sumW);
	Core.divide(w1, sumW, w1);
	Core.divide(w2, sumW, w2);
	// construct the gaussian pyramid for weight
	int level = 5;
	Mat[] weight1 = Pyramid.GaussianPyramid(w1, level);
	Mat[] weight2 = Pyramid.GaussianPyramid(w2, level);
	// construct the laplacian pyramid for input image channel
	img1.convertTo(img1, CvType.CV_32F);
	img2.convertTo(img2, CvType.CV_32F);
	List<Mat> bgr = new ArrayList<Mat>();
	Core.split(img1, bgr);
	Mat[] bCnl1 = Pyramid.LaplacianPyramid(bgr.get(0), level);
	Mat[] gCnl1 = Pyramid.LaplacianPyramid(bgr.get(1), level);
	Mat[] rCnl1 = Pyramid.LaplacianPyramid(bgr.get(2), level);
	bgr.clear();
	Core.split(img2, bgr);
	Mat[] bCnl2 = Pyramid.LaplacianPyramid(bgr.get(0), level);
	Mat[] gCnl2 = Pyramid.LaplacianPyramid(bgr.get(1), level);
	Mat[] rCnl2 = Pyramid.LaplacianPyramid(bgr.get(2), level);
	// fusion process
	Mat[] bCnl = new Mat[level];
	Mat[] gCnl = new Mat[level];
	Mat[] rCnl = new Mat[level];
	for (int i = 0; i < level; i++) {
		Mat cn = new Mat();
		Core.add(bCnl1[i].mul(weight1[i]), bCnl2[i].mul(weight2[i]), cn);
		bCnl[i] = cn.clone();
		Core.add(gCnl1[i].mul(weight1[i]), gCnl2[i].mul(weight2[i]), cn);
		gCnl[i] = cn.clone();
		Core.add(rCnl1[i].mul(weight1[i]), rCnl2[i].mul(weight2[i]), cn);
		rCnl[i] = cn.clone();
	}
	// reconstruct & output
	Mat bChannel = Pyramid.PyramidReconstruct(bCnl);
	Mat gChannel = Pyramid.PyramidReconstruct(gCnl);
	Mat rChannel = Pyramid.PyramidReconstruct(rCnl);
	Mat fusion = new Mat();
	Core.merge(new ArrayList<Mat>(Arrays.asList(bChannel, gChannel, rChannel)), fusion);
	fusion.convertTo(fusion, CvType.CV_8UC1);
	new ImShow("fusion").showImage(fusion);
}
 
Example 18
Source File: Filters.java    From OptimizedImageEnhance with MIT License 4 votes vote down vote up
private static Mat filterSingleChannel(Mat p, double s, ArrayList<Mat> Isubchannels, ArrayList<Mat> Ichannels, 
		Mat mean_I_r, Mat mean_I_g, Mat mean_I_b, Mat invrr, Mat invrg, Mat invrb, Mat invgg, Mat invgb, 
		Mat invbb, double r_sub) {
	Mat p_sub = new Mat();
	Imgproc.resize(p, p_sub, new Size(p.cols() / s, p.rows() / s), 0.0, 0.0, Imgproc.INTER_NEAREST);

	Mat mean_p = boxfilter(p_sub, (int) r_sub);

	Mat mean_Ip_r = boxfilter(Isubchannels.get(0).mul(p_sub), (int) r_sub);
	Mat mean_Ip_g = boxfilter(Isubchannels.get(1).mul(p_sub), (int) r_sub);
	Mat mean_Ip_b = boxfilter(Isubchannels.get(2).mul(p_sub), (int) r_sub);

	// convariance of (I, p) in each local patch
	Mat cov_Ip_r = new Mat();
	Mat cov_Ip_g = new Mat();
	Mat cov_Ip_b = new Mat();
	Core.subtract(mean_Ip_r, mean_I_r.mul(mean_p), cov_Ip_r);
	Core.subtract(mean_Ip_g, mean_I_g.mul(mean_p), cov_Ip_g);
	Core.subtract(mean_Ip_b, mean_I_b.mul(mean_p), cov_Ip_b);

	Mat temp1 = new Mat();
	Mat a_r = new Mat();
	Mat a_g = new Mat();
	Mat a_b = new Mat();
	Core.add(invrr.mul(cov_Ip_r), invrg.mul(cov_Ip_g), temp1);
	Core.add(temp1, invrb.mul(cov_Ip_b), a_r);
	Core.add(invrg.mul(cov_Ip_r), invgg.mul(cov_Ip_g), temp1);
	Core.add(temp1, invgb.mul(cov_Ip_b), a_g);
	Core.add(invrb.mul(cov_Ip_r), invgb.mul(cov_Ip_g), temp1);
	Core.add(temp1, invbb.mul(cov_Ip_b), a_b);

	Mat b = new Mat();
	Core.subtract(mean_p, a_r.mul(mean_I_r), b);
	Core.subtract(b, a_g.mul(mean_I_g), b);
	Core.subtract(b, a_b.mul(mean_I_b), b);

	Mat mean_a_r = boxfilter(a_r, (int) r_sub);
	Mat mean_a_g = boxfilter(a_g, (int) r_sub);
	Mat mean_a_b = boxfilter(a_b, (int) r_sub);
	Mat mean_b = boxfilter(b, (int) r_sub);

	Imgproc.resize(mean_a_r, mean_a_r, 
			new Size(Ichannels.get(0).cols(), Ichannels.get(0).rows()), 0.0, 0.0, Imgproc.INTER_LINEAR);
	Imgproc.resize(mean_a_g, mean_a_g, 
			new Size(Ichannels.get(0).cols(), Ichannels.get(0).rows()), 0.0, 0.0, Imgproc.INTER_LINEAR);
	Imgproc.resize(mean_a_b, mean_a_b, 
			new Size(Ichannels.get(0).cols(), Ichannels.get(0).rows()), 0.0, 0.0, Imgproc.INTER_LINEAR);
	Imgproc.resize(mean_b, mean_b, 
			new Size(Ichannels.get(0).cols(), Ichannels.get(0).rows()), 0.0, 0.0, Imgproc.INTER_LINEAR);

	Mat result = new Mat();
	Core.add(mean_a_r.mul(Ichannels.get(0)), mean_a_g.mul(Ichannels.get(1)), temp1);
	Core.add(temp1, mean_a_b.mul(Ichannels.get(2)), temp1);
	Core.add(temp1, mean_b, result);
	return result;
}
 
Example 19
Source File: Filters.java    From OptimizedImageEnhance with MIT License 4 votes vote down vote up
/**
 * Guided Image Filter for grayscale image, O(1) time implementation of guided filter
 *
 * @param I guidance image (should be a gray-scale/single channel image)
 * @param p filtering input image (should be a gray-scale/single channel image)
 * @param r local window radius
 * @param eps regularization parameter
 * @return filtered image
 */
public static Mat GuidedImageFilter(Mat I, Mat p, int r, double eps) {
	I.convertTo(I, CvType.CV_64FC1);
	p.convertTo(p, CvType.CV_64FC1);
	//[hei, wid] = size(I);
	int rows = I.rows();
	int cols = I.cols();
	// N = boxfilter(ones(hei, wid), r); % the size of each local patch; N=(2r+1)^2 except for boundary pixels.
	Mat N = new Mat();
	Imgproc.boxFilter(Mat.ones(rows, cols, I.type()), N, -1, new Size(r, r));
	// mean_I = boxfilter(I, r) ./ N;
	Mat mean_I = new Mat();
	Imgproc.boxFilter(I, mean_I, -1, new Size(r, r));
	// mean_p = boxfilter(p, r) ./ N
	Mat mean_p = new Mat();
	Imgproc.boxFilter(p, mean_p, -1, new Size(r, r));
	// mean_Ip = boxfilter(I.*p, r) ./ N;
	Mat mean_Ip = new Mat();
	Imgproc.boxFilter(I.mul(p), mean_Ip, -1, new Size(r, r));
	// cov_Ip = mean_Ip - mean_I .* mean_p; % this is the covariance of (I, p) in each local patch.
	Mat cov_Ip = new Mat();
	Core.subtract(mean_Ip, mean_I.mul(mean_p), cov_Ip);
	// mean_II = boxfilter(I.*I, r) ./ N;
	Mat mean_II = new Mat();
	Imgproc.boxFilter(I.mul(I), mean_II, -1, new Size(r, r));
	// var_I = mean_II - mean_I .* mean_I;
	Mat var_I = new Mat();
	Core.subtract(mean_II, mean_I.mul(mean_I), var_I);
	// a = cov_Ip ./ (var_I + eps); % Eqn. (5) in the paper;
	Mat a = new Mat();
	Core.add(var_I, new Scalar(eps), a);
	Core.divide(cov_Ip, a, a);
	//b = mean_p - a .* mean_I; % Eqn. (6) in the paper;
	Mat b = new Mat();
	Core.subtract(mean_p, a.mul(mean_I), b);
	// mean_a = boxfilter(a, r) ./ N;
	Mat mean_a = new Mat();
	Imgproc.boxFilter(a, mean_a, -1, new Size(r, r));
	Core.divide(mean_a, N, mean_a);
	// mean_b = boxfilter(b, r) ./ N;
	Mat mean_b = new Mat();
	Imgproc.boxFilter(b, mean_b, -1, new Size(r, r));
	Core.divide(mean_b, N, mean_b);
	// q = mean_a .* I + mean_b; % Eqn. (8) in the paper;
	Mat q = new Mat();
	Core.add(mean_a.mul(I), mean_b, q);
	q.convertTo(q, CvType.CV_32F);
	return q;
}
 
Example 20
Source File: Filters.java    From ImageEnhanceViaFusion with MIT License 4 votes vote down vote up
/**
 * Guided Image Filter for grayscale image, O(1) time implementation of guided filter
 *
 * @param I
 *            guidance image (should be a gray-scale/single channel image)
 * @param p
 *            filtering input image (should be a gray-scale/single channel
 *            image)
 * @param r
 *            local window radius
 * @param eps
 *            regularization parameter
 * @return filtered image
 */
public static Mat GuidedImageFilter(Mat I, Mat p, int r, double eps) {
	I.convertTo(I, CvType.CV_64FC1);
	p.convertTo(p, CvType.CV_64FC1);
	//[hei, wid] = size(I);
	int rows = I.rows();
	int cols = I.cols();
	// N = boxfilter(ones(hei, wid), r); % the size of each local patch; N=(2r+1)^2 except for boundary pixels.
	Mat N = new Mat();
	Imgproc.boxFilter(Mat.ones(rows, cols, I.type()), N, -1, new Size(r, r));
	// mean_I = boxfilter(I, r) ./ N;
	Mat mean_I = new Mat();
	Imgproc.boxFilter(I, mean_I, -1, new Size(r, r));
	// mean_p = boxfilter(p, r) ./ N
	Mat mean_p = new Mat();
	Imgproc.boxFilter(p, mean_p, -1, new Size(r, r));
	// mean_Ip = boxfilter(I.*p, r) ./ N;
	Mat mean_Ip = new Mat();
	Imgproc.boxFilter(I.mul(p), mean_Ip, -1, new Size(r, r));
	// cov_Ip = mean_Ip - mean_I .* mean_p; % this is the covariance of (I, p) in each local patch.
	Mat cov_Ip = new Mat();
	Core.subtract(mean_Ip, mean_I.mul(mean_p), cov_Ip);
	// mean_II = boxfilter(I.*I, r) ./ N;
	Mat mean_II = new Mat();
	Imgproc.boxFilter(I.mul(I), mean_II, -1, new Size(r, r));
	// var_I = mean_II - mean_I .* mean_I;
	Mat var_I = new Mat();
	Core.subtract(mean_II, mean_I.mul(mean_I), var_I);
	// a = cov_Ip ./ (var_I + eps); % Eqn. (5) in the paper;
	Mat a = new Mat();
	Core.add(var_I, new Scalar(eps), a);
	Core.divide(cov_Ip, a, a);
	//b = mean_p - a .* mean_I; % Eqn. (6) in the paper;
	Mat b = new Mat();
	Core.subtract(mean_p, a.mul(mean_I), b);
	// mean_a = boxfilter(a, r) ./ N;
	Mat mean_a = new Mat();
	Imgproc.boxFilter(a, mean_a, -1, new Size(r, r));
	Core.divide(mean_a, N, mean_a);
	// mean_b = boxfilter(b, r) ./ N;
	Mat mean_b = new Mat();
	Imgproc.boxFilter(b, mean_b, -1, new Size(r, r));
	Core.divide(mean_b, N, mean_b);
	// q = mean_a .* I + mean_b; % Eqn. (8) in the paper;
	Mat q = new Mat();
	Core.add(mean_a.mul(I), mean_b, q);
	//for (int i = 0; i < rows; i++) {
	//	for (int j = 0; j < cols; j++) {
	//		if (q.get(i, j)[0] <= 0)
	//			q.put(i, j, 1.0 / 255);
	//	}
	//}
	q.convertTo(q, CvType.CV_32F);
	return q;
}