%% Perform Histogram Equalization by Using OpenCV in MATLAB
% Perform histogram equalization of an image by using the prebuilt MATLAB interface to the OpenCV function cv::CLAHE. The cv::CLAHE function is a base class, and you must create a MATLAB interface object by using the getBasePtr utility function to access the public methods and attributes of the base class.
% Add the MATLAB interface to OpenCV package names to the import list.

import clib.opencv.*;
import vision.opencv.util.*;

% Read an image into the MATLAB workspace.

img = imread("cameraman.tif");

% Create the MATLAB interface objects for the OpenCV MatND and InputArray classes to store the input image.

[inputMat,inputArray] = createMat(img);

% Create the MATLAB interface objects for the OpenCV MatND and OutputArray classes to write the output image returned by the OpenCV function.

[outputMat,outputArray] = createMat;

% Create a MATLAB interface object to represent the smart pointer of the OpenCV CLAHE class cv::CLAHE.

ocvPtr = cv.createCLAHE;

% Create a MATLAB interface object for the base class cv::CLAHE.

basePtr = getBasePtr(ocvPtr);

% Set the value of clip limit to 4.

basePtr.setClipLimit(4);

% Perform contrast-limited adaptive histogram equalization by using the apply method of the cv::CLAHE base class.

basePtr.apply(inputArray,outputArray);

% Read the enhanced output image.

enhanced = getImage(outputArray);

% Display the original input and the enhanced output image.

figure
imshow(img)
title("Input Image")
figure
imshow(enhanced)
title("Enhanced Image")

% Copyright 2021 The MathWorks, Inc.