我尝试使用open cv的java绑定为(color)png图像应用非全局对比(histogram)优化,但是我无法使其工作。
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.File;
import javax.imageio.ImageIO;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.CLAHE;
import org.opencv.imgproc.Imgproc;
public class Main {
public static void main( String[] args ) {
try {
System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
// fetch the png
File input = new File("test.png");
BufferedImage buffImage = ImageIO.read(input);
byte[] data = ((DataBufferByte) buffImage.getRaster().getDataBuffer()).getData();
// build MAT for original image
Mat orgImage = new Mat(buffImage.getHeight(),buffImage.getWidth(), CvType.CV_8UC3);
orgImage.put(0, 0, data);
// transform from to LAB
Mat labImage = new Mat(buffImage.getHeight(), buffImage.getWidth(), CvType.CV_8UC4);
Imgproc.cvtColor(orgImage, labImage, Imgproc.COLOR_BGR2Lab);
// apply CLAHE
CLAHE clahe = Imgproc.createCLAHE()
Mat destImage = new Mat(buffImage.getHeight(),buffImage.getWidth(), CvType.CV_8UC4);
clahe.apply(labImage, destImage);
Imgcodecs.imwrite("test_clahe.png", destImage);
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
我得到例外:
Error: cv::Exception: C:\builds\master_PackSlaveAddon-win64-vc12-static\opencv\modules\imgproc\src\clahe.cpp:354: error: (-215) _src.type() == CV_8UC1 || _src.type() == CV_16UC1 in function `anonymous
-namespace'::CLAHE_Impl::apply
我想我需要使用各个频道,但我无法弄清楚如何。代码的灵感来自this c++ example,但不知怎的,我无法提取相应的图层(我想我只需要L chanel for clahe.apply())
答案 0 :(得分:1)
This Example只需拆分Lab图像并在L通道上应用Clahe,L通道是强度通道。所以只需将此代码用于java。
List<Mat> channels = new LinkedList();
Core.split(labImage, channels);
CLAHE clahe = Imgproc.createCLAHE()
Mat destImage = new Mat(buffImage.getHeight(),buffImage.getWidth(), CvType.CV_8UC4);
clahe.apply(channels[0], destImage);
Imgproc.merge(channels, labImage);
最后将强度通道合并到其他通道。我没有改变任何参数,因为我不知道你的图像看起来如何,但我想这不是问题所在。希望它有所帮助!