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

The following examples show how to use org.opencv.core.Core#merge() . 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: OptimizedContrastEnhance.java    From OptimizedImageEnhance with MIT License 6 votes vote down vote up
public static Mat enhance(Mat image, int blkSize, int patchSize, double lambda, double eps, int krnlSize) {
	image.convertTo(image, CvType.CV_32F);
	// obtain air-light
	double[] airlight = AirlightEstimate.estimate(image, blkSize);
	// obtain coarse transmission map
	double fTrans = 0.5;
	Mat T = TransmissionEstimate.transEstimate(image, patchSize, airlight, lambda, fTrans);
	// refine the transmission map
	Mat gray = new Mat();
	Imgproc.cvtColor(image, gray, Imgproc.COLOR_RGB2GRAY);
	Core.divide(gray, new Scalar(255.0), gray);
	T = Filters.GuidedImageFilter(gray, T, krnlSize, eps);
	// dehaze
	List<Mat> bgr = new ArrayList<>();
	Core.split(image, bgr);
	Mat bChannel = dehaze(bgr.get(0), T, airlight[0]);
	//Core.normalize(bChannel, bChannel, 0, 255, Core.NORM_MINMAX);
	Mat gChannel = dehaze(bgr.get(1), T, airlight[1]);
	//Core.normalize(gChannel, gChannel, 0, 255, Core.NORM_MINMAX);
	Mat rChannel = dehaze(bgr.get(2), T, airlight[2]);
	//Core.normalize(rChannel, rChannel, 0, 255, Core.NORM_MINMAX);
	Mat dehazedImg = new Mat();
	Core.merge(new ArrayList<>(Arrays.asList(bChannel, gChannel, rChannel)), dehazedImg);
	return dehazedImg;
}
 
Example 2
Source File: FusionEnhance.java    From OptimizedImageEnhance with MIT License 6 votes vote down vote up
private static Mat[] applyCLAHE(Mat img, Mat L) {
	Mat[] result = new Mat[2];
	CLAHE clahe = Imgproc.createCLAHE();
	clahe.setClipLimit(2.0);
	Mat L2 = new Mat();
	clahe.apply(L, L2);
	Mat LabIm2 = new Mat();
	List<Mat> lab = new ArrayList<>();
	Core.split(img, lab);
	Core.merge(new ArrayList<>(Arrays.asList(L2, lab.get(1), lab.get(2))), LabIm2);
	Mat img2 = new Mat();
	Imgproc.cvtColor(LabIm2, img2, Imgproc.COLOR_Lab2BGR);
	result[0] = img2;
	result[1] = L2;
	return result;
}
 
Example 3
Source File: GuidedFilterFlashExample.java    From OptimizedImageEnhance with MIT License 6 votes vote down vote up
public static void main(String[] args) {
	String imgPath = "src/main/resources/dcp_images/flash/cave-flash.bmp";
	String guidedImgPath = "src/main/resources/dcp_images/flash/cave-noflash.bmp";
	Mat image = Imgcodecs.imread(imgPath, Imgcodecs.CV_LOAD_IMAGE_COLOR);
	new ImShow("image").showImage(image);
	image.convertTo(image, CvType.CV_32F);
	Mat guide = Imgcodecs.imread(guidedImgPath, Imgcodecs.CV_LOAD_IMAGE_COLOR);
	guide.convertTo(guide, CvType.CV_32F);
	List<Mat> img = new ArrayList<>();
	List<Mat> gid = new ArrayList<>();
	Core.split(image, img);
	Core.split(guide, gid);
	
	int r = 8;
	double eps = 0.02 * 0.02;
	Mat q_r = Filters.GuidedImageFilter(img.get(0), gid.get(0), r, eps);
	Mat q_g = Filters.GuidedImageFilter(img.get(1), gid.get(1), r, eps);
	Mat q_b = Filters.GuidedImageFilter(img.get(2), gid.get(2), r, eps);
	Mat q = new Mat();
	Core.merge(new ArrayList<>(Arrays.asList(q_r, q_g, q_b)), q);
	q.convertTo(q, CvType.CV_8UC1);
	new ImShow("q").showImage(q);
}
 
