필터 지우기
필터 지우기

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

조회 수: 3 (최근 30일)
Julie
Julie 2023년 3월 23일
편집: Julie 2023년 3월 27일
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
Julie
Julie 2023년 3월 24일
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 2023년 3월 24일
편집: DGM 2023년 3월 24일
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.

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

답변 (1개)

DGM
DGM 2023년 3월 24일
편집: DGM 2023년 3월 24일
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
DGM
DGM 2023년 3월 24일
편집: DGM 2023년 3월 24일
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 2023년 3월 24일
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.

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

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by