Compute Threshold value using Balanced histogram

6 views (last 30 days)
I want to compute balanced histogram threshodling value. The function I want to create must give an optimum value for threshold. I have adapted code from here The AI Learner which is basically in python but I converted it to MATLAB as follow:
However, the optimum value calculated is 234 which isn't accurate for the following attached histogram. Testing image lenna512_low_dynamic_range.bmp is also attached.
Any help would be appreciated.
clc;clear all; close all;
im_ldr=uint8(imread('lenna512_low_dynamic_range.bmp'));
subplot(2,2,1);
imshow(im_ldr);
title('Original Image');
subplot(2,2,2);
imhist(im_ldr);
title('Non-Equalized Histogram');
x = imhist(im_ldr);
i_s = find(x>0,1,'first');
i_e = find(x>0,1,'last');
i_m = (i_s + i_e)/2;
A = i_s:i_m;
w_l = sum(A);
B = i_m:i_e;
w_r = sum(B);
while i_s ~= i_e
if w_r > w_l
w_r = w_r - x(i_e);
if ((i_s + i_e)/2) < i_m
w_l = w_l - i_m;
w_r = w_r + i_m;
i_m = i_m-1;
end
else
w_l = w_l - i_s;
i_s = i_s + 1;
if ((i_s + i_e)/2) >= i_m;
w_l = w_l + i_m+1;
w_r = w_r - i_m+1;
i_m = i_m + 1;
end
end
end
  2 Comments
Adnan Khan
Adnan Khan on 23 Jul 2021
Ohh, that's my silly mistake. Sorry for wasting your time Image Analyst.
I added the line and now i_m value is 177, which seems not bad. Can you please go through my code and let me know whether my MATLAB implementation is as correct as that in the above link?
Also if there is any built-in command in MATLAB which computes the threshold value using the same process and with which I could compare the results, please let me know that too.
Thank you so much 💐💐

Sign in to comment.

Answers (0)

Products


Release

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!