OpenCV.直方图均衡化

这篇博客介绍了直方图均衡化的概念及其在Java中实现的步骤。通过改变图像的灰度直方图分布,使用OpenCV库进行图像处理,实现了调整图像亮度和对比度的目的。代码示例展示了如何读取图像、转换为灰度、执行直方图均衡化,并绘制直方图。最后将处理后的图像显示出来。
摘要由CSDN通过智能技术生成

直方图均衡化

直方图均衡化其作用为改变原有的图像直方图分布,用改变后的灰度LUT方式重建图像,来调整图像的亮度与对比度。其函数声明如下:

equalizeHist(src, dst);

其中src为单通道的8单位灰度图像,dst为输出源。

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 equalizeHistogram(String filePath) throws IOException {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        // Read source image to src.
        Mat src = Imgcodecs.imread(filePath);
        Mat dst = new Mat();

        // Create a temp mat for calcHist's first parameter.
        Mat convert_temp = new Mat();

        // Convert src to gray mat, and equalized histogram.
        Mat gray = new Mat();
        Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY);
        Imgproc.equalizeHist(gray, dst);

        // Copy dst ro temp mat.
        dst.copyTo(convert_temp);

        List<Mat> images = new ArrayList<>();
        images.add(convert_temp);
        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;
    }

}

运行图
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值