home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The World of Computer Software
/
World_Of_Computer_Software-02-385-Vol-1of3.iso
/
m
/
morpho.zip
/
morpho
/
src
/
vgmclose
/
vgmclose.prog
< prev
Wrap
Text File
|
1992-07-26
|
10KB
|
295 lines
-authors
Pascal ADAM and Greg DONOHOE
-authors_end
-short_prog_description
Close a greyscale image X, by a structuring element B.
-short_prog_description_end
-short_lib_description
Close a greyscale image X, by a structuring element B.
-short_lib_description_end
-man1_long_description
.I vgmclose
computes the Closing of an greyscale image by a structuring element.
.sp 2
This algorithms is taken from Digital Image Processing by R.C. Gonzalez and
R.E. Woods, Addison-Wesley, 1992. It Dilates X by B,
then Erodes the result by B.
The result is clipped to the range [0-255]. Valid values of B(x,y)
are in the range [0 - 255]. Pixels in B that are not part of the domain
of B should be negative. For this reason, the structuring element
is of type 'int'.
-man1_long_description_end
-man1_examples
vgmclose -i image1 -k kernel -o image2
.LP
Computes the closeing of an image X (intput -i) by a structuring
element B (input -k) and stores the result in the output image ( -o).
-man1_examples_end
-man1_restrictions
\fIvgmclose\fP accepts only \fIBYTE\fP images, and the kernel must
be type \fIINT\fP.
-man1_restrictions_end
-man1_see_also
vmcustom(1), lvmcustom(3)
-man1_see_also_end
-man3_long_description
.I lvgmclose
computes the Closing of an greyscale image by a structuring element.
.sp 2
This algorithms is taken from Digital Image Processing by R.C. Gonzalez and
R.E. Woods, Addison-Wesley, 1992. Closing is defined as a
Dilation followed by an Erosion.
The result is clipped to the range [0-255]. Valid values of B(x,y)
are in the range [0 - 255]. Pixels in B that are not part of the domain
of B should be negative. For this reason, the structuring element
is of type 'int'.
-man3_long_description_end
-man3_restrictions
.LP
\fIvgmclose\fP accepts only \fIBYTE\fP images, and the kernel must
be type\fIINT\fP.
-man3_restrictions_end
-man3_see_also
vgmclose(1)
-man3_see_also_end
-usage_additions
-usage_additions_end
-include_includes
-include_includes_end
-include_additions
-include_additions_end
-include_macros
#define READINPUT(image,kernel) \
image = readimage(vgmclose->i_file); \
if (image == NULL) { \
(void) fprintf(stderr, "vgmclose: Can not read input image \n"); \
exit(1); /* Quit if bad image */ \
} \
kernel = readimage(vgmclose->k_file); \
if (kernel == NULL) { \
(void) fprintf(stderr, "vgmclose: Can not read input kernel\n"); \
exit(1); /* Quit if bad kernel */ \
}
#define CHECKINPUT(program, image,kernel) \
propertype(program,image,VFF_TYP_1_BYTE,TRUE); \
proper_num_images(program,image,1,TRUE); \
proper_num_bands(program,image,1,TRUE); \
proper_map_enable(program,image,VFF_MAP_OPTIONAL,TRUE); \
propertype(program,kernel,VFF_TYP_4_BYTE,TRUE) ; \
proper_num_images(program,kernel,1,TRUE); \
proper_num_bands(program,kernel,1,TRUE); \
proper_map_enable(program,kernel,VFF_MAP_OPTIONAL,TRUE); \
-include_macros_end
-main_variable_list
struct xvimage *image, *kernel,*readimage();
-main_variable_list_end
-main_before_lib_call
if (check_args()) exit(1);
READINPUT (image, kernel);
CHECKINPUT (program, image, kernel);
-main_before_lib_call_end
-main_library_call
if(! lvgmclose(image,kernel))
{
(void) fprintf(stderr, "vgmclose: lvgmclose Failed\n");
exit(1);
}
-main_library_call_end
-main_after_lib_call
writeimage(vgmclose->o_file,image);
-main_after_lib_call_end
-library_includes
-library_includes_end
-library_input
.IP "img" 15
xvimage structure, must be a byte image.
.IP "ker" 15
xvimage structure, morphological kernel or structuring element.
Should have been created by vmcustom.
-library_input_end
-library_output
.IP "img2" 15
xvimage structure, closeing of img1 by kernel ker.
-library_output_end
-library_def
int
lvgmclose(image,kernel)
struct xvimage *image, *kernel;
-library_def_end
-library_code
{
int belong, /* TRAN(IMG2;i,j) belongs to IMG1 ? */
nc, /* number of columns of image */
nr, /* " " rows " " */
starti, /* x upper left corner position of kernel */
startj, /* y " " " " " " */
kernc, /* number of columns of kernel */
kernr; /* number of rows of kernel */
unsigned char *result; /* storage of final result */
char *program = "lvgmclose";
int i,j,k,ii,jj,iii,jjj;
register int minpix, maxpix, imagepix, kernpix;
unsigned char *c1, *c3;
int *c2;
/* Check type,... of image */
if (!(propertype(program, image, VFF_TYP_1_BYTE, FALSE))) {
(void) fprintf (stderr, "\n\n%s: ", program);
(void) fprintf (stderr, "lvgmclose: image must be of type byte\n");
return (0);
}
if (!(proper_num_images (program, image, 1, FALSE))) {
(void) fprintf (stderr, "\n\n%s: ", program);
(void) fprintf (stderr, "Can only work on files with one image\n\n");
return (0);
}
if (!(proper_num_bands (program, image, 1, FALSE))) {
(void) fprintf (stderr,"\n\n%s: ", program);
(void) fprintf (stderr,"Can only work on images with 1 data band\n\n");
return (0);
}
nc = image->row_size; /* number of columns */
nr = image->col_size; /* number of rows */
c1 = (unsigned char *)(image->imagedata); /* pointer on image's pixels */
/* Check type,... of image */
if (!(propertype(program, kernel, VFF_TYP_4_BYTE, FALSE))) {
(void) fprintf (stderr,"\n\n%s: ", program);
(void) fprintf (stderr,"lvgmclose: kernel must be of type int/n");
return (0);
}
if (!(proper_num_images (program, kernel, 1, FALSE))) {
(void) fprintf (stderr,"\n\n%s: ", program);
(void) fprintf (stderr,"Can only work on files with one image\n\n");
return (0);
}
if (!(proper_num_bands (program, kernel, 1, FALSE))) {
(void) fprintf (stderr,"\n\n%s: ", program);
(void) fprintf (stderr,"Can only work on images with 1 data band\n\n");
return (0);
}
kernc = kernel->row_size;/* number of columns of the kernel */
kernr = kernel->col_size;/* number of rows of the kernel */
starti = (int)kernel->ispare1; /* position of upper left hand */
startj = (int)kernel->ispare2; /* corner of the kernel (x,y) */
c2 = (int *)(kernel->imagedata); /* pointer on kernel values */
/* Checks if the relative position of the kernel relative to the
** dimension of the image will give a significant result image */
if (starti>nr/2 || startj>nc/2) {
(void) fprintf (stderr, "\n%s: ", program);
(void) fprintf (stderr, "Warning : the size of the kernel\n");
(void) fprintf (stderr, "is too big compared to the image\n");
(void) fprintf (stderr, "size. The visible result may out\n");
(void) fprintf (stderr, "of the result image dimension...\n");
}
/* dynamic memory allocation for temporary result stockage */
result = (unsigned char *) malloc( nc * nr *sizeof(int));
if (result == NULL) {
(void) fprintf (stderr, "\n%s: ", program);
(void) fprintf (stderr, "Insufficient Space avaliable\n");
return(0);
}
c3 = result; /* pointer on the result image data */
k = nc * nr; /* total number of pixels in the input image */
/* initialisation of the resulting image */
for (j=0;j<k;j++) *(c3 +j)=0;
/* Scans and computes the final value of the result image */
/* First DILATE the image by the structuring element */
for (i=0;i<nr;i++) {
for (j=0;j<nc;j++) {
maxpix = 0;
for (ii=0; ii < kernr; ii++) {
iii = i + ii + starti;
if (iii<0 || iii>=nr) continue;
for (jj = 0; jj < kernc; jj++) {
jjj = j + jj + startj;
if (jjj>=0 && jjj<nc) {
imagepix = *(c1+iii*nc+jjj);
kernpix = *(c2+ii*kernc+jj);
if (kernpix >= 0)
maxpix = MAX(maxpix, (imagepix + kernpix));
}
}
}
maxpix = MIN(maxpix, 255);
*(c3 + i*nc + j) = (unsigned char)maxpix;
}
}
/* Now ERODE the image by the structuring element */
/* Use the input image for the result */
for (i=0;i<nr;i++) {
for (j=0;j<nc;j++) {
minpix = 255;
for (ii=0; ii < kernr; ii++) {
iii = i + ii + starti;
if (iii<0 || iii>=nr) continue;
for (jj = 0; jj < kernc; jj++) {
jjj = j + jj + startj;
if (jjj>=0 && jjj<nc) {
imagepix = *(c3+iii*nc+jjj);
kernpix = *(c2+ii*kernc+jj);
if (kernpix >= 0)
minpix = MIN(minpix, (imagepix - kernpix));
}
}
}
minpix = MAX(minpix, 0);
*(c1 + i*nc + j) = (unsigned char)minpix;
}
}
/* Now the input image containes the closing */
/* free memory location of temporary result storage */
free(result);
return(1);
}
-library_code_end
-library_mods
Has been updated to ghostX on 5/24/90 by Pascal ADAM
-library_mods_end