OpenCV - Égalisation d'histogramme

le histogramd'une image montre la fréquence des valeurs d'intensité des pixels. Dans un histogramme d'image, l'axe X montre les intensités du niveau de gris et l'axe Y montre la fréquence de ces intensités.

Histogram equalizationaméliore le contraste d'une image, afin d'étendre la plage d'intensité. Vous pouvez égaliser l'histogramme d'une image donnée en utilisant la méthodeequalizeHist() du Imgprocclasse. Voici la syntaxe de cette méthode.

equalizeHist(src, dst)

Cette méthode accepte les paramètres suivants -

  • src - Un objet de la classe Mat représentant l'image source (entrée).

  • dst - Un objet de la classe Matreprésentant la sortie. (Image obtenue après égalisation de l'histogramme)

Exemple

Le programme suivant montre comment égaliser l'histogramme d'une image donnée.

import java.util.ArrayList;
import java.util.List;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

public class HistoTest {
   public static void main (String[] args) {
      // Loading the OpenCV core library
      System.loadLibrary( Core.NATIVE_LIBRARY_NAME );

      // Reading the Image from the file and storing it in to a Matrix object
      String file ="E:/OpenCV/chap20/histo_input.jpg";

      // Load the image
      Mat img = Imgcodecs.imread(file);

      // Creating an empty matrix
      Mat equ = new Mat();
      img.copyTo(equ);

      // Applying blur
      Imgproc.blur(equ, equ, new Size(3, 3));

      // Applying color
      Imgproc.cvtColor(equ, equ, Imgproc.COLOR_BGR2YCrCb);
      List<Mat> channels = new ArrayList<Mat>();

      // Splitting the channels
      Core.split(equ, channels);

      // Equalizing the histogram of the image
      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);

      Imgcodecs.imwrite("E:/OpenCV/chap20/histo_output.jpg", equ);
      System.out.println("Image Processed");
   }
}

Supposons que ce qui suit est l'image d'entrée histo_input.jpg spécifié dans le programme ci-dessus.

Production

Lors de l'exécution du programme, vous obtiendrez la sortie suivante -

Image Processed

Si vous ouvrez le chemin spécifié, vous pouvez observer l'image de sortie comme suit -