May I please ask what's the error of line4 ?

4 Ansichten (letzte 30 Tage)
Julie
Julie am 23 Mär. 2023
Bearbeitet: Julie am 27 Mär. 2023
So I am trying to use basic auto contrast to enhance an image, I used imread and imshow functions to display the image, I named it imE,
I used imF as the enhanced image, I am going to attach two sets of codes of how did I use the formulas to create an auto-contrast function, unfortunately, no matter which functions (shorter or longer) I am using, it keeps telling me the line 4 is wrong. I don't know what the reason caused it is and how to fix it, if anyone knows, please share your opinions and I will appreciate it.
Set code of 1:
imE = imread('peppers.png');
imshow(imE)
imF = AutoContrast(imE);
imshow(imF)
imF = BasicAutoContrast(imE);
imshow(imF)
function [imF] = AutoContrast(imE)
% Basic auto-contrast algorithm.
imE = double(imE); %------------ line 4, I can't run the function due to line 4 has errors
% define V1 & Vh
Vl = 0;
Vh = 255;
% find Vd & Vb
Vd = min(imE(:));
Vb = max(imE(:));
imF = uint8((imE - Vd+1) .* ((Vh - Vl +1)/ (Vb-Vd)) -1);
end
set of code 2:
function [imF] = BasicAutoContrast(imE)
% Basic auto-contrast algorithm.
imF = zeros(size(imE));
imE = double(imE); %-------- line4 has errors
%define V1 & Vh
V1 = 0;
Vh = 255;
%find Vd & Vb
Vd = min(imE(:));
Vb = max(imE(:));
%Mapping Vi to Vn
for Vi = Vd:Vb
Vn = (Vi-Vd+1) .* (Vh-V1+1 )/(Vb-Vd+1) - 1;
idx = (imE == Vi);
imF(idx) = Vn;
end
imF = uint8(imF);
end
The all of the red lines on the command window:
>> AutoContrast
Not enough input arguments.
Error in AutoContrast (line 4)
imE = double(imE);
>> AutoContrast
Not enough input arguments.
Error in AutoContrast (line 4)
imE = double(imE);
That's the image that I uploaded in MATLAB
  14 Kommentare
Julie
Julie am 24 Mär. 2023
I don't know why this page doesn't allow me to reply any messages, I updated some information that you guys suggested.
DGM
DGM am 24 Mär. 2023
Bearbeitet: DGM am 24 Mär. 2023
There really isn't much point in using cast() dependent on the input class, since it will still only work if the input is uint8.
You'd actually need to conditionally scale the image.
function [outpict] = AutoContrast(inpict)
outclass = class(inpict);
outrange = getrangefromclass(inpict);
outpict = mat2gray(inpict); % normalize
outpict = rescale(outpict,outrange(1),outrange(2)); % rescale
outpict = cast(outpict,outclass);
end
This is why MIMT imcast() exists.
Of course, I'm assuming that the point of writing these functions was to do the math the hard way, otherwise we might as well just say
outpict = imadjust(inpict,stretchlim(inpict,0));
and avoid the need to write the functions at all.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

DGM
DGM am 24 Mär. 2023
Bearbeitet: DGM am 24 Mär. 2023
You need to pass the image as an input argument when you call your function -- just as is done in the question you posted. The error occurs on that specific line only because it's the first line where the missing input is required.
To demonstrate again how the function is called:
% read an image
inpict = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1335254/image.png');
% adjust the image
outpict = AutoContrast(inpict); % pass the image as an input, get an image as an output
% test the result
[mn mx] = bounds(outpict,'all') % output scaling is off by 1
mn = uint8 1
mx = uint8 255
function [imF] = AutoContrast(imE)
% Basic auto-contrast algorithm.
imE = double(imE);
% define V1 & Vh
Vl = 0;
Vh = 255;
% find Vd & Vb
Vd = min(imE(:));
Vb = max(imE(:));
% this line needs to be fixed
% see my comments above
imF = uint8((imE - Vd+1) .* ((Vh - Vl +1)/ (Vb-Vd)) -1);
end
See the suggestions posted in the comments if you want the output scaling to be correct and consistent.
  10 Kommentare
DGM
DGM am 24 Mär. 2023
Bearbeitet: DGM am 24 Mär. 2023
The answer I posted includes a script which reads the image file, calls the function and stores its output, and then checks the correctness of the result.
Here is another script which repeats the task, but with correct results.
% read an image
inpict = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1335254/image.png');
% adjust the image
outpict = AutoContrast(inpict); % pass the image as an input, get an image as an output
% test the result
[mn mx] = bounds(outpict,'all') % output scaling is correct
mn = uint8 0
mx = uint8 255
% compare the two images visually
imshow([inpict outpict])
function [outpict] = AutoContrast(inpict)
% Basic auto-contrast algorithm.
% presume all images are uint8
outpict = double(inpict);
% define OUTPUT LEVELS
Vol = 0;
Voh = 255;
% find INPUT LEVELS
Vil = min(outpict(:));
Vih = max(outpict(:));
% adjust image
outpict = Vol + (Voh-Vol)*(outpict - Vil)/(Vih - Vil);
% ^---rescale---^ ^--------normalize--------^
% cast back to presumed input class
outpict = uint8(outpict);
end
Mind you, this is not a thorough test of correctness. I'm only performing a test sufficient to reveal that the original math was not producing the specified output levels. If you wanted to test the output more thoroughly, you wouldn't bother feeding it an image; you'd just feed it a test ramp.
DGM
DGM am 24 Mär. 2023
Regarding testing. I'm going to keep this simple. The function files are attached for sake of compactness.
% create a simple linear ramp between two random values in uint8
inlevels = sort([randi([0 192],1,1) ...
randi([64 255],1,1)]);
inpict = uint8(inlevels(1):inlevels(2));
% process the test image
outpict1 = imadjust(inpict,stretchlim(inpict,0)); % known good
outpict2 = AutoContrastBad(inpict); % original code
outpict3 = AutoContrast(inpict); % corrected code
% is the output range [0 255]?
outrange1 = imrange(outpict1) % YES
outrange1 = 1×2
0 255
outrange2 = imrange(outpict2) % NO
outrange2 = 1×2
5 255
outrange3 = imrange(outpict3) % YES
outrange3 = 1×2
0 255
% compare the values against the known good
err13 = immse(outpict1,outpict3) % corrected code matches imadjust() exactly
err13 = 0
err12 = immse(outpict1,outpict2) % original code only occasionally matches
err12 = 26.4565
Note that I was wrong. It's not spuriously off by 1. It's off by a variable amount that's dependent on the input range.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Image Processing Toolbox finden Sie in Help Center und File Exchange

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by