Hi, I'm a bit confused with how Percent contrast is calculated within Imaging software (eg 0% to 100%). This may be more a mathematical question than a MATLAB question, but I'll post the code. I have an matrix image "Im" let's say [1024 1024] with various intensity values. I understand a subset of intensity values is stretched to the desired output range, let's assume linearly, to change contrast. But what in the world does a "percentage" mean? 50% on most programs denotes no change in contrast. Contrast stretching is given by the following equation (which is a pixel operation)
Im_new = a.*(Im - Imid)+Imid
(brightness has been taken out of this equation). This is how I think it is performed in most imaging softwares, where a is some coefficient that should be
a = (highestIntensityOutput - lowestIntensityOutput)/(highestIntensityInitial-lowestIntensityInitial).
If a>1, some percentage of histogram above and below certain mid-range intensity values is threshholded, but I may be wrong.
Here is my code for adjusting contrast display manually: (Im is for original image)
vec = I(:);
minn =min(vec);
maxx =max(vec);
nbins = maxx-minn;
edges = linspace(minn,maxx,nbins);
hist_c = histcounts(vec,nbins);
plot(edges(1:nbins), hist_c);
Now let's say you have chosen to use a subset of the intensity values in the image and have selected loValue and hiValue as the range for your intensity values. Then we find the midpoint of this range for our contrast calculation (Note: this may be either wrong, or clunky).
lo = abs(edges-loValue);
hi = abs(edges-hiValue);
[~, i] = sort(lo);
[~,j]=sort(hi);
mid = round((j(1)-i(1))/2)+i(1);
midI = edges(mid);
Now we have the mid-Intensity value about which we will adjust the contrast. For the desired percent <50% contrast, I would assume the following (and this is where I might be horribly wrong), where cPer is "percent contrast". Note, this is only for percent contrast <50%.
a = (cPer/100)*2;
Ig = a.*(I-midI)+midI;
Ig(Ig<loValue) = loValue;
Ig(Ig>hiValue) = hiValue;
Is this correct? Is this how the "percent contrast" is calculated below 50% (I will make another post for above 50%). Comparison to the software tells me no. What I essentially did was map 0 (%) to 50(%) to values from 0 to 1 for the "a" stretch coefficient, which is what I assumed the software was doing. Sorry if this is such a confusing question, I'm probably making it 50x more complicated than it actually is.