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

The following examples show how to use org.opencv.core.Core#subtract() . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
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 2
Mat buildSkeleton(Mat img){
    Mat morph = Imgproc.getStructuringElement(Imgproc.CV_SHAPE_CROSS, new Size(3, 3));
    Mat skel = new Mat(img.size(), CvType.CV_8UC1, Scalar.all(0));
    Mat eroded = new Mat();
    Mat temp = new Mat();

    boolean done = false;

    do{
        Imgproc.morphologyEx(img, eroded, Imgproc.MORPH_ERODE, morph);
        Imgproc.morphologyEx(eroded, temp, Imgproc.MORPH_DILATE, morph);
        Core.subtract(img, temp, temp);
        Core.bitwise_or(skel, temp, skel);
        eroded.copyTo(img);

        done = Core.countNonZero(img) == 0;
    }while (!done);

    return skel;
}
 
Example 3
Source Project: OptimizedImageEnhance   File: FeatureWeight.java    License: MIT License 6 votes vote down vote up
public static Mat LocalContrast(Mat img) {
	double[] h = { 1.0 / 16.0, 4.0 / 16.0, 6.0 / 16.0, 4.0 / 16.0, 1.0 / 16.0 };
	Mat mask = new Mat(h.length, h.length, img.type());
	for (int i = 0; i < h.length; i++) {
		for (int j = 0; j < h.length; j++) {
			mask.put(i, j, h[i] * h[j]);
		}
	}
	Mat localContrast = new Mat();
	Imgproc.filter2D(img, localContrast, img.depth(), mask);
	for (int i = 0; i < localContrast.rows(); i++) {
		for (int j = 0; j < localContrast.cols(); j++) {
			if (localContrast.get(i, j)[0] > Math.PI / 2.75) localContrast.put(i, j, Math.PI / 2.75);
		}
	}
	Core.subtract(img, localContrast, localContrast);
	return localContrast.mul(localContrast);
}
 
Example 4
Source Project: ImageEnhanceViaFusion   File: WeightCalculate.java    License: MIT License 6 votes vote down vote up
public static Mat LocalContrast(Mat img) {
	double[] h = { 1.0 / 16.0, 4.0 / 16.0, 6.0 / 16.0, 4.0 / 16.0, 1.0 / 16.0 };
	Mat mask = new Mat(h.length, h.length, img.type());
	for (int i = 0; i < h.length; i++) {
		for (int j = 0; j < h.length; j++) {
			mask.put(i, j, h[i] * h[j]);
		}
	}
	Mat localContrast = new Mat();
	Imgproc.filter2D(img, localContrast, img.depth(), mask);
	for (int i = 0; i < localContrast.rows(); i++) {
		for (int j = 0; j < localContrast.cols(); j++) {
			if (localContrast.get(i, j)[0] > Math.PI / 2.75)
				localContrast.put(i, j, Math.PI / 2.75);
		}
	}
	Core.subtract(img, localContrast, localContrast);
	return localContrast.mul(localContrast);
}
 
Example 5
Source Project: ImageEnhanceViaFusion   File: Pyramid.java    License: MIT License 6 votes vote down vote up
public static Mat[] LaplacianPyramid(Mat img, int level) {
	Mat[] lapPyr = new Mat[level];
	//Mat mask = filterMask(img);
	lapPyr[0] = img.clone();
	Mat tmpImg = img.clone();
	for (int i = 1; i < level; i++) {
		// resize image
		Imgproc.resize(tmpImg, tmpImg, new Size(), 0.5, 0.5, Imgproc.INTER_LINEAR);
		lapPyr[i] = tmpImg.clone();
	}
	// calculate the DoG
	for (int i = 0; i < level - 1; i++) {
		Mat tmpPyr = new Mat();
		Imgproc.resize(lapPyr[i + 1], tmpPyr, lapPyr[i].size(), 0, 0, Imgproc.INTER_LINEAR);
		Core.subtract(lapPyr[i], tmpPyr, lapPyr[i]);
	}
	return lapPyr;
}
 
Example 6
public String recognize(Mat img, String expectedLabel){
    // Ignore
    img = img.reshape(1,1);
    // Subtract mean
    img.convertTo(img, CvType.CV_32F);
    Core.subtract(img, Psi, img);
    // Project to subspace
    Mat projected = getFeatureVector(img);
    // Save all points of image for tSNE
    img.convertTo(img, CvType.CV_8U);
    addImage(projected, expectedLabel, true);
    //addImage(projected, expectedLabel);
    Mat distance = new Mat(Omega.rows(), 1, CvType.CV_64FC1);
    for (int i=0; i<Omega.rows(); i++){
        double dist = Core.norm(projected.row(0), Omega.row(i), Core.NORM_L2);
        distance.put(i, 0, dist);
    }
    Mat sortedDist = new Mat(Omega.rows(), 1, CvType.CV_8UC1);
    Core.sortIdx(distance, sortedDist, Core.SORT_EVERY_COLUMN + Core.SORT_ASCENDING);
    // Give back the name of the found person
    int index = (int)(sortedDist.get(0,0)[0]);
    return labelMap.getKey(labelList.get(index));
}
 
