error in my modified auto-contrast function

조회 수: 4 (최근 30일)
Ionela Pastinaru
Ionela Pastinaru 2021년 8월 10일
편집: Dave B 2021년 8월 10일
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

답변 (1개)

Dave B
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
Ionela Pastinaru
Ionela Pastinaru 2021년 8월 10일
편집: Ionela Pastinaru 2021년 8월 10일
Thank you a lot! I put v(c + 1) and it works but now it gives me an error at: y(i,j) = a_min; I did the auto contrast function on the same principle, with loops and everything, that's why i want to be like this
Dave B
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);

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

카테고리

Help CenterFile Exchange에서 Parallel Computing에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by