grey8image.cc
来自「VC视频对象的跟踪提取原代码(vc视频监控源码)」· CC 代码 · 共 1,031 行 · 第 1/2 页
CC
1,031 行
/* * Grey8Image.cc * Image class for 8-bit greylevel images * * author : A M Baumberg */#include "Grey8Image.h"#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <cassert>#include "Kernel.h"#include "RGB32Image.h"#include "Point2.h"#include "text_output.h"#include "tracker_defines_types_and_helpers.h" #ifndef NO_DISPLAY#ifdef USE_GL#include <gl/gl.h>#else#include <X11/X.h>#include <X11/Xlib.h>#include <X11/Xutil.h>#include <X11/Xatom.h>#endif#endif // #ifndef NO_DISPLAYnamespace ReadingPeopleTracker{// definition and initialisation of static member variablesconst int Grey8Image::BOX_SIZE = 8;// definition and initialisation of static member variables#ifndef NO_DISPLAY#ifndef USE_GL// definition and initialisation of X11 interface specific static member variablesGrey8Image::Colormap cmap = 0;int Grey8Image::no_using_cmap = 0;unsigned char Grey8Image::pixel;//unsigned long plane_masks[8];//unsigned int no_planes = 8;unsigned long Grey8Image::reverse_cmap[256];int Grey8Image::no_grey_cells;#endif // ifndef USE_GL#endif // ifndef NO_DISPLAYImage *Grey8Image::copy(Image *res) const{ if (res == NULL) res = (Image *) new Grey8Image(width, height, frame_id, frame_time_in_ms); else { // check destination image format and dimensions assert (res->get_image_type() == GREY8); assert (res->get_width() == width); assert (res->get_height() == height); // use same frame id and time since it is the same image res->set_frame_id(frame_id); res->set_frame_time_in_ms(frame_time_in_ms); } res->set_draw_colour(Grey8_draw_colour); res->set_timestamp(×tamp); // copy over actual image data... void *src = data; void *dest = res->get_data(); size_t n = width * height; memcpy(dest, src, n); return res;}Image *Grey8Image::resample(unsigned int xstep, unsigned int ystep, Image *resampled){ int new_width = ((width / xstep) / 4) * 4; int new_height = height / ystep; if ((height % ystep) != 0) new_height++; if (resampled == NULL) resampled = new Grey8Image(new_width, new_height, frame_id, frame_time_in_ms); unsigned char *idat; unsigned char *odat; unsigned int outy = 0; for (unsigned int y = 0; y < height; y += ystep) { idat = get_pixel(0,y); odat = resampled->get_pixel(0, outy); for (unsigned int x = 0; x < new_width; x ++) { *odat = *idat; odat++; idat = idat + xstep; } outy++; } return resampled;}void Grey8Image::clear_border(){ int w1 = width-1; int h1 = height-1; for (int y = 0; y < height; y++) { *get_pixel(0,y) = CLEAR_MARK; *get_pixel(w1,y) = CLEAR_MARK; } for (int x = 0; x < width; x++) { *get_pixel(x,0) = CLEAR_MARK; *get_pixel(x,h1) = CLEAR_MARK; }}Image *Grey8Image::convolve(Kernel k, Image *convolved){ unsigned char *upnt, *pnt, *dpnt; unsigned char *out; int temp_total; int k11 = k.coeff[0][0], k12 = k.coeff[0][1], k13 = k.coeff[0][2]; int k21 = k.coeff[1][0], k22 = k.coeff[1][1], k23 = k.coeff[1][2]; int k31 = k.coeff[2][0], k32 = k.coeff[2][1], k33 = k.coeff[2][2]; int ksum = abs(k11)+abs(k12)+abs(k13) +abs(k21)+abs(k22)+abs(k23) +abs(k31)+abs(k32)+abs(k33); if (convolved == NULL) convolved = new Grey8Image(width, height, frame_id, frame_time_in_ms); convolved->clear_border(); for (int y = 1; y < (height-1); y++) { upnt = get_pixel(0,y-1); pnt = get_pixel(0,y); dpnt = get_pixel(0,y+1); out = convolved->get_pixel(1,y); for (int x = 1; x < (width-1); x++) { temp_total = k11 * upnt[0] + k12 * upnt[1] + k13 * upnt[2]; temp_total += k21 * pnt[0] + k22 * pnt[1] + k23 * pnt[2]; temp_total += k31 * dpnt[0] + k32 * dpnt[1] + k33 * dpnt[2]; *(out++) = (unsigned char) abs(temp_total / ksum); upnt++; pnt++; dpnt++; } } return convolved;}inline int Grey8Image::compar_char(const void *c1, const void *c2){ return (int) (*((unsigned char *) c1) - *((unsigned char *) c2));}Image *Grey8Image::neighbour_order(unsigned int n, Image *ordered){ if (ordered == NULL) ordered = new Grey8Image (width, height, frame_id, frame_time_in_ms); ordered->clear_border(); unsigned char table[9]; unsigned char *upnt, *pnt, *dpnt; unsigned char *out; int ntake1 = n - 1; for (int y = 1; y < height-1; y++) { upnt = get_pixel(0,y-1); pnt = get_pixel(0,y); dpnt = get_pixel(0,y+1); out = ordered->get_pixel(1,y); for (int x = 1; x < width-1; x++) { table[0] = upnt[0]; table[1] = upnt[1]; table[2] = upnt[2]; table[3] = pnt[0]; table[4] = pnt[1]; table[5] = pnt[2]; table[6] = dpnt[0]; table[7] = dpnt[1]; table[8] = dpnt[2]; qsort(table, 9, sizeof(unsigned char), &compar_char); *out = table[ntake1]; upnt++; pnt++; dpnt++;out++; } } return ordered;}Image *Grey8Image::minimum(Image *res){ if (res == NULL) res = new Grey8Image(width, height, frame_id, frame_time_in_ms); res->clear_border(); unsigned char *upnt, *pnt, *dpnt, min, tmp; unsigned char *out; for (int y = 1; y < height-1; y++) { upnt = get_pixel(0,y-1); pnt = get_pixel(0,y); dpnt = get_pixel(0,y+1); out = res->get_pixel(1,y); for (int x = 1; x < width-1; x++) { min = *upnt; if (min > (tmp = *(upnt + 1))) min = tmp; if (min > (tmp = *(upnt + 2))) min = tmp; if (min > (tmp = *(pnt))) min = tmp; if (min > (tmp = *(pnt + 1))) min = tmp; if (min > (tmp = *(pnt + 2))) min = tmp; if (min > (tmp = *(dpnt))) min = tmp; if (min > (tmp = *(dpnt + 1))) min = tmp; if (min > (tmp = *(dpnt + 2))) min = tmp; *out++ = min; upnt++; pnt++; dpnt++; } } return res;}Image *Grey8Image::maximum(Image *res){ if (res == NULL) res = new Grey8Image(width, height, frame_id, frame_time_in_ms); res->clear_border(); unsigned char *upnt, *pnt, *dpnt, max, tmp; unsigned char *out; for (unsigned int y = 1; y < height-1; y++) { upnt = get_pixel(0,y-1); pnt = get_pixel(0,y); dpnt = get_pixel(0,y+1); out = res->get_pixel(1,y); for (unsigned int x = 1; x < width-1; x++) { max = *upnt; if (max < (tmp = *(upnt + 1))) max = tmp; if (max < (tmp = *(upnt + 2))) max = tmp; if (max < (tmp = *(pnt))) max = tmp; if (max < (tmp = *(pnt + 1))) max = tmp; if (max < (tmp = *(pnt + 2))) max = tmp; if (max < (tmp = *(dpnt))) max = tmp; if (max < (tmp = *(dpnt + 1))) max = tmp; if (max < (tmp = *(dpnt + 2))) max = tmp; *out++ = max; upnt++; pnt++; dpnt++; } } return res;}unsigned char Grey8Image::get_mid (unsigned char &a, unsigned char &b, unsigned char &c){ if (a < b) { if (b <= c) return b; else { if (c <= a) return a; else return c; } } else { if (a <= c) return a; else { if (b >= c) return b; else return c; } }}Image *Grey8Image::fmed(Image *res){ if (res == NULL) res = new Grey8Image(width, height, frame_id, frame_time_in_ms); res->clear_border(); unsigned char *upnt, *pnt, *dpnt, mid1, mid2, mid3; unsigned char *out; for (int y = 1; y < height-1; y++) { upnt = get_pixel(0,y-1); pnt = get_pixel(0,y); dpnt = get_pixel(0,y+1); out = res->get_pixel(1,y); for (int x = 1; x < width-1; x++) { mid1 = get_mid(upnt[0],upnt[1],upnt[2]); mid2 = get_mid(pnt[0],pnt[1],pnt[2]); mid3 = get_mid(dpnt[0],dpnt[1],dpnt[2]); *out++ = get_mid(mid1, mid2, mid3); upnt++; pnt++; dpnt++; } } return res;}Image *Grey8Image::sobel(unsigned int threshold, Image *hsobel){ unsigned int sqthresh = threshold * threshold; unsigned int hpix; unsigned int vpix; hsobel = convolve(SOBELH_KER, hsobel); Image *vsobel = convolve(SOBELV_KER); unsigned char *hdat, *vdat, *idat, *enddat; enddat = get_end_data(); hdat = hsobel->get_data(); vdat = vsobel->get_data(); for (idat = data; idat < enddat; idat++) { hpix = (int) *hdat; vpix = (int) *vdat; if ((hpix * hpix + vpix * vpix) > sqthresh) *hdat = MARK; else *hdat = CLEAR_MARK; hdat++; vdat++; } delete vsobel; return hsobel;}Image *Grey8Image::sobel(Image *hsobel){ int hpix, vpix; hsobel = convolve(Kernel(1,0,-1,2,0,-2,1,0,-1,true), hsobel); Image *vsobel = convolve(Kernel(1,0,-1,2,0,-2,1,0,-1,false)); unsigned char *hdat, *vdat, *idat, *enddat; enddat = get_end_data(); hdat = hsobel->get_data(); vdat = vsobel->get_data(); for (idat = data; idat < enddat; idat++) { hpix = (int) *hdat; vpix = (int) *vdat; *hdat = (unsigned char) (MAX(hpix,vpix)); hdat++; vdat++; } delete vsobel; return hsobel;}Image *Grey8Image::threshold(unsigned int thresh, Image *outimg, unsigned int *no_marked){ if (outimg == NULL) outimg = new Grey8Image(width, height, frame_id, frame_time_in_ms); unsigned int mark_count = 0; unsigned char *idat = data, *odat = outimg->get_data(); unsigned char *enddat = get_end_data(); for (;idat<enddat;idat++) { if (*idat > thresh) { *odat = MARK; mark_count++; } else *odat = CLEAR_MARK; odat ++; } if (no_marked != NULL) *no_marked = mark_count; return outimg;}Image *Grey8Image::extract_subimage(int xmin, int xmax, int ymin, int ymax, Image *subimg){ assert (xmin < xmax); assert (ymin < ymax); assert (check_coords(xmin,ymin) == true); assert (check_coords(xmax,ymax) == true); if (subimg == NULL) subimg = new Grey8Image((xmax - xmin) + 1, (ymax - ymin) + 1, frame_id, frame_time_in_ms); else { assert (subimg->get_width() == (xmax - xmin) + 1); assert (subimg->get_height() == (ymax - ymin) + 1); } unsigned char *indat; unsigned char *outdat; for (int y = ymin; y <= ymax; y++) { indat = get_pixel(xmin,y); outdat = subimg->get_pixel(0,y-ymin); for (int x = xmin; x <= xmax; x++) { *outdat++ = *indat; *indat++ = CLEAR_MARK; } } return subimg;}Image *Grey8Image::fcombine(Image *image2, int (*func) (void*, void*), Image *res){ if (res == NULL) res = new Grey8Image(width, height, frame_id, frame_time_in_ms); unsigned char *idat1 = data, *idat2 = image2->get_data(); unsigned char *odat = res->get_data(), *enddat = get_end_data(); for (;idat1 < enddat; idat1++) *odat++ = (*func) ((void*) (idat1), (void*) (idat2++)); return res;}Image *Grey8Image::simple_difference(Image *image2, realno thresh, unsigned int *no_marked, Image *res){ unsigned int mark_count = 0; unsigned int threshold = (unsigned int) thresh; // if thresh < 0 don't threshold, just subtract if (thresh < 0) { if (no_marked != NULL) *no_marked = 0; return difference(image2, res); } if (image2->get_width() != width || image2->get_height() != height) { cerror << " cannot difference different sized images !"; exit(1); } if (res == NULL) res = new Grey8Image(width, height, frame_id, frame_time_in_ms); unsigned char *idat1 = data; unsigned char *idat2 = image2->get_data(); unsigned char *odat = res->get_data(); unsigned char *enddat = get_end_data(); for ( ;idat1 < enddat; idat1++) { if (abs(*idat1 - *idat2) > threshold) { *odat = MARK; mark_count++; } else *odat = CLEAR_MARK; idat2++;odat++; } if (no_marked != NULL) *no_marked = mark_count; return res;}Image *Grey8Image::difference(Image *image2, Image *res){ if (image2->get_width() != width || image2->get_height() != height) { cerror << " cannot difference different sized images !"; exit(1); } if (res == NULL) res = new Grey8Image(width, height, frame_id, frame_time_in_ms); unsigned char *idat1; unsigned char *idat2 = image2->get_data(); unsigned char *odat = res->get_data(); unsigned char *enddat = get_end_data(); for (idat1 = data; idat1 < enddat; idat1++, idat2++) { *odat++ = abs (*idat1 - *idat2); } return res;}Image *Grey8Image::half_blur(Image *res){ int new_width = width / 2; int new_height = height / 2;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?