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

The following are Jave code examples for showing how to use subtract() of the org.opencv.core.Core class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: OptimizedImageEnhance   File: TransmissionEstimate.java   Source Code and License Vote up 7 votes
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
Project: OptimizedImageEnhance   File: FeatureWeight.java   Source Code and License Vote up 6 votes
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 3
Project: OptimizedImageEnhance   File: FeatureWeight.java   Source Code and License Vote up 6 votes
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
Project: ImageEnhanceViaFusion   File: WeightCalculate.java   Source Code and License Vote up 6 votes
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
Project: ImageEnhanceViaFusion   File: Pyramid.java   Source Code and License Vote up 6 votes
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
Project: DNNLibrary   File: MainActivity.java   Source Code and License Vote up 6 votes
private float[] getInputDataLeNet(Bitmap bitmap) {
    final int INPUT_LENGTH = 28;

    Mat imageMat = new Mat();
    Mat inputMat = new Mat();

    Utils.bitmapToMat(bitmap, imageMat);

    // convert the image to 28 * 28, grayscale, 0~1, and smaller means whiter
    Imgproc.cvtColor(imageMat, imageMat, Imgproc.COLOR_RGBA2GRAY);
    imageMat = centerCropAndScale(imageMat, INPUT_LENGTH);
    imageMat.convertTo(imageMat, CvType.CV_32F, 1. / 255);
    Core.subtract(Mat.ones(imageMat.size(), CvType.CV_32F), imageMat, inputMat);

    float[] inputData = new float[inputMat.width() * inputMat.height()];

    inputMat.get(0, 0, inputData);

    return inputData;
}
 
Example 7
Project: OptimizedImageEnhance   File: BlkTransEstimate.java   Source Code and License Vote up 5 votes
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
Project: OptimizedImageEnhance   File: OptimizedContrastEnhance.java   Source Code and License Vote up 5 votes
@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
Project: OptimizedImageEnhance   File: OptimizedContrastEnhance.java   Source Code and License Vote up 5 votes
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
Project: OptimizedImageEnhance   File: DarkChannelPriorDehaze.java   Source Code and License Vote up 5 votes
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
Project: OptimizedImageEnhance   File: RemoveBackScatter.java   Source Code and License Vote up 5 votes
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
Project: classchecks   File: ImgprocessUtils.java   Source Code and License Vote up 5 votes
/**
 * 其主要思路为:
	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 13
Project: classchecks   File: Recognition.java   Source Code and License Vote up 5 votes
public static Mat subspaceProject(Mat W, Mat mean, Mat src) {
	int n = src.rows();
	int d = src.cols();
	Mat X = new Mat();
	Mat Y = new Mat();
	src.convertTo(X, W.type());
	if(!mean.empty()) {
		for(int i = 0; i < n; i ++) {
			Mat r_i = X.row(i);
			Core.subtract(r_i, mean.reshape(1, 1), r_i);
		}
	}
	Core.gemm(X, W, 1.0, new Mat(), 0.0, Y);
	return Y;
}
 
Example 14
Project: OptimizedImageEnhance   File: Filters.java   Source Code and License Vote up 4 votes
/**
 * 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 15
Project: OptimizedImageEnhance   File: Filters.java   Source Code and License Vote up 4 votes
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 16
Project: OptimizedImageEnhance   File: DarkChannelPriorDehaze.java   Source Code and License Vote up 4 votes
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 17
Project: ImageEnhanceViaFusion   File: Filters.java   Source Code and License Vote up 4 votes
/**
 * 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 18
Project: ImageEnhanceViaFusion   File: Filters.java   Source Code and License Vote up 4 votes
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
Project: DNNLibrary   File: MainActivity.java   Source Code and License Vote up 4 votes
private float[] getInputDataResNet18(Bitmap bitmap) {
    final int INPUT_SIDE_LENGTH = 224;

    Mat imageMat = new Mat();

    Utils.bitmapToMat(bitmap, imageMat);

    Imgproc.cvtColor(imageMat, imageMat, Imgproc.COLOR_RGBA2BGR);
    imageMat = centerCropAndScale(imageMat, INPUT_SIDE_LENGTH);
    Core.subtract(imageMat, new Scalar(104, 117, 123), imageMat);
    imageMat.convertTo(imageMat, CvType.CV_32FC3);

    float[] inputData = new float[imageMat.width() * imageMat.height() * imageMat.channels()];

    imageMat.get(0, 0, inputData);

    return inputData;
}
 
Example 20
Project: SpotSpotter   File: AutoEdgeDetect.java   Source Code and License Vote up 3 votes
public static Mat[] iRCF_NH_ME(Mat image, int blurVal, double minAreaSize, int offset) {
	Mat[] matSet = new Mat[3];

	// MatView.imshow(image, "Ori");

	final Mat gray = new Mat();
	Imgproc.cvtColor(image, gray, Imgproc.COLOR_RGB2GRAY);

	final double percentOfCrop = 0.5;
	final Rect rect = new Rect((int) (gray.width() * percentOfCrop / 2), (int) (gray.height() * percentOfCrop / 2),
			(int) (gray.width() * (1 - percentOfCrop)), (int) (gray.height() * (1 - percentOfCrop)));

	final Mat findThresh = gray.clone().submat(rect);
	final double thresh = Imgproc.threshold(findThresh, findThresh, 0, 255, Imgproc.THRESH_OTSU + Imgproc.THRESH_BINARY);

	Mat roi = ROI_Irregular.RectangleSubROI(gray, rect);

	Imgproc.GaussianBlur(roi, roi, new Size(blurVal, blurVal), 0);

	// MatView.imshow(roi, "ROI");

	Imgproc.threshold(roi, roi, thresh, 255, Imgproc.THRESH_BINARY);
	// MatView.imshow(roi, "Bin");

	matSet = largestContour(image.clone(), roi, minAreaSize, offset);

	final Mat emptyBox = matSet[2];
	// MatView.imshow(emptyBox, "EmptyBox");

	roi = matSet[0];
	// MatView.imshow(roi, "Contour");

	Core.subtract(roi, emptyBox, roi);

	// Core.bitwise_xor(roi, emptyBox, roi);
	// MatView.imshow(roi, "Contour2");

	final Mat box = matSet[1];
	// MatView.imshow(box, "Box");

	Core.bitwise_and(image, roi, roi);
	// MatView.imshow(roi, "Final ROI1");

	return matSet;
}