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 #include <fvmodels/color/colormodel.h>
28 #include <fvutils/color/yuv.h>
29 
30 #include <cstddef>
31 
32 namespace firevision {
33 
34 /** @class FilterColorSegmentation <fvfilters/segment_color.h>
35  * Segmentation filter.
36  * Visually marks pixels depending of their classification determined by the
37  * supplied color model to make the segmentation visible.
38  * The pixels are marked with the color matching the segmentation with an
39  * appropriate place holder color
40  * @author Tim Niemueller
41  */
42 
43 /** Constructor.
44  * @param cm color model to use
45  */
46 FilterColorSegmentation::FilterColorSegmentation(ColorModel *cm) : Filter("FilterColorSegmentation")
47 {
48  this->cm = cm;
49 }
50 
51 void
53 {
54  unsigned int h = 0;
55  unsigned int w = 0;
56 
57  // source y-plane
58  unsigned char *yp = src[0] + (src_roi[0]->start.y * src_roi[0]->line_step)
59  + (src_roi[0]->start.x * src_roi[0]->pixel_step);
60  // source u-plane
61  unsigned char *up =
62  YUV422_PLANAR_U_PLANE(src[0], src_roi[0]->image_width, src_roi[0]->image_height)
63  + ((src_roi[0]->start.y * src_roi[0]->line_step) / 2
64  + (src_roi[0]->start.x * src_roi[0]->pixel_step) / 2);
65  // source v-plane
66  unsigned char *vp =
67  YUV422_PLANAR_V_PLANE(src[0], src_roi[0]->image_width, src_roi[0]->image_height)
68  + ((src_roi[0]->start.y * src_roi[0]->line_step) / 2
69  + (src_roi[0]->start.x * src_roi[0]->pixel_step) / 2);
70 
71  // destination y-plane
72  unsigned char *dyp =
74  // destination u-plane
75  unsigned char *dup =
76  YUV422_PLANAR_U_PLANE(dst, dst_roi->image_width, dst_roi->image_height)
77  + ((dst_roi->start.y * dst_roi->line_step) / 2 + (dst_roi->start.x * dst_roi->pixel_step) / 2);
78  // destination v-plane
79  unsigned char *dvp =
80  YUV422_PLANAR_V_PLANE(dst, dst_roi->image_width, dst_roi->image_height)
81  + ((dst_roi->start.y * dst_roi->line_step) / 2 + (dst_roi->start.x * dst_roi->pixel_step) / 2);
82 
83  // line starts
84  unsigned char *lyp = yp; // source y-plane
85  unsigned char *lup = up; // source u-plane
86  unsigned char *lvp = vp; // source v-plane
87  unsigned char *ldyp = dyp; // destination y-plane
88  unsigned char *ldup = dup; // destination y-plane
89  unsigned char *ldvp = dvp; // destination y-plane
90 
91  color_t c1;
92  // Unused for now: color_t c2;
93 
94  for (h = 0; (h < src_roi[0]->height) && (h < dst_roi->height); ++h) {
95  for (w = 0; (w < src_roi[0]->width) && (w < dst_roi->width); w += 2) {
96  c1 = cm->determine(*yp++, *up++, *vp++);
97  yp++;
98  //c2 = cm->determine(*yp++, *up++, *vp++);
99 
100  switch (c1) {
101  case C_ORANGE:
102  *dyp++ = 128;
103  *dyp++ = 128;
104  *dup++ = 0;
105  *dvp++ = 255;
106  break;
107  case C_MAGENTA:
108  *dyp++ = 128;
109  *dyp++ = 128;
110  *dup++ = 128;
111  *dvp++ = 255;
112  break;
113  case C_CYAN:
114  *dyp++ = 128;
115  *dyp++ = 128;
116  *dup++ = 255;
117  *dvp++ = 0;
118  break;
119  case C_BLUE:
120  *dyp++ = 128;
121  *dyp++ = 128;
122  *dup++ = 255;
123  *dvp++ = 128;
124  break;
125  case C_YELLOW:
126  *dyp++ = 255;
127  *dyp++ = 255;
128  *dup++ = 0;
129  *dvp++ = 128;
130  break;
131  case C_GREEN:
132  *dyp++ = 128;
133  *dyp++ = 128;
134  *dup++ = 0;
135  *dvp++ = 0;
136  break;
137  case C_WHITE:
138  *dyp++ = 255;
139  *dyp++ = 255;
140  *dup++ = 128;
141  *dvp++ = 128;
142  break;
143  case C_RED:
144  *dyp++ = 196;
145  *dyp++ = 196;
146  *dup++ = 0;
147  *dvp++ = 255;
148  break;
149  default:
150  *dyp++ = 0;
151  *dyp++ = 0;
152  *dup++ = 128;
153  *dvp++ = 128;
154  break;
155  }
156  }
157  lyp += src_roi[0]->line_step;
158  lup += src_roi[0]->line_step / 2;
159  lvp += src_roi[0]->line_step / 2;
160  ldyp += dst_roi->line_step;
161  ldup += dst_roi->line_step / 2;
162  ldvp += dst_roi->line_step / 2;
163  yp = lyp;
164  up = lup;
165  vp = lvp;
166  dyp = ldyp;
167  dup = ldup;
168  dvp = ldvp;
169  }
170 }
171 
172 } // end namespace firevision
firevision::FilterColorSegmentation::apply
virtual void apply()
Definition: segment_color.cpp:58
firevision::ROI::width
unsigned int width
ROI width.
Definition: roi.h:121
firevision::Filter::src
unsigned char ** src
Source buffers, dynamically allocated by Filter ctor.
Definition: filter.h:70
firevision::ROI::image_width
unsigned int image_width
width of image that contains this ROI
Definition: roi.h:125
firevision::ROI::image_height
unsigned int image_height
height of image that contains this ROI
Definition: roi.h:127
firevision::ColorModel::determine
virtual color_t determine(unsigned int y, unsigned int u, unsigned int v) const =0
firevision::ROI::height
unsigned int height
ROI height.
Definition: roi.h:123
firevision::Filter::src_roi
ROI ** src_roi
Source ROIs, dynamically allocated by Filter ctor.
Definition: filter.h:75
firevision::FilterColorSegmentation::FilterColorSegmentation
FilterColorSegmentation(ColorModel *cm)
Constructor.
Definition: segment_color.cpp:52
fawkes::upoint_t::y
unsigned int y
y coordinate
Definition: types.h:36
firevision::ROI::pixel_step
unsigned int pixel_step
pixel step
Definition: roi.h:131
firevision::Filter::dst_roi
ROI * dst_roi
Destination ROI.
Definition: filter.h:77
firevision::ROI::start
fawkes::upoint_t start
ROI start.
Definition: roi.h:119
firevision::ROI::line_step
unsigned int line_step
line step
Definition: roi.h:129
fawkes::upoint_t::x
unsigned int x
x coordinate
Definition: types.h:35
firevision::Filter::dst
unsigned char * dst
Destination buffer.
Definition: filter.h:72