MATLAB: Finding minimum and maximum value in a histogram

histogramimageimage processingmaximaminima

I have an image of a face with all its non-skin part removed (hair, shirt, eyebrow, etc). From that image, I want to find its minimum and maximum value in its histogram of X and Y channel (CIE XYZ). I've tried sorting value from left side to middle and right side to middle, but the minimum value gets messed up. Anyone have an idea I can try?
Here's the code I've tried:
clc
clear all
close all
img_rgb=imread('crop02.jpg');
img_xyz=rgb2xyz(img_rgb);
img_x=img_xyz(:,:,1);
img_y=img_xyz(:,:,2);
hist_x=imhist(img_x);
hist_y=imhist(img_y);
min_x=0;
max_x=0;
min_y=0;
max_y=0;
for i=1:1:length(hist_x)
if min_x==0
if hist_x(i)~=0
min_x=i;
end
end
end
for i=length(hist_x):-1:1
if max_x==0
if hist_x(i)~=0
max_x=i;
end
end
end
for i=1:1:length(hist_y)
if min_y==0
if hist_y(i)~=0
min_y=i;
end
end
end
for i=length(hist_y):-1:1
if max_y==0
if hist_y(i)~=0
max_y=i;
end
end
end
min_x=(min_x-1)/(length(hist_x)-1)
max_x=(max_x-1)/(length(hist_x)-1)
min_y=(min_y-1)/(length(hist_y)-1)
max_y=(max_y-1)/(length(hist_y)-1)
+EDIT+ I'm trying to find minimum and maximum value of X-axis, not the Y-axis, in the area I marked. With min() and max() function, the minimum value is 0, and maximum value is about 0.7~0.9 with all three of them. As I see it, those values do not represent the area I marked. The second histogram should have min value of ~0.07 and max value of ~0.32. How should I do it?

Best Answer

If you want the max and min in the image (rather than of the bin center or edge values), you can use max():
min_x = min(img_x(:));
min_y = min(img_y(:));
max_x = max(img_x(:));
max_y = max(img_y(:));