error in my modified auto-contrast function
3 ビュー (過去 30 日間)
古いコメントを表示
Hello guys, I have a project about contrast in image processing and i have to do modified auto-contrast. Here is my code:
function y = modified_autocontrast(a)
a = double(a);
[m, n] = size(a);
a_min = 0;
a_max = 255;
q_low = 0.005;
q_high = 0.005;
n = 256;
count = zeros(1,n);
v = zeros(1,n);
inc = 1;
for p = 0:255
for k = 1:m
for l = 1:n
if(a(k,l) == p)
count(p+1)=count(p+1)+inc;
end
end
end
end
for p = 0:255
if(count(p+1) >= (m*n*q_low))
for c = 0:255
v(c) = count(p+1);
end
end
end
a_low = min(v);
for p = 0:255
if(count(p+1) <= (m*n*(1-q_high)))
for c = 0:255
v(c) = count(p+1);
end
end
end
a_high = max(v);
for i = 1:m
for j = 1:n
if (a <= a_low)
y(i,j) = a_min;
elseif (a_low < a && a < a_high)
y(i, j) = a_min + (a - a_low) * ((a_max - a_min)/(a_high - a_low));
elseif(a >= a_high)
y(i,j) = a_max;
end
end
end
I have an error at this line: v(c) = count(p+1);
How can I fix it? I am new in matlab, help me please
0 件のコメント
回答 (1 件)
Dave B
2021 年 8 月 10 日
編集済み: Dave B
2021 年 8 月 10 日
Your bug: c starts at 0, matlab indexing is one based, you're referring to c(0) but that's not allowed.
Consider: this is a very not MATLAB (and probably slow) approach. Can you do this in a matrix focussed way instead of with loops?
e.g. if the goal of your first loop is to get counts for each value, maybe histcounts would serve you better? Or tabulate (statistics and ML toolbox)? or even don't cast to double and use imhist (image processing toolbox)?
2 件のコメント
Dave B
2021 年 8 月 10 日
編集済み: Dave B
2021 年 8 月 10 日
What is the error?
- I don't see how y is initialized in your code.
- a is a matrix and a_low is a scalar...a <= a_low is almost certainly not what you're looking for.
Again the loops are just making your code slow and bug-prone,
data = rand(10);
themin=0; themax=100;
data(data <= 0.2) = themin;
data(data >= 0.8) = themax;
datatoscale(data>0.2 & data <0.8) = themin + (data(data>0.2 & data <0.8) - 0.2) * ((themax-themin)/(0.8 - 0.2));
Or maybe what you're looking for is just:
rescale(data, themin, themax, 'InputMin', .2, 'InputMax', .8);
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!