Example 4
Source File: GuidedFilterEnhanceExample.java    From OptimizedImageEnhance with MIT License 6 votes vote down vote up
public static void main(String[] args) {
	String imgPath = "src/main/resources/dcp_images/enhancement/tulips.bmp";
	Mat image = Imgcodecs.imread(imgPath, Imgcodecs.CV_LOAD_IMAGE_COLOR);
	new ImShow("image").showImage(image);
	image.convertTo(image, CvType.CV_32F);
	List<Mat> img = new ArrayList<>();
	Core.split(image, img);
	int r = 16;
	double eps = 0.01;
	
	Mat q_r = Filters.GuidedImageFilter(img.get(0), img.get(0), r, eps);
	Mat q_g = Filters.GuidedImageFilter(img.get(1), img.get(1), r, eps);
	Mat q_b = Filters.GuidedImageFilter(img.get(2), img.get(2), r, eps);
	
	Mat q = new Mat();
	Core.merge(new ArrayList<>(Arrays.asList(q_r, q_g, q_b)), q);
	q.convertTo(q, CvType.CV_8UC1);
	new ImShow("q").showImage(q);
}
 
Example 5
Source File: EnhanceFunc.java    From ImageEnhanceViaFusion with MIT License 6 votes vote down vote up
private static Mat[] applyCLAHE(Mat img, Mat L) {
	Mat[] result = new Mat[2];
	CLAHE clahe = Imgproc.createCLAHE();
	clahe.setClipLimit(2.0);
	Mat L2 = new Mat();
	clahe.apply(L, L2);
	Mat LabIm2 = new Mat();
	List<Mat> lab = new ArrayList<Mat>();
	Core.split(img, lab);
	Core.merge(new ArrayList<Mat>(Arrays.asList(L2, lab.get(1), lab.get(2))), LabIm2);
	Mat img2 = new Mat();
	Imgproc.cvtColor(LabIm2, img2, Imgproc.COLOR_Lab2BGR);
	result[0] = img2;
	result[1] = L2;
	return result;
}
 
Example 6
Source File: HistogramEqualization.java    From opencv-fun with GNU Affero General Public License v3.0 6 votes vote down vote up
public static void main (String[] args) {
	CVLoader.load();
	
	// load the image
	Mat img = Highgui.imread("data/topdown-9.png");
	Mat equ = new Mat();
	img.copyTo(equ);
	Imgproc.blur(equ, equ, new Size(3, 3));
	
	Imgproc.cvtColor(equ, equ, Imgproc.COLOR_BGR2YCrCb);
	List<Mat> channels = new ArrayList<Mat>();
	Core.split(equ, channels);
	Imgproc.equalizeHist(channels.get(0), channels.get(0));
	Core.merge(channels, equ);
	Imgproc.cvtColor(equ, equ, Imgproc.COLOR_YCrCb2BGR);
	
	Mat gray = new Mat();
	Imgproc.cvtColor(equ, gray, Imgproc.COLOR_BGR2GRAY);
	Mat grayOrig = new Mat();
	Imgproc.cvtColor(img, grayOrig, Imgproc.COLOR_BGR2GRAY);
	
	ImgWindow.newWindow(img);
	ImgWindow.newWindow(equ);
	ImgWindow.newWindow(gray);
	ImgWindow.newWindow(grayOrig);
}
 
Example 7
Source File: Filters.java    From OptimizedImageEnhance with MIT License 5 votes vote down vote up
/**
 * Simplest Color Balance. Performs color balancing via histogram
 * normalization.
 *
 * @param img input color or gray scale image
 * @param percent controls the percentage of pixels to clip to white and black. (normally, choose 1~10)
 * @return Balanced image in CvType.CV_32F
 */
