00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <filters/morphology/segenerator.h>
00026
00027 #include <utils/math/angle.h>
00028
00029 #include <fvutils/draw/drawer.h>
00030 #include <fvutils/color/colorspaces.h>
00031 #include <fvutils/writers/png.h>
00032 #include <fvutils/writers/fvraw.h>
00033
00034 #include <cstdlib>
00035 #include <cstring>
00036
00037 namespace firevision {
00038 #if 0
00039 }
00040 #endif
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 unsigned char *
00060 SEGenerator::linear(unsigned int width, unsigned int height,
00061 unsigned int *proposed_center_x, unsigned int *proposed_center_y,
00062 float slope_angle_rad)
00063 {
00064
00065
00066
00067
00068
00069
00070
00071 if ( height == 0 ) return NULL;
00072 if ( width == 0) return NULL;
00073
00074
00075 unsigned char *tmp = (unsigned char *)malloc(colorspace_buffer_size(YUV422_PLANAR, width, height));
00076 memset(tmp, 0, colorspace_buffer_size(YUV422_PLANAR, width, height));
00077 Drawer *d = new Drawer();
00078 d->set_buffer(tmp, width, height);
00079 d->set_color(1, 0, 0);
00080
00081 float a = fawkes::normalize_mirror_rad( slope_angle_rad );
00082
00083 if ( (a == M_PI/2) || (a == -M_PI/2) ) {
00084
00085
00086 d->draw_line(0, 0, 0, height - 1);
00087 } else {
00088
00089
00090 if ( a > M_PI / 2) a -= M_PI;
00091 if ( a < - M_PI / 2) a += M_PI;
00092
00093 int y = (int)roundf(((float)width - 1.f) * tan( a ));
00094
00095 if ( y < 0) {
00096
00097 d->draw_line( 0, 0, width - 1, -y );
00098 } else {
00099
00100 d->draw_line( 0, y, width - 1, 0 );
00101 }
00102 }
00103
00104 delete d;
00105
00106 unsigned char *se = (unsigned char *)malloc(width * height);
00107 memcpy(se, tmp, width * height);
00108
00109 PNGWriter *png = new PNGWriter();
00110 png->set_dimensions( width, height );
00111 png->set_buffer(YUV422_PLANAR, tmp);
00112 png->set_filename("se_test.png");
00113 png->write();
00114 delete png;
00115
00116 FvRawWriter *fvraw = new FvRawWriter("se_test.raw", width, height, YUV422_PLANAR, tmp);
00117 fvraw->write();
00118 delete fvraw;
00119
00120 free( tmp );
00121
00122 if ( (proposed_center_x != NULL) && (proposed_center_y != NULL) ) {
00123 unsigned int min_x = width;
00124 unsigned int max_x = 0;
00125 unsigned int min_y = height;
00126 unsigned int max_y = 0;
00127 for (unsigned int h = 0; h < height; ++h) {
00128 for (unsigned int w = 0; w < width; ++w) {
00129 if ( se[ h * width + w ] != 0 ) {
00130 if ( w < min_x ) min_x = w;
00131 if ( w > max_x ) max_x = w;
00132 if ( h < min_y ) min_y = h;
00133 if ( h > max_y ) max_y = h;
00134 }
00135 }
00136 }
00137
00138 *proposed_center_x = min_x + (max_x - min_x) / 2;
00139 *proposed_center_y = min_y + (max_y - min_y) / 2;
00140 }
00141
00142 return se;
00143 }
00144
00145
00146
00147
00148
00149
00150
00151
00152 unsigned char *
00153 SEGenerator::square(unsigned int width, unsigned int height)
00154 {
00155 unsigned char *se = (unsigned char *)malloc(width * height);
00156 memset(se, 1, width * height);
00157 return se;
00158 }
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168 void
00169 SEGenerator::drawSE(unsigned char *yuv422planar_buffer, unsigned char *mask, unsigned int width, unsigned int height)
00170 {
00171 memset(yuv422planar_buffer, 128, colorspace_buffer_size(YUV422_PLANAR, width, height) );
00172 for (unsigned int h = 0; h < height; ++h) {
00173 for (unsigned int w = 0; w < width; ++w) {
00174 if ( mask[ h * width + w ] != 0 ) {
00175 yuv422planar_buffer[ h * width + w ] = 255;
00176 }
00177 }
00178 }
00179 }
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189 void
00190 SEGenerator::drawSEbw(unsigned char *yuv422planar_buffer, unsigned char *mask, unsigned int width, unsigned int height)
00191 {
00192 memset(yuv422planar_buffer, 128, colorspace_buffer_size(YUV422_PLANAR, width, height) );
00193 memset(yuv422planar_buffer, 255, width * height );
00194 for (unsigned int h = 0; h < height; ++h) {
00195 for (unsigned int w = 0; w < width; ++w) {
00196 if ( mask[ h * width + w ] != 0 ) {
00197 yuv422planar_buffer[ h * width + w ] = 0;
00198 }
00199 }
00200 }
00201 }
00202
00203 }