图像直方图
图像直方图反映的是图像的统计学特征,可大概看出其分布特征。
假设某输入源为普通图像,经二值化后其显示像素值范围为0~255,其像素值经过排序后呈正常分布,那么某一像素值或某一像素范围可大概描述其像素特征。即其BIN(BIN越多,直方图对颜色的分辨率越强)取值范围越大,其空间分布越平均,越小则会有尖锐。
基于此可以统计图像的BIN以此来绘制其直方图,其函数声明如下:
calcHist(images, channels, mask, hist, histSize, ranges);
各参数解释如下:
images
输入图像,List< Mat>类型。
channels
通道索引数。
mask
表示images遮盖层。
hist
计算而得的直方图数据,为一维或二维的稀疏矩阵。
histSize
直方图的大小,BIN的个数。
ranges
直方图取值范围。
Java代码(JavaFX Controller层)
public class Controller{
@FXML private Text fxText;
@FXML private ImageView imageView;
@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 = drawHistogram(filePath);
Platform.runLater(new Runnable() {
@Override
public void run() {
imageView.setImage(writableImage);
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
private WritableImage drawHistogram(String filePath) throws IOException {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat src = Imgcodecs.imread(filePath);
Mat dst = new Mat();
// Calculate image histogram data and normalization.
Mat gray = new Mat();
Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY);
List<Mat> images = new ArrayList<>();
images.add(gray);
Mat mask = Mat.ones(src.size(), CvType.CV_8UC1);
Mat hist = new Mat();
Imgproc.calcHist(images, new MatOfInt(0), mask, hist, new MatOfInt(256), new MatOfFloat(0,255));
Core.normalize(hist, hist,0,255, Core.NORM_MINMAX);
int height = hist.rows();
dst.create(400,400,src.type());
dst.setTo(new Scalar(200,200,200));
float[] histData = new float[256];
hist.get(0,0, histData);
int offset_x = 50;
int offset_y = 350;
// Draw histogram.
Imgproc.line(dst, new Point(offset_x, 0), new Point(offset_x, offset_y), new Scalar(0,0,0));
Imgproc.line(dst, new Point(offset_x, offset_y), new Point(400, offset_y), new Scalar(0,0,0));
for (int i = 0; i < height - 1; i++) {
int y1 = (int)histData[i];
int y2 = (int)histData[i+1];
Rect rect = new Rect();
rect.x = offset_x + i;
rect.y = offset_y - y1;
rect.width = 1;
rect.height = y1;
Imgproc.rectangle(dst, rect.tl(), rect.br(), new Scalar(15,15,15) );
}
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;
}
}
如何查看图像直方图
正文已经提及,图像直方图描述的是统计学特征。那么通尔易俗地讲,二值化的图像直方图描述的就是其黑白颜色在其空间上的分布,如下:
现在回顾一下原图,即Lenna人物图,发现其二值化图像黑色区域在帽子与镜子边缘及头发部分,而白色区域仅在部分线条和帽子区域,而值域[100,200]则占大部分,这与计算的二值图像描述刚好对应。