Your problem is that you misunderstand nargin.
By way of example, I wrote this very simple function.
function week7hw1(pixels, varargin)
So all it does is write out the number of elements in varargin, as well as write out the value of nargin. Now I'll try it out.
Hmm. there were two inputs to the function.
Nargin was 2. But numel(varargin) was 1, as expected.
So, what does nargin do? It is NOT the number of elements in varargin. nargin is the TOTAL number of input arguments to the function. Similarly, if I provide no second argument, then what happens?
Again, I provided no second argument. nargin was 1, since there was ONE argument. varargin was an empty array this time.
So, why does your code fail? You test to see if nargin is ZERO or ONE. But nargin will always be at least 1, because you always are providing the first argument. So you want to distinguish if there are 1 or 2 arguments provided. The necessary fix to your code is very simple.