function [Ieq, c, r, npix] = roihisteq(I, c, r)
%ROIHISTEQ Computes the localized histogram Equalization of an ROI in an image.
% [Ieq, C, R, NPIX] = ROIHISTEQ(I, C, R) computes the localized histogram
% equalization, Ieq, of a polygonal region of interest (ROI) in image I.
% The polygonal region is defined by the column and row coordinates of its
% vertices, which are specified (sequentially) in vectors C and R,
% respectively. If C and R were not supplied, the user may interactively
% draw an ROI on the image. All pixels of F must be >= 0. Parameter NPIX
% is the number of pixels in the polygonal region.
%
% See also histeq, adapthisteq.
%
% Example:
% I = imread('tire.tif');
% imshow(I);
% [Ieq, c, r] = roihisteq(I);
% subplot(1,2,1); imshow(I); title('Original Image');
% hold on; line(c,r,'color','r');
% subplot(1,2,2); imshow(Ieq); title('ROIHISTEQ Image');
% hold on; line(c,r,'color','r');
%
% Shanrong Zhang
% Department of Radiology
% University of Washington
% 03/13/2006
%
% Generate the binary mask image.
if nargin == 3
B = roipoly(I, c, r);
else
[x,y,B,c,r] = roipoly;
end
Ieq = I;
% Compute the localized histogram equalization of the pixels in the ROI.
Ieq(B) = histeq(I(B));
% Obtain the number of pixels in the ROI if requested in the output.
if nargout > 3
npix = sum(B(:));
end