Fawkes API  Fawkes Development Version
colorthreshold.cpp
1 
2 /***************************************************************************
3  * colorthreshold.cpp - color threshold filter
4  *
5  * Created: Mon Jan 27 06:36:27 2014 +0100
6  * Copyright 2014 Victor Matare
7  ****************************************************************************/
8 
9 /* This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version. A runtime exception applies to
13  * this software (see LICENSE.GPL_WRE file mentioned below for details).
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
21  */
22 
23 #include <fvfilters/colorthreshold.h>
24 #include <fvutils/color/rgbyuv.h>
25 #include <fvutils/color/threshold.h>
26 #include <fvutils/color/yuv.h>
27 
28 #include <math.h>
29 
30 namespace firevision {
31 
33 : Filter("FilterColorThreshold", 1), color_model_(color_model)
34 {
35 }
36 
37 FilterColorThreshold::~FilterColorThreshold()
38 {
39 }
40 
41 void
43 {
44  unsigned int h = 0;
45  unsigned int w = 0;
46 
47  unsigned char *p_src_y = src[0] + (src_roi[0]->start.y * src_roi[0]->line_step)
48  + (src_roi[0]->start.x * src_roi[0]->pixel_step);
49  unsigned char *p_src_u =
50  YUV422_PLANAR_U_PLANE(src[0], src_roi[0]->image_width, src_roi[0]->image_height)
51  + ((src_roi[0]->start.y * src_roi[0]->line_step) / 2
52  + (src_roi[0]->start.x * src_roi[0]->pixel_step) / 2);
53  unsigned char *p_src_v =
54  YUV422_PLANAR_V_PLANE(src[0], src_roi[0]->image_width, src_roi[0]->image_height)
55  + ((src_roi[0]->start.y * src_roi[0]->line_step) / 2
56  + (src_roi[0]->start.x * src_roi[0]->pixel_step) / 2);
57 
58  unsigned char *p_dst_y =
60  unsigned char *p_dst_u =
61  YUV422_PLANAR_U_PLANE(dst, dst_roi->image_width, dst_roi->image_height)
62  + ((dst_roi->start.y * dst_roi->line_step) / 2 + (dst_roi->start.x * dst_roi->pixel_step) / 2);
63  unsigned char *p_dst_v =
64  YUV422_PLANAR_V_PLANE(dst, dst_roi->image_width, dst_roi->image_height)
65  + ((dst_roi->start.y * dst_roi->line_step) / 2 + (dst_roi->start.x * dst_roi->pixel_step) / 2);
66 
67  unsigned const char *p_line_src_y = p_src_y, *p_line_src_u = p_src_u, *p_line_src_v = p_src_v,
68  *p_line_dst_y = p_dst_y, *p_line_dst_u = p_dst_u, *p_line_dst_v = p_dst_v;
69 
70  for (h = 0; (h < src_roi[0]->height) && (h < dst_roi->height); ++h) {
71  for (w = 0; (w < src_roi[0]->width) && (w < dst_roi->width); w += 2) {
72  // just copy Y plane from src to dst
73  *p_dst_y++ = *p_src_y++;
74  *p_dst_y++ = *p_src_y++;
75 
76  if (color_model_->determine(*p_src_y, *p_src_u, *p_src_v) != C_OTHER) {
77  *p_dst_u++ = *p_src_u;
78  *p_dst_v++ = *p_src_v;
79  } else {
80  *p_dst_u++ = 0x80;
81  *p_dst_v++ = 0x80;
82  }
83  p_src_u++;
84  p_src_v++;
85  }
86 
87  p_line_src_y += src_roi[0]->line_step;
88  p_line_src_u += src_roi[0]->line_step / 2;
89  p_line_src_v += src_roi[0]->line_step / 2;
90 
91  p_line_dst_y += src_roi[0]->line_step;
92  p_line_dst_u += src_roi[0]->line_step / 2;
93  p_line_dst_v += src_roi[0]->line_step / 2;
94  }
95 }
96 
97 } /* namespace firevision */
Matches colors that are similar to given reference colors.
Definition: similarity.h:41
virtual color_t determine(unsigned int y, unsigned int u, unsigned int v) const
Determine the color class of a given YUV value.
Definition: similarity.cpp:63
FilterColorThreshold(ColorModelSimilarity *color_model)
Constructor.
virtual void apply()
Apply the filter.
Filter interface.
Definition: filter.h:33
ROI ** src_roi
Source ROIs, dynamically allocated by Filter ctor.
Definition: filter.h:66
unsigned char ** src
Source buffers, dynamically allocated by Filter ctor.
Definition: filter.h:61
unsigned char * dst
Destination buffer.
Definition: filter.h:63
ROI * dst_roi
Destination ROI.
Definition: filter.h:68
unsigned int height
ROI height.
Definition: roi.h:119
fawkes::upoint_t start
ROI start.
Definition: roi.h:115
unsigned int line_step
line step
Definition: roi.h:125
unsigned int width
ROI width.
Definition: roi.h:117
unsigned int image_width
width of image that contains this ROI
Definition: roi.h:121
unsigned int pixel_step
pixel step
Definition: roi.h:127
unsigned int image_height
height of image that contains this ROI
Definition: roi.h:123
unsigned int x
x coordinate
Definition: types.h:36
unsigned int y
y coordinate
Definition: types.h:37