#include "vibe-background.h" int main(int argc, char **argv){ // ViBe vibeModel_t *model = libvibeModelNew(); // stride - , uint8_t *image_data = acquire_image(stream); int32_t width = get_image_width(stream); int32_t height = get_image_height(stream); int32_t stride = get_image_stride(stream); // uint8_t *segmentation_map = malloc(stride * height); // libvibeModelAllocInit_8u_C1R(model, image_data, width, height, stride); // // - segmentation_map while(!finished(stream)){ image_data = acquire_image(stream); libvibeModelUpdate_8u_C1R(model, image_data, segmentation_map); } // libvibeModelFree(model); }
typedef struct { u8b *samples; u32b numberOfSamples; u32b sizeOfSample; } pixel; typedef struct { pixel *pixels; u32b width; u32b height; u32b stride; u32b numberOfSamples; u32b matchingThreshold; u32b matchingNumber; u32b updateFactor; } vibeModel; typedef vibeModel vibeModel_t;
vibeModel *libvibeModelNew() { vibeModel *model = (vibeModel*)calloc(1,sizeof(vibeModel)); if (model) { model->numberOfSamples = 20; model->matchingThreshold = 20; model->matchingNumber = 2; model->updateFactor = 16; } u32b seed = time(0); srand(seed); return model; }
u32b getRandPixel(const u8b *image_data, const u32b width, const u32b height, const u32b stride, const u32b x, const u32b y);
s32b libvibeModelAllocInit_8u_C1R(vibeModel *model, const u8b *image_data, const u32b width, const u32b height, const u32b stride) { if (!model || !image_data || !width || !height || !stride || (stride<width)) return 1; // model->width = width; model->height = height; model->stride = stride; // model->pixels = 0; model->pixels = (pixel*)calloc(model->width*model->height, sizeof(pixel)); if (!model->pixels) return 1; // . for (u32b i=0; i < model->width*model->height; i++) { model->pixels[i].numberOfSamples=model->numberOfSamples; model->pixels[i].sizeOfSample = 1; model->pixels[i].samples = 0; model->pixels[i].samples = (u8b*)calloc(model->numberOfSamples,sizeof(u8b)); if (!model->pixels[i].samples) return 1; } // . // . , // . u32b n=0; for (u32b j=0; j < model->height; j++) { for (u32b i=0; i < model->width; i++) { model->pixels[n].samples[0] = image_data[i+j*stride]; for (u32b k=1; k < model->numberOfSamples; k++) model->pixels[n].samples[k] = getRandPixel(image_data, width, height, stride, i, j); n++; } } return 0; }
// pix_data - // pixel - vibeModel s32b comparePixel(u8b pix_data, pixel *pixel, u32b matchingThreshold, u32b matchingNumber) { u32b matchingCounter=0; // for (u32b i=0; i<pixel->numberOfSamples; i++) { if (abs((s32b)pix_data-(s32b)pixel->samples[i]) < matchingThreshold) { // MatchingNumber, // , matchingCounter++; if (matchingCounter >= matchingNumber) return 1; } } return 0; }
updateModel(vibeModel *model, u8b pix_data, u32b width, u32b height, u32b stride, u32b x, u32b y);
s32b libvibeModelUpdate_8u_C1R(vibeModel *model, const u8b *image_data, u8b *segmentation_map) { s32b ad = model->stride - model->width; if (!model || !image_data || !segmentation_map) return 1; if (model->stride < model->width) return 1; u32b n=0; for (u32b j=0; j < model->height; j++) { for (u32b i=0; i < model->width; i++) { // if (comparePixel(image_data[n], &(model->pixels[n]), model->matchingThreshold, model->matchingNumber)) { // - segmentation_map[n] = 0; updateModel(model, image_data[n], model->width, model->height, model->stride,i,j); } else { // segmentation_map[n] = 0xFFU; } n++; } if (model->stride > model->width) n+=ad; } return 0; }
Source: https://habr.com/ru/post/165677/
All Articles