Example 7
Source Project: OptimizedImageEnhance   File: BlkTransEstimate.java    License: 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 8
@SuppressWarnings("unused")
public static Mat enhanceEachChannel(Mat image, int blkSize, int patchSize, double lambda, double eps, int krnlSize) {
	image.convertTo(image, CvType.CV_32F);
	// split image to three channels
	List<Mat> bgr = new ArrayList<>();
	Core.split(image, bgr);
	Mat bChannel = bgr.get(0);
	Mat gChannel = bgr.get(1);
	Mat rChannel = bgr.get(2);
	// obtain air-light
	double[] airlight = AirlightEstimate.estimate(image, blkSize);
	// obtain coarse transmission map and refine it for each channel
	double fTrans = 0.3;
	Mat T = TransmissionEstimate.transEstimateEachChannel(bChannel, patchSize, airlight[0], lambda, fTrans);
	Core.subtract(T, new Scalar(1.0), T);
	Core.multiply(T, new Scalar(-1.0), T);
	Mat Tb = Filters.GuidedImageFilter(bChannel, T, krnlSize, eps);
	T = TransmissionEstimate.transEstimateEachChannel(gChannel, patchSize, airlight[1], lambda, fTrans);
	Core.subtract(T, new Scalar(1.0), T);
	Core.multiply(T, new Scalar(-1.0), T);
	Mat Tg = Filters.GuidedImageFilter(gChannel, T, krnlSize, eps);
	T = TransmissionEstimate.transEstimateEachChannel(rChannel, patchSize, airlight[2], lambda, fTrans);
	Core.subtract(T, new Scalar(1.0), T);
	Core.multiply(T, new Scalar(-1.0), T);
	Mat Tr = Filters.GuidedImageFilter(rChannel, T, krnlSize, eps);
	// dehaze
	bChannel = dehaze(bChannel, Tb, airlight[0]);
	gChannel = dehaze(gChannel, Tg, airlight[1]);
	rChannel = dehaze(rChannel, Tr, airlight[2]);
	Mat outval = new Mat();
	Core.merge(new ArrayList<>(Arrays.asList(bChannel, gChannel, rChannel)), outval);
	return outval;
}
 
Example 9
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 10
private static Mat dehaze(Mat channel, Mat t, double minAtmosLight) {
	Mat t_ = new Mat();
	Core.subtract(t, new Scalar(1.0), t_);
	Core.multiply(t_, new Scalar(-1.0 * minAtmosLight), t_);
	Core.subtract(channel, t_, channel);
	Core.divide(channel, t, channel);
	return channel;
}
 
Example 11
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 Project: ImageEnhanceViaFusion   File: WeightCalculate.java    License: 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 13
public Eyes getEyes(Mat img){
    double halfWidth = img.cols() / 2;
    double height = img.rows();
    double[] values = new double[4];
    values[0] = 0;
    values[1] = 0;
    values[2] = halfWidth;
    values[3] = height;
    Rect rightHalf = new Rect(values);
    values[0] = halfWidth;
    Rect leftHalf = new Rect(values);
    MatOfRect rightEyes = new MatOfRect();
    MatOfRect leftEyes = new MatOfRect();

    Mat rightHalfImg = img.submat(rightHalf);
    rightEyeDetector.detectMultiScale(rightHalfImg, rightEyes);
    Mat leftHalfImg = img.submat(leftHalf);
    leftEyeDetector.detectMultiScale(leftHalfImg, leftEyes);

    if (rightEyes.empty() || leftEyes.empty() || rightEyes.toArray().length > 1 || leftEyes.toArray().length > 1){
        return null;
    }

    Rect rightEye = rightEyes.toArray()[0];
    Rect leftEye = leftEyes.toArray()[0];

    MatOfFloat rightPoint = new MatOfFloat(rightEye.x + rightEye.width / 2, rightEye.y + rightEye.height / 2);
    MatOfFloat leftPoint = new MatOfFloat(img.cols() / 2 + leftEye.x + leftEye.width / 2, leftEye.y + leftEye.height / 2);

    MatOfFloat diff = new MatOfFloat();
    Core.subtract(leftPoint, rightPoint, diff);
    double angle = Core.fastAtan2(diff.toArray()[1], diff.toArray()[0]);
    double dist = Core.norm(leftPoint, rightPoint, Core.NORM_L2);
    Eyes eyes = new Eyes(dist, rightPoint, leftPoint, angle);
    return eyes;
}
 
Example 14
Source Project: classchecks   File: ImgprocessUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 其主要思路为:
	1、求取源图I的平均灰度,并记录rows和cols;
	2、按照一定大小,分为N*M个方块,求出每块的平均值,得到子块的亮度矩阵D;
	3、用矩阵D的每个元素减去源图的平均灰度,得到子块的亮度差值矩阵E;
	4、用双立方差值法,将矩阵E差值成与源图一样大小的亮度分布矩阵R;
	5、得到矫正后的图像result=I-R;
