hv_search.cpp

00001
00002 /***************************************************************************
00003  *  hv_search.cpp - Implementation of horizontal- and vertical-search filter
00004  *
00005  *  Created: Tue Jul 12 14:40:40 2005
00006  *  Copyright  2005-2007  Tim Niemueller [www.niemueller.de]
00007  *             2005       Yuxiao Hu (Yuxiao.Hu@rwth-aachen.de)
00008  *
00009  ****************************************************************************/
00010
00011 /*  This program is free software; you can redistribute it and/or modify
00012  *  it under the terms of the GNU General Public License as published by
00013  *  the Free Software Foundation; either version 2 of the License, or
00014  *  (at your option) any later version. A runtime exception applies to
00015  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00016  *
00017  *  This program is distributed in the hope that it will be useful,
00018  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020  *  GNU Library General Public License for more details.
00021  *
00022  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00023  */
00024
00025 #include <filters/hv_search.h>
00026
00027 #include <fvutils/color/yuv.h>
00028
00029 #include <cstddef>
00030 #include <cstring>
00031 
00032 /** @class FilterHVSearch <filters/hv_search.h>
00033  * Horizontal/vertical search filter.
00034  * This filter works similar to the horizontal search filter, but additionally
00035  * it search for color changes in vertical direction.
00036  * @author Yuxiao Hu
00037  * @author Tim Niemueller
00038  */
00039
00040 
00041 /** Constructor.
00042  * @param cm color model to use to determine the color change
00043  * @param what what to look for, this color is considered as foreground,
00044  * all other colors are background.
00045  */
00046 FilterHVSearch::FilterHVSearch(ColorModel *cm, color_t what)
00047   : Filter("FilterHVSearch")
00048 {
00049   this->cm = cm;
00050   this->what = what;
00051 }
00052
00053
00054 void
00055 FilterHVSearch::apply()
00056 {
00057   register unsigned int h = 0;
00058   register unsigned int w = 0;
00059
00060   unsigned int width = src_roi[0]->width <= dst_roi->width ? src_roi[0]->width : dst_roi->width;
00061
00062   // Here use array to avoid overhead of dynamic mem allocation.
00063   unsigned int top[width];
00064   unsigned int bottom[width];
00065   bool vflag[width];
00066
00067   // y-plane
00068   register unsigned char *yp   = src[0] + (src_roi[0]->start.y * src_roi[0]->line_step) + (src_roi[0]->start.x * src_roi[0]->pixel_step);
00069   // u-plane
00070   register unsigned char *up   = YUV422_PLANAR_U_PLANE(src[0], src_roi[0]->image_width, src_roi[0]->image_height)
00071                                    + ((src_roi[0]->start.y * src_roi[0]->line_step) / 2 + (src_roi[0]->start.x * src_roi[0]->pixel_step) / 2) ;
00072   // v-plane
00073   register unsigned char *vp   = YUV422_PLANAR_V_PLANE(src[0], src_roi[0]->image_width, src_roi[0]->image_height)
00074                                    + ((src_roi[0]->start.y * src_roi[0]->line_step) / 2 + (src_roi[0]->start.x * src_roi[0]->pixel_step) / 2);
00075
00076   // destination y-plane
00077   register unsigned char *dyp  = dst + (dst_roi->start.y * dst_roi->line_step) + (dst_roi->start.x * dst_roi->pixel_step);
00078
00079   // line starts
00080   unsigned char *lyp  = yp;   // y-plane
00081   unsigned char *lup  = up;   // u-plane
00082   unsigned char *lvp  = vp;   // v-plane
00083   unsigned char *ldyp = dyp;  // destination y-plane
00084
00085   // left and right boundary of the current line
00086   unsigned int left;
00087   unsigned int right;
00088   bool flag;
00089
00090   // Confidence threshold for a line as "what" inside.
00091   const unsigned int    MIN_INTERIA     = 9;
00092   unsigned int          num_what;
00093
00094   // Remember the widest orange line,
00095   // and if the following lines shrink dramatically,
00096   // we stop searching. This help eliminate reflextion.
00097   const unsigned int    MAX_SHRINK      = 16;
00098   unsigned int          max_width       = 0;
00099   bool                  not_reflect     = true;
00100
00101   memset(top, 0, width * sizeof(unsigned int));
00102   memset(bottom, 0, width * sizeof(unsigned int));
00103   memset(vflag, 0, width * sizeof(bool));
00104
00105   for (h = 0; (h < src_roi[0]->height) && (h < dst_roi->height); ++h) {
00106     flag = false;
00107     left = right = 0;
00108     num_what = 0;
00109     for (w = 0; (w < src_roi[0]->width) && (w < dst_roi->width); ++w) {
00110       if ( (cm->determine(*yp++, *up, *vp) == what) ) {
00111         right = w;
00112         if (not_reflect) bottom[w] = h;
00113         flag = true;
00114         vflag[w] = true;
00115         ++num_what;
00116       } else {
00117         left = flag?left:w;
00118         if (!vflag[w]) top[w] = h;
00119       }
00120       if ( (cm->determine(*yp++, *up++, *vp++) == what) ) {
00121         right = ++w;
00122         if (not_reflect) bottom[w] = h;
00123         flag = true;
00124         vflag[w] = true;
00125         ++num_what;
00126       } else {
00127         ++w;
00128         left = flag?left:w;
00129         if (!vflag[w]) top[w] = h;
00130       }
00131     }
00132
00133     // clear the dst buffer for this line
00134     memset(ldyp, 0, dst_roi->width);
00135
00136     if (num_what * MIN_INTERIA > right - left)
00137     {
00138       if (right - left > max_width)
00139         max_width = right - left;
00140       if (not_reflect)
00141       {
00142         if (right - left < max_width / MAX_SHRINK)
00143         {
00144           // cout << "In line:" << h << " \tleft = " << left
00145           //      << " \tright = " << right << " \tmax_width = "
00146           //      << max_width << endl;
00147           not_reflect = false; // the reflection begins from here
00148         }
00149
00150         // set the left- and right-most pixel to white
00151         // but if the pixel is at the boundary, we ignore it
00152         // in order to eliminate a straight line at the border.
00153         if (left != 0 && left < dst_roi->width-1)
00154         {
00155           ldyp[left] = 255;
00156           // ldyp[left+1] = 255;
00157         }
00158         if (right != 0 && right < dst_roi->width-1)
00159         {
00160           ldyp[right] = 255;
00161           // ldyp[right-1] = 255;
00162         }
00163       }
00164     }
00165
00166     lyp  += src_roi[0]->line_step;
00167     lup  += src_roi[0]->line_step / 2;
00168     lvp  += src_roi[0]->line_step / 2;
00169     ldyp += dst_roi->line_step;
00170     yp    = lyp;
00171     up    = lup;
00172     vp    = lvp;
00173     dyp   = ldyp;
00174   }
00175   for (w = 0; w < dst_roi->width; w++)
00176   {
00177     if (top[w] != 0 && top[w] != dst_roi->height - 1)
00178         *(dst + ((dst_roi->start.y + top[w]) * dst_roi->line_step)
00179               + ((dst_roi->start.x + w) * dst_roi->pixel_step)) = 255;
00180     if (bottom[w] != 0 && bottom[w] != dst_roi->height - 1)
00181         *(dst + ((dst_roi->start.y + bottom[w]) * dst_roi->line_step)
00182               + ((dst_roi->start.x + w) * dst_roi->pixel_step)) = 255;
00183   }
00184 }
00185