Leptonica 1.68
C Image Processing Library
|
00001 /*====================================================================* 00002 - Copyright (C) 2001 Leptonica. All rights reserved. 00003 - This software is distributed in the hope that it will be 00004 - useful, but with NO WARRANTY OF ANY KIND. 00005 - No author or distributor accepts responsibility to anyone for the 00006 - consequences of using this software, or for whether it serves any 00007 - particular purpose or works at all, unless he or she says so in 00008 - writing. Everyone is granted permission to copy, modify and 00009 - redistribute this source code, for commercial or non-commercial 00010 - purposes, with the following restrictions: (1) the origin of this 00011 - source code must not be misrepresented; (2) modified versions must 00012 - be plainly marked as such; and (3) this notice may not be removed 00013 - or altered from any source or modified source distribution. 00014 *====================================================================*/ 00015 00016 /* 00017 * colormorph.c 00018 * 00019 * Top-level color morphological operations 00020 * 00021 * PIX *pixColorMorph() 00022 * 00023 * Method: Algorithm by van Herk and Gil and Werman, 1992 00024 * Apply grayscale morphological operations separately 00025 * to each component. 00026 */ 00027 00028 #include <stdio.h> 00029 #include <stdlib.h> 00030 #include "allheaders.h" 00031 00032 00033 /*-----------------------------------------------------------------* 00034 * Top-level color morphological operations * 00035 *-----------------------------------------------------------------*/ 00036 /*! 00037 * pixColorMorph() 00038 * 00039 * Input: pixs 00040 * type (L_MORPH_DILATE, L_MORPH_ERODE, L_MORPH_OPEN, 00041 * or L_MORPH_CLOSE) 00042 * hsize (of Sel; must be odd; origin implicitly in center) 00043 * vsize (ditto) 00044 * Return: pixd 00045 * 00046 * Notes: 00047 * (1) This does the morph operation on each component separately, 00048 * and recombines the result. 00049 * (2) Sel is a brick with all elements being hits. 00050 * (3) If hsize = vsize = 1, just returns a copy. 00051 */ 00052 PIX * 00053 pixColorMorph(PIX *pixs, 00054 l_int32 type, 00055 l_int32 hsize, 00056 l_int32 vsize) 00057 { 00058 PIX *pixr, *pixg, *pixb, *pixrm, *pixgm, *pixbm, *pixd; 00059 00060 PROCNAME("pixColorMorph"); 00061 00062 if (!pixs) 00063 return (PIX *)ERROR_PTR("pixs not defined", procName, NULL); 00064 if (pixGetDepth(pixs) != 32) 00065 return (PIX *)ERROR_PTR("pixs not 32 bpp", procName, NULL); 00066 if (type != L_MORPH_DILATE && type != L_MORPH_ERODE && 00067 type != L_MORPH_OPEN && type != L_MORPH_CLOSE) 00068 return (PIX *)ERROR_PTR("invalid morph type", procName, NULL); 00069 if (hsize < 1 || vsize < 1) 00070 return (PIX *)ERROR_PTR("hsize or vsize < 1", procName, NULL); 00071 if ((hsize & 1) == 0 ) { 00072 L_WARNING("horiz sel size must be odd; increasing by 1", procName); 00073 hsize++; 00074 } 00075 if ((vsize & 1) == 0 ) { 00076 L_WARNING("vert sel size must be odd; increasing by 1", procName); 00077 vsize++; 00078 } 00079 00080 if (hsize == 1 && vsize == 1) 00081 return pixCopy(NULL, pixs); 00082 00083 pixr = pixGetRGBComponent(pixs, COLOR_RED); 00084 pixg = pixGetRGBComponent(pixs, COLOR_GREEN); 00085 pixb = pixGetRGBComponent(pixs, COLOR_BLUE); 00086 if (type == L_MORPH_DILATE) { 00087 pixrm = pixDilateGray(pixr, hsize, vsize); 00088 pixgm = pixDilateGray(pixg, hsize, vsize); 00089 pixbm = pixDilateGray(pixb, hsize, vsize); 00090 } 00091 else if (type == L_MORPH_ERODE) { 00092 pixrm = pixErodeGray(pixr, hsize, vsize); 00093 pixgm = pixErodeGray(pixg, hsize, vsize); 00094 pixbm = pixErodeGray(pixb, hsize, vsize); 00095 } 00096 else if (type == L_MORPH_OPEN) { 00097 pixrm = pixOpenGray(pixr, hsize, vsize); 00098 pixgm = pixOpenGray(pixg, hsize, vsize); 00099 pixbm = pixOpenGray(pixb, hsize, vsize); 00100 } 00101 else { /* type == L_MORPH_CLOSE */ 00102 pixrm = pixCloseGray(pixr, hsize, vsize); 00103 pixgm = pixCloseGray(pixg, hsize, vsize); 00104 pixbm = pixCloseGray(pixb, hsize, vsize); 00105 } 00106 pixd = pixCreateRGBImage(pixrm, pixgm, pixbm); 00107 pixDestroy(&pixr); 00108 pixDestroy(&pixrm); 00109 pixDestroy(&pixg); 00110 pixDestroy(&pixgm); 00111 pixDestroy(&pixb); 00112 pixDestroy(&pixbm); 00113 00114 return pixd; 00115 } 00116 00117 00118