hi ..i am writing a code for quadrant dynamic histogram equalization...but i am getting an error while mapping back to the original image as 'Attempted to access map(0); index must be a positive integer or logical.'..i have attached the code

15 views (last 30 days)
clc;
close all;
clear all;
WAAD_img= imread('C:\Users\Public\Pictures\Sample Pictures\image14.jpg');
WAAD_img= rgb2gray(WAAD_img);
% WAAD_hist=Fn_get_hist(WAAD_img,R,C);
WAAD_hist=imhist(uint8(WAAD_img))';
m = zeros(1,5);
[R C] = size(WAAD_img);
for i = 1 : 256
if WAAD_hist(i)>R*C/100000; m(1) = i; break;
end
end
for i = 256 :-1: 1
if WAAD_hist(i)>R*C/100000; m(5) = i; break;
end
end
cdf_data = cumsum(WAAD_hist)/(R*C);
[val idx] = find(cdf_data >= 0.25 &cdf_data < 0.50);
if size(idx) ~= 0
m(2) = idx(1);
m(3) = idx(end);
else
[val idx] = find(cdf_data > 0);
m(2) = m(1);
m(3) = m(1);
end
[val idx] = find(cdf_data >= 0.75);
m(4) = idx(1)-1;
% cliping
avgFreqOfimage = floor(R*C/256);
chist_data = zeros(1,256);
for i = 1 :256
if WAAD_hist(i) >= avgFreqOfimage
chist_data(i) = avgFreqOfimage;
else
chist_data(i) = WAAD_hist(i);
end
end
span= zeros(1,4);
for i = 1 : 4
span(i) = m(i+1)-m(i);
end
range = round(256*span/sum(span));
sepPosition = cumsum([0 range]);
new_cdf= zeros(1,256);
map = zeros(1,256);
for i = 1 : 4
if i==1
temp = cumsum(chist_data(m(i):m(i+1)));
new_cdf(m(i):m(i+1)) = temp/temp(end);
map(m(i):m(i+1)) = round(range(i)*new_cdf(m(i):m(i+1)));
else
temp = cumsum(chist_data(m(i):m(i+1)));
new_cdf(m(i):m(i+1)) = temp/temp(end);
map(m(i):m(i+1)) = round(range(i)*new_cdf(m(i):m(i+1))+sepPosition(i));
end
end
QDHE_img = zeros(R,C);
for i = 1: R
for j = 1: C
QDHE_img(i,j) = map(WAAD_img(i,j));
end
end
QDHE_img = uint8(QDHE_img);
figure, imshow(QDHE_img)

Accepted Answer

Walter Roberson
Walter Roberson on 13 Feb 2014
QDHE_img(i,j) = map(1 + int16(WAAD_img(i,j)));

More Answers (1)

DHIVYA BHARKAVI
DHIVYA BHARKAVI on 19 Dec 2017
Sir, Please help to my project your Quandrant Dynamic histogram Equalization in Version Matlab 2013a
  1 Comment
DGM
DGM on 27 Nov 2023
Edited: DGM on 27 Nov 2023
The attached code (and the identical code in the other redundant questions-as-answers that have been deleted) is basically the same as OP's code with Walter's fix included. It runs on R2009b and R2015b just fine so long as the input image depth is appropriate, and I see no reason it shouldn't be the same in R2013a.
Was the code throwing an error because the input to rgb2gray() was already single channel? Probably. Certainly, cameraman.tif would be. The call to rgb2gray() should only be done if the image is RGB.
% any practical use of rgb2gray() requires extra code
if size(WAAD_img,3) == 3
WAAD_img = rgb2gray(WAAD_img);
elseif size(WAAD_img,3) ~= 1
error('this image is neither RGB nor I')
end
Was the code throwing an error because IPT wasn't installed? Maybe. rgb2gray() and imshow() would still be in IPT at the time.
Was it something else? This is why you need to actually describe the problem. "Please help" isn't effective at troubleshooting in a timely manner.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!