* @Title: unevenLightCompensate 
* @Description: 光线补偿 
* @param image
* @param blockSize
* void 
* @throws
 */
public static void unevenLightCompensate(Mat image, int blockSize) {
	if(image.channels() == 3) {
		Imgproc.cvtColor(image, image, 7);
	}
	double average = Core.mean(image).val[0];
	Scalar scalar = new Scalar(average);
	int rowsNew = (int) Math.ceil((double)image.rows() / (double)blockSize);
	int colsNew = (int) Math.ceil((double)image.cols() / (double)blockSize);
	Mat blockImage = new Mat();
	blockImage = Mat.zeros(rowsNew, colsNew, CvType.CV_32FC1);
	for(int i = 0; i < rowsNew; i ++) {
		for(int j = 0; j < colsNew; j ++) {
			int rowmin = i * blockSize;
			int rowmax = (i + 1) * blockSize;
			if(rowmax > image.rows()) rowmax = image.rows();
			int colmin = j * blockSize;
			int colmax = (j +1) * blockSize;
			if(colmax > image.cols()) colmax = image.cols();
			Range rangeRow = new Range(rowmin, rowmax);
			Range rangeCol = new Range(colmin, colmax);
			Mat imageROI = new Mat(image, rangeRow, rangeCol);
			double temaver = Core.mean(imageROI).val[0];
			blockImage.put(i, j, temaver);
		}
	}
	
	Core.subtract(blockImage, scalar, blockImage);
	Mat blockImage2 = new Mat();
	int INTER_CUBIC = 2;
	Imgproc.resize(blockImage, blockImage2, image.size(), 0, 0, INTER_CUBIC);
	Mat image2 = new Mat();
	image.convertTo(image2, CvType.CV_32FC1);
	Mat dst = new Mat();
	Core.subtract(image2, blockImage2, dst);
	dst.convertTo(image, CvType.CV_8UC1);
}
 
Example 15
Source Project: OptimizedImageEnhance   File: Filters.java    License: 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 16
Source Project: OptimizedImageEnhance   File: Filters.java    License: 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 17
public static Mat enhance(Mat image, double krnlRatio, double minAtmosLight, double eps) {
	image.convertTo(image, CvType.CV_32F);
	// extract each color channel
	List<Mat> rgb = new ArrayList<>();
	Core.split(image, rgb);
	Mat rChannel = rgb.get(0);
	Mat gChannel = rgb.get(1);
	Mat bChannel = rgb.get(2);
	int rows = rChannel.rows();
	int cols = rChannel.cols();
	// derive the dark channel from original image
	Mat dc = rChannel.clone();
	for (int i = 0; i < image.rows(); i++) {
		for (int j = 0; j < image.cols(); j++) {
			double min = Math.min(rChannel.get(i, j)[0], Math.min(gChannel.get(i, j)[0], bChannel.get(i, j)[0]));
			dc.put(i, j, min);
		}
	}
	// minimum filter
	int krnlSz = Double.valueOf(Math.max(Math.max(rows * krnlRatio, cols * krnlRatio), 3.0)).intValue();
	Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(krnlSz, krnlSz), new Point(-1, -1));
	Imgproc.erode(dc, dc, kernel);
	// get coarse transmission map
	Mat t = dc.clone();
	Core.subtract(t, new Scalar(255.0), t);
	Core.multiply(t, new Scalar(-1.0), t);
	Core.divide(t, new Scalar(255.0), t);
	// obtain gray scale image
	Mat gray = new Mat();
	Imgproc.cvtColor(image, gray, Imgproc.COLOR_RGB2GRAY);
	Core.divide(gray, new Scalar(255.0), gray);
	// refine transmission map
	int r = krnlSz * 4;
	t = Filters.GuidedImageFilter(gray, t, r, eps);
	// get minimum atmospheric light
	minAtmosLight = Math.min(minAtmosLight, Core.minMaxLoc(dc).maxVal);
	// dehaze each color channel
	rChannel = dehaze(rChannel, t, minAtmosLight);
	gChannel = dehaze(gChannel, t, minAtmosLight);
	bChannel = dehaze(bChannel, t, minAtmosLight);
	// merge three color channels to a image
	Mat outval = new Mat();
	Core.merge(new ArrayList<>(Arrays.asList(rChannel, gChannel, bChannel)), outval);
	outval.convertTo(outval, CvType.CV_8UC1);
	return outval;
}
 
Example 18
Source Project: ImageEnhanceViaFusion   File: Filters.java    License: 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;
}
 
Example 19
Source Project: ImageEnhanceViaFusion   File: Filters.java    License: 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 20
private void computePhi(){
    Mat Psi_repeated = new Mat();
    Core.repeat(Psi, Gamma.rows(), 1, Psi_repeated);
    Core.subtract(Gamma, Psi_repeated, Phi);
}