Fawkes API  Fawkes Development Version
segment_color.cpp
1 
2 /***************************************************************************
3  * segment_color.cpp - Implementation of color segmentation filter
4  * This filter can be used to draw the segmentation for
5  * all objects into a colored YUV422_PLANAR buffer
6  *
7  * Created: Mon Jul 04 16:18:15 2005
8  * Copyright 2005-2007 Tim Niemueller [www.niemueller.de]
9  *
10  ****************************************************************************/
11 
12 /* This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version. A runtime exception applies to
16  * this software (see LICENSE.GPL_WRE file mentioned below for details).
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU Library General Public License for more details.
22  *
23  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
24  */
25 
26 #include <fvfilters/segment_color.h>
27 
28 #include <fvmodels/color/colormodel.h>
29 #include <fvutils/color/yuv.h>
30 #include <cstddef>
31 
32 namespace firevision {
33 #if 0 /* just to make Emacs auto-indent happy */
34 }
35 #endif
36 
37 /** @class FilterColorSegmentation <fvfilters/segment_color.h>
38  * Segmentation filter.
39  * Visually marks pixels depending of their classification determined by the
40  * supplied color model to make the segmentation visible.
41  * The pixels are marked with the color matching the segmentation with an
42  * appropriate place holder color
43  * @author Tim Niemueller
44  */
45 
46 /** Constructor.
47  * @param cm color model to use
48  */
50  : Filter("FilterColorSegmentation")
51 {
52  this->cm = cm;
53 }
54 
55 
56 void
58 {
59  register unsigned int h = 0;
60  register unsigned int w = 0;
61 
62  // source y-plane
63  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);
64  // source u-plane
65  register unsigned char *up = YUV422_PLANAR_U_PLANE(src[0], src_roi[0]->image_width, src_roi[0]->image_height)
66  + ((src_roi[0]->start.y * src_roi[0]->line_step) / 2 + (src_roi[0]->start.x * src_roi[0]->pixel_step) / 2) ;
67  // source v-plane
68  register unsigned char *vp = YUV422_PLANAR_V_PLANE(src[0], src_roi[0]->image_width, src_roi[0]->image_height)
69  + ((src_roi[0]->start.y * src_roi[0]->line_step) / 2 + (src_roi[0]->start.x * src_roi[0]->pixel_step) / 2);
70 
71  // destination y-plane
72  register unsigned char *dyp = dst + (dst_roi->start.y * dst_roi->line_step) + (dst_roi->start.x * dst_roi->pixel_step);
73  // destination u-plane
74  register unsigned char *dup = YUV422_PLANAR_U_PLANE(dst, dst_roi->image_width, dst_roi->image_height)
75  + ((dst_roi->start.y * dst_roi->line_step) / 2 + (dst_roi->start.x * dst_roi->pixel_step) / 2) ;
76  // destination v-plane
77  register unsigned char *dvp = YUV422_PLANAR_V_PLANE(dst, dst_roi->image_width, dst_roi->image_height)
78  + ((dst_roi->start.y * dst_roi->line_step) / 2 + (dst_roi->start.x * dst_roi->pixel_step) / 2);
79 
80  // line starts
81  unsigned char *lyp = yp; // source y-plane
82  unsigned char *lup = up; // source u-plane
83  unsigned char *lvp = vp; // source v-plane
84  unsigned char *ldyp = dyp; // destination y-plane
85  unsigned char *ldup = dup; // destination y-plane
86  unsigned char *ldvp = dvp; // destination y-plane
87 
88  color_t c1;
89  // Unused for now: color_t c2;
90 
91  for (h = 0; (h < src_roi[0]->height) && (h < dst_roi->height); ++h) {
92  for (w = 0; (w < src_roi[0]->width) && (w < dst_roi->width); w += 2) {
93  c1 = cm->determine(*yp++, *up++, *vp++);
94  yp++;
95  //c2 = cm->determine(*yp++, *up++, *vp++);
96 
97  switch (c1) {
98  case C_ORANGE:
99  *dyp++ = 128;
100  *dyp++ = 128;
101  *dup++ = 0;
102  *dvp++ = 255;
103  break;
104  case C_MAGENTA:
105  *dyp++ = 128;
106  *dyp++ = 128;
107  *dup++ = 128;
108  *dvp++ = 255;
109  break;
110  case C_CYAN:
111  *dyp++ = 128;
112  *dyp++ = 128;
113  *dup++ = 255;
114  *dvp++ = 0;
115  break;
116  case C_BLUE:
117  *dyp++ = 128;
118  *dyp++ = 128;
119  *dup++ = 255;
120  *dvp++ = 128;
121  break;
122  case C_YELLOW:
123  *dyp++ = 255;
124  *dyp++ = 255;
125  *dup++ = 0;
126  *dvp++ = 128;
127  break;
128  case C_GREEN:
129  *dyp++ = 128;
130  *dyp++ = 128;
131  *dup++ = 0;
132  *dvp++ = 0;
133  break;
134  case C_WHITE:
135  *dyp++ = 255;
136  *dyp++ = 255;
137  *dup++ = 128;
138  *dvp++ = 128;
139  break;
140  case C_RED:
141  *dyp++ = 196;
142  *dyp++ = 196;
143  *dup++ = 0;
144  *dvp++ = 255;
145  break;
146  default:
147  *dyp++ = 0;
148  *dyp++ = 0;
149  *dup++ = 128;
150  *dvp++ = 128;
151  break;
152  }
153  }
154  lyp += src_roi[0]->line_step;
155  lup += src_roi[0]->line_step / 2;
156  lvp += src_roi[0]->line_step / 2;
157  ldyp += dst_roi->line_step;
158  ldup += dst_roi->line_step / 2;
159  ldvp += dst_roi->line_step / 2;
160  yp = lyp;
161  up = lup;
162  vp = lvp;
163  dyp = ldyp;
164  dup = ldup;
165  dvp = ldvp;
166  }
167 
168 }
169 
170 } // end namespace firevision
Color model interface.
Definition: colormodel.h:34
FilterColorSegmentation(ColorModel *cm)
Constructor.
fawkes::upoint_t start
ROI start.
Definition: roi.h:119
unsigned int y
y coordinate
Definition: types.h:36
unsigned int x
x coordinate
Definition: types.h:35
unsigned int width
ROI width.
Definition: roi.h:121
virtual void apply()
Apply the filter.
unsigned int image_width
width of image that contains this ROI
Definition: roi.h:125
unsigned char ** src
Source buffers, dynamically allocated by Filter ctor.
Definition: filter.h:65
unsigned int image_height
height of image that contains this ROI
Definition: roi.h:127
Filter interface.
Definition: filter.h:35
ROI ** src_roi
Source ROIs, dynamically allocated by Filter ctor.
Definition: filter.h:70
unsigned int height
ROI height.
Definition: roi.h:123
unsigned int line_step
line step
Definition: roi.h:129
unsigned char * dst
Destination buffer.
Definition: filter.h:67
unsigned int pixel_step
pixel step
Definition: roi.h:131
ROI * dst_roi
Destination ROI.
Definition: filter.h:72
virtual color_t determine(unsigned int y, unsigned int u, unsigned int v) const =0
Determine classification of YUV pixel.