From sources-request at octave dot org Thu Mar 24 14:05:17 2005 Subject: image filtering/"revealing" function.. From: "D Goel" To: octave-sources at bevo dot che dot wisc dot edu Date: Thu, 24 Mar 2005 14:53:41 -0500 --=-=-= I am having fun with images for the first time since yesterday.. so, please pardon me if this is an already well-known/uniquitous functionality.. Attached is a file, which focuses on an area of interest in an image, filters the average color out from that area, and turns up the contrast, thereby possibly revealing details not visible before; or improving faint images. The use is perhaps best illustrated by trying this: imfiltermy ("submerged-plant.jpg",[.2 .3 .1 .2]) .. where submerged-plant.jpg is here: http://gnufans.net/~deego/pub/octave/mine/dev/submerged-plant.jpg ## ## ## Imfilter enhances section of an image by removing the common ## background color in the section of interest, and increasing ## contrast in the the section, is both for interactive use and use ## in programs. A typical interactive session proceeds as follows: ## ## imfiltermy("subblue.jpg"); ## ## This loads the file, filters a default section and shows the ## unfiltered and filtered images. Next, based on that visual ## feedback, you will probably want to adjust the default selection ## and call the function again. So, you will write, something like: ## ## imfiltermy(0,[.4 .6 .3 .4]) ## ## 0 or "" as the first argument tells the function not to bother ## reloading, but to use the currently loaded image. ## ## showp is 1 by default, to enable easy interactive action. ## ## For example, look at the example file subblue.jpg provided ## here.Try, ## ## imfiltermy ("subblue,jpg",[.2 .75 .35 .7]). ## ## Not only is the wingspan much clearer, but you can see more detail. ## Maximize the window, and you will see a rectangular structure below ## and to the right of the wingspan, which was not visible before. ## ## ## For another fun example, try ## ## imfiltermy("subplants.jpg") ## ##and imfiltermy("subplants.jpg",[0 1 .6 1]) ## ## MOTIVATION: Imagine a submerged submarine's image, all covered in ## blue water. The water is so blue that the ship behind are not too ## visible. If only the blue could be filtered out and the contrast ## turned up on the remaining image. This is precisely what this ## function does. Moreover, we can supply the xy coordinates of the ## portion of image we are interested in filtering. ## ## xyxy, if present, is of the form [x1 x2 y1 y2], ## where each value lies between 0 and 1, and tells us what part of ## the image to focus on. If xyxy is not supplied, we perform a sample ## .4 .6 ... in the center.. If xyxy == "all", we take it as 0 1 0 1. ## This will successfully work only if the ENTIRE selected area is ## more-or less of uniform color, which is then averaged and filtered ## out. ## ## ## The first argument FILE is the path to an image. Alternatively, ## this can be a cell containing r,g, and b. This argument can also ## be 0, in which case, we use the last used image. ## ## The second argument is of the form [x x y y], each value within 0 ## and 1, which tells what rectangular part of the image to focus on. ## The average color to be filtered out from the image is then taken ## to be the average of this rectangular selection. If this argument ## is missing, or == "", we use a default selection in the middle. If ## this argument =="all", we use the entire image, corresponsing to [0 ## 1 0 1]. ## ## If the next argument showp is nonzero, we display the final image, ## and also the initial image if the initial image was loaded from a ## file. ## ## This function returns the r,g,b matrices of the new image in a ## cell. ## ## ## If the final argument, adjparams is supplied and not == "", then it ## is a cell of 3 parameters. Each of those parameters is itself a ## list (cell) of arguments to be supplied to the imadjust function. ## Thus, for example, if you wanted a very white image, you might try: ## ## imfiltermy("subplants.jpg","","",{{[.8; 1]}, {[.8; 1]}, {[.8; ## 1]}}); ## ## xxyy can also be a number between 0 and 1. In that case, it refers ## to the size of the inbound box relative to 1. ## ## Thus, to quickly see the entire last figure filtered, you can ## simply type imfiltermy(0,1); In this sense, this function simply ## increases the contrast of the figure to the maximum sensible value. ## To see how this can improve picture/photo contrasts in general, ## try: imfiltermy ("submerged-plant.jpg",1) ## ## Keywords: enhance image, zoom, filter out, reveal hidden image, ## reveal detail, faint. DG http://gnufans.net/ -- --=-=-= Content-Type: application/octet-stream Content-Disposition: attachment; filename=imfiltermy.m function [rngnbn]=imfiltermy(file,xxyy,showp,adjparams) ## Copyright (C) 2005 and onwards D. Goel ## ## This file is NOT (yet) part of Octave. ## ## This is free software; you can redistribute it and/or modify it ## under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2, or (at your option) ## any later version. ## ## This software is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ## General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with Octave; see the file COPYING. If not, write to the Free ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA ## 02111-1307, USA. ## ## ## Author: D. Goel Created: 24 Mar 2005 ## Adapted-By: ## ##function [rngnbn]=imfiltermy(file,xxyy,showp,adjparams) ## Like imadjust, but works for sections of images too. ## ## The use is perhaps best illustrated by trying this: imfiltermy ## ("submerged-plant.jpg",[.2 .3 .1 .2]) .. where submerged-plant.jpg ## is here: ## http://gnufans.net/~deego/pub/octave/mine/dev/submerged-plant.jpg ## .. can help *reveal faint or invisible images*. ## ## ## Imfilter enhances section of an image by removing the common ## background color in the section of interest, and increasing ## contrast in the the section, is both for interactive use and use ## in programs. A typical interactive session proceeds as follows: ## ## imfiltermy("subblue.jpg"); ## ## This loads the file, filters a default section and shows the ## unfiltered and filtered images. Next, based on that visual ## feedback, you will probably want to adjust the default selection ## and call the function again. So, you will write, something like: ## ## imfiltermy(0,[.4 .6 .3 .4]) ## ## 0 or "" as the first argument tells the function not to bother ## reloading, but to use the currently loaded image. ## ## showp is 1 by default, to enable easy interactive action. ## ## For example, look at the example file subblue.jpg provided ## here.Try, ## ## imfiltermy ("subblue,jpg",[.2 .75 .35 .7]). ## ## Not only is the wingspan much clearer, but you can see more detail. ## Maximize the window, and you will see a rectangular structure below ## and to the right of the wingspan, which was not visible before. ## ## ## For another fun example, try ## ## imfiltermy("subplants.jpg") ## ##and imfiltermy("subplants.jpg",[0 1 .6 1]) ## ## MOTIVATION: Imagine a submerged submarine's image, all covered in ## blue water. The water is so blue that the ship behind are not too ## visible. If only the blue could be filtered out and the contrast ## turned up on the remaining image. This is precisely what this ## function does. Moreover, we can supply the xy coordinates of the ## portion of image we are interested in filtering. ## ## xyxy, if present, is of the form [x1 x2 y1 y2], ## where each value lies between 0 and 1, and tells us what part of ## the image to focus on. If xyxy is not supplied, we perform a sample ## .4 .6 ... in the center.. If xyxy == "all", we take it as 0 1 0 1. ## This will successfully work only if the ENTIRE selected area is ## more-or less of uniform color, which is then averaged and filtered ## out. ## ## ## The first argument FILE is the path to an image. Alternatively, ## this can be a cell containing r,g, and b. This argument can also ## be 0, in which case, we use the last used image. ## ## The second argument is of the form [x x y y], each value within 0 ## and 1, which tells what rectangular part of the image to focus on. ## The average color to be filtered out from the image is then taken ## to be the average of this rectangular selection. If this argument ## is missing, or == "", we use a default selection in the middle. If ## this argument =="all", we use the entire image, corresponsing to [0 ## 1 0 1]. ## ## If the next argument showp is nonzero, we display the final image, ## and also the initial image if the initial image was loaded from a ## file. ## ## This function returns the r,g,b matrices of the new image in a ## cell. ## ## ## If the final argument, adjparams is supplied and not == "", then it ## is a cell of 3 parameters. Each of those parameters is itself a ## list (cell) of arguments to be supplied to the imadjust function. ## Thus, for example, if you wanted a very white image, you might try: ## ## imfiltermy("subplants.jpg","","",{{[.8; 1]}, {[.8; 1]}, {[.8; ## 1]}}); ## ## xxyy can also be a number between 0 and 1. In that case, it refers ## to the size of the inbound box relative to 1. ## ## Thus, to quickly see the entire last figure filtered, you can ## simply type imfiltermy(0,1); In this sense, this function simply ## increases the contrast of the figure to the maximum sensible value. ## To see how this can improve picture/photo contrasts in general, ## try: imfiltermy ("submerged-plant.jpg",1) ## ## Keywords: enhance image, zoom, filter out, reveal hidden image, ## reveal detail, faint. persistent r; persistent g; persistent b; filep=0; if nargin<1; file=0; endif if nargin<2; xxyy=""; endif if strcmp(xxyy,""); xxyy=[.45 .55 .45 .55]; elseif strcmp(xxyy,"all"); xxyy=[0 1 0 1]; elseif size(xxyy)==1; minmin=.5-xxyy/2; maxmax=.5+xxyy/2; xxyy=[minmin maxmax minmin maxmax]; endif if nargin<3; showp=""; endif if strcmp(showp,""); showp=1; endif if isstr(file)¬(strcmp(file,"")); filep=1; [r g b]=imread(file); elseif iscell(file) r=file{1}; g=file{2}; b=file{3}; else "ok"; ## persistent endif ## by now, we have an image in memory.. cachedp=1; if showp&filep; disp("Displaying original image.."); imshow(r,g,b); disp("Displaying original image..done"); endif xlen=columns(r); ylen=rows(r); xmin=ceil(xlen*xxyy(1)); xmax=ceil(xlen*xxyy(2)); if xmin==0; xmin++; endif if xmin==xmax; xmin=xmax-1; endif ymin=ceil(ylen*xxyy(3)); ymax=ceil(ylen*xxyy(4)); if ymin==0; ymin++; endif if ymin==ymax; ymin=ymax-1; endif ## r of interest: rint=r(ymin:ymax,xmin:xmax); gint=g(ymin:ymax,xmin:xmax); bint=b(ymin:ymax,xmin:xmax); # if nargin<4; # b0=mean(bint(:)); # g0=mean(gint(:)); # r0=mean(rint(:)); # else # r0=r0g0b0(1); # g0=r0g0b0(2); # b0=r0g0b0(3); # endif norml=255; if nargin<4; adjparams="" endif if not(iscell(adjparams)); rn1=norml*imadjust(rint); gn1=norml*imadjust(gint); bn1=norml*imadjust(bint); else adj=adjparams(:); lenad=length(adj{1}); lenad1=1+lenad; rb1=gb1=bb1=cell(lenad1,1); rb1{1}=rint; gb1{1}=gint; bb1{1}=bint; ## note round brackets! rb1(2:lenad1)=adj{1}; gb1(2:lenad1)=adj{2}; rb1(2:lenad1)=adj{3}; ## disp(rb1) ##"dfdffdf" ##pause rn1=norml*leval('imadjust',rb1); bn1=norml*leval('imadjust',bb1); gn1=norml*leval('imadjust',gb1); endif rn=r; gn=g; bn=b; rn(ymin:ymax,xmin:xmax)=rn1; gn(ymin:ymax,xmin:xmax)=gn1; bn(ymin:ymax,xmin:xmax)=bn1; if showp disp("Displaying final image.."); imshow(rn,gn,bn); disp("Displaying final image..done"); endif if nargout>0; rngnbn={rn,gn,bn}; endif endfunction --=-=-=--