From bug-octave-request at bevo dot che dot wisc dot edu Sat Apr 20 13:26:48 2002 Subject: rgb2hsv patch From: Paul Kienzle To: bug-octave at bevo dot che dot wisc dot edu Date: Sat, 20 Apr 2002 14:26:19 -0400 octave:3> rgb2hsv([0 0 0]) warning: division by zero warning: in /home/pkienzle/cvs/octave/scripts/image/rgb2hsv.m near line 48, column 5: >>> s = (v' > 0) .* (1 - min (rgb') ./ v)'; ans = 0 0 0 The following patch removes this. Note that the code would be cleaner if rgb(condition,idx) would work even if condition is always false. Paul Kienzle * image/rgb2hsv.m: faster, more accurate, remove the divide by zero warning. Index: rgb2hsv.m =================================================================== RCS file: /cvs/octave/scripts/image/rgb2hsv.m,v retrieving revision 1.1 diff -c -r1.1 rgb2hsv.m *** rgb2hsv.m 2001/02/28 08:24:43 1.1 --- rgb2hsv.m 2002/04/20 18:18:31 *************** *** 43,90 **** error ("rgb2hsv: argument must be a matrix of size n x 3"); endif ! # get saturation and value ! v = max (rgb'); ! s = (v' > 0) .* (1 .- min (rgb') ./ v)'; ! # if v==0 set s to 0 too ! s(isnan (s)) = 0; ! ! # subtract minimum and divide trough maximum ! # to get the bright and saturated colors ! sc = (rgb - kron ([1, 1, 1], min (rgb')')); ! sv = sc ./ kron([1, 1, 1], max (sc')'); ! ! # if r=g=b (gray value) set hue to 0 ! sv(isnan (sv)) = 0; ! ! # hue=f(color) must be splitted into 6 parts ! # 2 for each color ! ! # h1(green) ! tmp = (sv(:, 1) == 1 & sv(:,3) == 0) .* (1/6 * sv(:,2) + eps); ! # avoid problems with h2(red) since hue(0)==hue(1) ! h = (tmp < 1/6) .* tmp; ! # h2(green) ! h = h + ((h == 0) & sv(:,1) == 0 & sv(:,3) == 1) \ ! .* (-1/6 * sv(:,2) + 2/3 + eps); ! ! # h1(red) ! h = h + ((h == 0) & sv(:,2) == 1 & sv(:,3) == 0) \ ! .* (-1/6 * sv(:,1) + 1/3 + eps); ! ! # h2(red) ! h = h + ((h == 0) & sv(:,2) == 0 & sv(:,3) == 1) \ ! .* (1/6 * sv(:,1) + 2/3 + eps); ! ! # h1(blue) ! h = h + ((h == 0) & sv(:,1) == 1 & sv(:,2) == 0) \ ! .* (-1/6 * sv(:,3) + 1 + eps); ! ! # h2(blue) ! h = h + ((h == 0) & sv(:,1) == 0 & sv(:,2) == 1) \ ! .* (1/6 * sv(:,3) + 1/3); ! ! hsval = [h, s, v']; endfunction --- 43,82 ---- error ("rgb2hsv: argument must be a matrix of size n x 3"); endif ! ## get the max and min ! s = min (rgb')'; ! v = max (rgb')'; ! ! ## set hue to zero for undefined values (gray has no hue) ! h = zeros(size(v)); ! notgray = (s != v); ! ! ## blue hue ! idx = (v == rgb(:,3) & notgray); ! if any(idx) ! h(idx) = 2/3 + 1/6 * (rgb(idx,1)-rgb(idx,2)) ./ (v(idx) - s(idx)); ! endif ! ! ## green hue ! idx = (v == rgb(:,2) & notgray); ! if any(idx) ! h(idx) = 1/3 + 1/6 * (rgb(idx,3)-rgb(idx,1)) ./ (v(idx) - s(idx)); ! endif ! ! ## red hue ! idx = (v == rgb(:,1) & notgray); ! if any(idx) ! h(idx) = 1/6 * (rgb(idx,2)-rgb(idx,3)) ./ (v(idx) - s(idx)); ! endif ! ! ## correct for negative red ! idx = (h < 0); ! h(idx) = 1+h(idx); ! ! ## set the saturation ! s(!notgray) = 0; ! s(notgray) = 1 - s(notgray) ./ v(notgray); ! hsval = [h, s, v]; endfunction ----- End forwarded message ----- ------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.octave.org How to fund new projects: http://www.octave.org/funding.html Subscription information: http://www.octave.org/archive.html -------------------------------------------------------------