error in my modified auto-contrast function

3 visualizaciones (últimos 30 días)
Ionela Pastinaru
Ionela Pastinaru el 10 de Ag. de 2021
Editada: Dave B el 10 de Ag. de 2021
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

Respuestas (1)

Dave B
Dave B el 10 de Ag. de 2021
Editada: Dave B el 10 de Ag. de 2021
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 comentarios
Ionela Pastinaru
Ionela Pastinaru el 10 de Ag. de 2021
Editada: Ionela Pastinaru el 10 de Ag. de 2021
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 el 10 de Ag. de 2021
Editada: Dave B el 10 de Ag. de 2021
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);

Iniciar sesión para comentar.

Categorías

Más información sobre Parallel Computing en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by