public static Mat SimplestColorBalance(Mat img, int percent) {
	if (percent <= 0)
		percent = 5;
	img.convertTo(img, CvType.CV_32F);
	List<Mat> channels = new ArrayList<>();
	int rows = img.rows(); // number of rows of image
	int cols = img.cols(); // number of columns of image
	int chnls = img.channels(); //  number of channels of image
	double halfPercent = percent / 200.0;
	if (chnls == 3) Core.split(img, channels);
	else channels.add(img);
	List<Mat> results = new ArrayList<>();
	for (int i = 0; i < chnls; i++) {
		// find the low and high precentile values (based on the input percentile)
		Mat flat = new Mat();
		channels.get(i).reshape(1, 1).copyTo(flat);
		Core.sort(flat, flat, Core.SORT_ASCENDING);
		double lowVal = flat.get(0, (int) Math.floor(flat.cols() * halfPercent))[0];
		double topVal = flat.get(0, (int) Math.ceil(flat.cols() * (1.0 - halfPercent)))[0];
		// saturate below the low percentile and above the high percentile
		Mat channel = channels.get(i);
		for (int m = 0; m < rows; m++) {
			for (int n = 0; n < cols; n++) {
				if (channel.get(m, n)[0] < lowVal) channel.put(m, n, lowVal);
				if (channel.get(m, n)[0] > topVal) channel.put(m, n, topVal);
			}
		}
		Core.normalize(channel, channel, 0.0, 255.0 / 2, Core.NORM_MINMAX);
		channel.convertTo(channel, CvType.CV_32F);
		results.add(channel);
	}
	Mat outval = new Mat();
	Core.merge(results, outval);
	return outval;
}
 
Example 8
Source File: OptimizedContrastEnhance.java    From OptimizedImageEnhance with MIT License 5 votes vote down vote up
@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
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 10
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 11
Source File: DarkChannelPriorDehaze.java    From OptimizedImageEnhance with MIT License 4 votes vote down vote up
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 12
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 13
Source File: ColorBalance.java    From ImageEnhanceViaFusion with MIT License 4 votes vote down vote up
/**
 * Simplest Color Balance. Performs color balancing via histogram
 * normalization.
 *
 * @param img
 *            input color or gray scale image
 * @param percent
 *            controls the percentage of pixels to clip to white and black.
 *            (normally, choose 1~10)
 * @return Balanced image in CvType.CV_32F
 */
public static Mat SimplestColorBalance(Mat img, int percent) {
	if (percent <= 0)
		percent = 5;
	img.convertTo(img, CvType.CV_32F);
	List<Mat> channels = new ArrayList<Mat>();
	int rows = img.rows(); // number of rows of image
	int cols = img.cols(); // number of columns of image
	int chnls = img.channels(); //  number of channels of image
	double halfPercent = percent / 200.0;
	if (chnls == 3) {
		Core.split(img, channels);
	} else {
		channels.add(img);
	}
	List<Mat> results = new ArrayList<Mat>();
	for (int i = 0; i < chnls; i++) {
		// find the low and high precentile values (based on the input percentile)
		Mat flat = new Mat();
		channels.get(i).reshape(1, 1).copyTo(flat);
		Core.sort(flat, flat, Core.SORT_ASCENDING);
		double lowVal = flat.get(0, (int) Math.floor(flat.cols() * halfPercent))[0];
		double topVal = flat.get(0, (int) Math.ceil(flat.cols() * (1.0 - halfPercent)))[0];
		// saturate below the low percentile and above the high percentile
		Mat channel = channels.get(i);
		for (int m = 0; m < rows; m++) {
			for (int n = 0; n < cols; n++) {
				if (channel.get(m, n)[0] < lowVal)
					channel.put(m, n, lowVal);
				if (channel.get(m, n)[0] > topVal)
					channel.put(m, n, topVal);
			}
		}
		Core.normalize(channel, channel, 0, 255, Core.NORM_MINMAX);
		channel.convertTo(channel, CvType.CV_32F);
		results.add(channel);
	}
	Mat outval = new Mat();
	Core.merge(results, outval);
	return outval;
}