直方图比较
直方图系图像的本身属性之一,所受外界操纵变化较小,可用来比较图像间的相似度。假设两幅图像的直方图为H1与H2,其距离d则表示其相互匹配的程度,下面是OpenCV提供的计算方法:
compareHist(H1, H2, method);
H1与H2系Mat对象,method系计算方法,枚举类型,其值如下:
-
HISTCMP_CORREL
相关性 -
HISTCMP_CHISQR
卡方 -
HISTCMP_INTERSECT
相交 -
HISTCMP_BHATTACHARYYA
巴氏距离 -
HISTCMP_HELLINGER
同上 -
HISTCMP_CHISQR_ALT
可选卡方 -
HISTCMP_KL_DIV
散度
其中相关性越接近1表示越像;卡方越接近0表示越像;相交法数值越大表示越像;巴氏距离越接近0表示越像。
Java代码(JavaFX Controller层)
public class Controller{
@FXML private Text fxText;
@FXML private ImageView imageView;
@FXML private TextArea textArea;
@FXML public void handleButtonEvent(ActionEvent actionEvent) throws IOException {
Node source = (Node) actionEvent.getSource();
Window theStage = source.getScene().getWindow();
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("PNG files (*.png)", "*.png");
fileChooser.getExtensionFilters().add(extFilter);
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("JPG Files(*.jpg)", "*.jpg"));
File file = fileChooser.showOpenDialog(theStage);
runInSubThread(file.getPath());
}
private void runInSubThread(String filePath){
new Thread(new Runnable() {
@Override
public void run() {
try {
WritableImage writableImage = histogramCompared(filePath);
Platform.runLater(new Runnable() {
@Override
public void run() {
imageView.setImage(writableImage);
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
private WritableImage histogramCompared(String filePath) throws IOException {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
// Read source image to src.
Mat src = Imgcodecs.imread(filePath);
Mat dst = new Mat();
Mat gray = new Mat();
Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY);
Imgproc.equalizeHist(gray, dst);
List<Mat> images = new ArrayList<>();
images.add(gray);
Mat mask = Mat.ones(src.size(), CvType.CV_8UC1);
Mat hist_1 = new Mat();
Imgproc.calcHist(images, new MatOfInt(0), mask, hist_1, new MatOfInt(256), new MatOfFloat(0,255));
Core.normalize(hist_1, hist_1,0,255, Core.NORM_MINMAX);
images.clear();
Mat src_2 = Imgcodecs.imread("E:/Chrome/lenna_test.jpg");
Mat dst_2 = new Mat();
Mat gray_2 = new Mat();
Imgproc.cvtColor(src_2, gray_2, Imgproc.COLOR_BGR2GRAY);
Imgproc.equalizeHist(gray_2, dst_2);
images.add(gray_2);
Mat hist_2 = new Mat();
Imgproc.calcHist(images, new MatOfInt(0), mask, hist_2, new MatOfInt(256), new MatOfFloat(0,255));
Core.normalize(hist_2, hist_2,0,255, Core.NORM_MINMAX);
double[] distanceArray = new double[7];
distanceArray[0] = Imgproc.compareHist(hist_1, hist_2, Imgproc.HISTCMP_CORREL);
distanceArray[1] = Imgproc.compareHist(hist_1, hist_2, Imgproc.HISTCMP_CHISQR);
distanceArray[2] = Imgproc.compareHist(hist_1, hist_2, Imgproc.HISTCMP_INTERSECT);
distanceArray[3] = Imgproc.compareHist(hist_1, hist_2, Imgproc.HISTCMP_BHATTACHARYYA);
distanceArray[4] = Imgproc.compareHist(hist_1, hist_2, Imgproc.HISTCMP_HELLINGER);
distanceArray[5] = Imgproc.compareHist(hist_1, hist_2, Imgproc.HISTCMP_CHISQR_ALT);
distanceArray[6] = Imgproc.compareHist(hist_1, hist_2, Imgproc.HISTCMP_KL_DIV);
List<String> typeList = new ArrayList<>();
typeList.add("HISTCMP_CORREL");
typeList.add("HISTCMP_CHISQR");
typeList.add("HISTCMP_INTERSECT");
typeList.add("HISTCMP_BHATTACHARYYA");
typeList.add("HISTCMP_HELLINGER");
typeList.add("HISTCMP_CHISQR_ALT");
typeList.add("HISTCMP_KL_DIV");
for (int i = 0; i < 7; i++) {
textArea.appendText("Type: " + typeList.get(i) + " | Distance: "+ distanceArray[i] + "\n");
}
src.copyTo(dst);
src_2.copyTo(dst_2);
MatOfByte matOfByte = new MatOfByte();
Imgcodecs.imencode(".jpg", dst, matOfByte);
byte[] bytes = matOfByte.toArray();
InputStream in = new ByteArrayInputStream(bytes);
BufferedImage bufImage = ImageIO.read(in);
WritableImage writableImage = SwingFXUtils.toFXImage(bufImage, null);
return writableImage;
}
}
相关图片