Compute Threshold value using Balanced histogram

조회 수: 7 (최근 30일)
Adnan Khan
Adnan Khan 2021년 7월 22일
댓글: Adnan Khan 2021년 7월 23일
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
Image Analyst
Image Analyst 2021년 7월 23일
Why did you not do line 18 of the example?
Adnan Khan
Adnan Khan 2021년 7월 23일
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 💐💐

댓글을 달려면 로그인하십시오.

답변 (0개)

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by