VglSimpleBGModel::VglSimpleBGModel(VglImage* img_in, float std_thresh, int window_size, int num_training_images){ this->width = img_in->shape[VGL_WIDTH]; this->height = img_in->shape[VGL_HEIGHT]; this->nChannels = img_in->nChannels; this->depth = img_in->depth; if (depth != IPL_DEPTH_8U){ printf("VglSimpleBGModel: Error: depth must be IPL_DEPTH_8U\n"); return; } if (num_training_images < 2){ printf("VglSimpleBGModel: Error: num_training_images must be >= 2\n"); return; } if (std_thresh <= 0.0){ printf("VglSimpleBGModel: Error: std_thresh must be > 0.0\n"); return; } if (window_size < 1){ printf("VglSimpleBGModel: Error: window_size must be >= 1\n"); return; } this->num_training_images = num_training_images; this->std_thresh = std_thresh; this->window_size = window_size; foreground = vglCreateImage(img_in); // should be single channel foregroundClose = vglCreateImage(img_in); // should be single channel buf = vglCreateImage(img_in); // should be single channel background32 = vglCreateImage(cvSize(width, height), IPL_DEPTH_32F, nChannels); // should be float variance32 = vglCreateImage(cvSize(width, height), IPL_DEPTH_32F, nChannels); // should be float vglClear(foreground, 0.0, 0.0, 0.0); vglClear(foregroundClose, 0.0, 0.0, 0.0); vglClear(buf, 0.0, 0.0, 0.0); vglClear(background32, 0.0, 0.0, 0.0); vglClear(variance32, 0.0, 0.0, 0.0); curr_training_image = 0; sobel = 0; color = 0; grayx = 0; grayy = 0; //TrainVglSimpleBGModel(img_in); vglSetContext(foreground, VGL_GL_CONTEXT); vglSetContext(foregroundClose, VGL_GL_CONTEXT); vglSetContext(buf, VGL_GL_CONTEXT); vglSetContext(background32, VGL_GL_CONTEXT); vglSetContext(variance32, VGL_GL_CONTEXT); }
void vglGlToCl(VglImage* img) { glFlush(); glFinish(); if (img->oclPtr == NULL) { vglClAlloc(img); } if (!vglIsInContext(img, VGL_CL_CONTEXT)) { //printf("==========ACQUIRE: vgl = %p, ocl = %d\n", img, img->oclPtr); cl_int err_cl = clFlush(cl.commandQueue); vglClCheckError(err_cl, (char*) "clFlush"); err_cl = clFinish(cl.commandQueue); vglClCheckError(err_cl, (char*) "clFinish"); err_cl = clEnqueueAcquireGLObjects(cl.commandQueue, 1 , (cl_mem*) &img->oclPtr, 0 , NULL, NULL); vglClCheckError(err_cl, (char*) "clEnqueueAcquireGLObjects"); vglSetContext(img, VGL_CL_CONTEXT); } //printf("Vai sair de %s\n", __FUNCTION__); }
void vglClToGl(VglImage* img) { //vglDownload(img); if (!vglIsInContext(img, VGL_CL_CONTEXT)) { //vglGlToCl(img); //fprintf(stderr, "vglClToGl: Error: image context = %d not in VGL_CL_CONTEXT\n", img->inContext); return; } cl_int err_cl; //printf("==========RELEASE: vgl = %p, ocl = %d\n", img, img->oclPtr); err_cl = clEnqueueReleaseGLObjects(cl.commandQueue, 1 , (cl_mem*) &img->oclPtr, 0 , NULL, NULL); vglClCheckError(err_cl, (char*) "clEnqueueReleaseGLObjects"); err_cl = clFlush(cl.commandQueue); vglClCheckError(err_cl, (char*) "clFlush"); err_cl = clFinish(cl.commandQueue); vglClCheckError(err_cl, (char*) "clFinish"); vglSetContext(img, VGL_GL_CONTEXT); //printf("Vai sair de %s\n", __FUNCTION__); }
/** Function for loading a stack of 3d TIFF images */ VglImage* vglLoad4dTiff(char* filename, int lStart, int lEnd, bool has_mipmap /*=0*/) { char* tempFilename = (char*)malloc(strlen(filename) + 256); sprintf(tempFilename, filename, lStart); VglImage* tmp = vglLoadTiff(tempFilename); int n = lEnd-lStart+1; int shape[VGL_MAX_DIM+1]; shape[0] = tmp->nChannels; shape[1] = tmp->getWidth(); shape[2] = tmp->getHeight(); shape[3] = tmp->getLength(); shape[4] = n; VglImage* img = vglCreateNdImage(4, shape, tmp->depth); //vglPrintImageInfo(img, "4D image"); int delta = tmp->getTotalSizeInBytes(); int offset = 0; vglReleaseImage(&tmp); for(int i = lStart; i <= lEnd; i++) { sprintf(tempFilename, filename, i); printf("filename[%d] = %s\n", i, tempFilename); VglImage* tmp = vglLoadTiff(tempFilename); memcpy(img->getImageData() + offset, tmp->getImageData(), delta); offset += delta; } vglSetContext(img, VGL_RAM_CONTEXT); return img; }
/** Function for loading TIFF images. Function for loading TIFF images. Supports RGB (8 bits) and grayscale (8 and 16 bits) images, 2D and 3D. Alternative version with simpler code. */ VglImage* vglLoadTiffAlt(char* inFilename) { TIFF* tif; VglImage* img; uint16 pageNumber, numberPages, subfileType; tif = TIFFOpen(inFilename, "r"); if (tif == NULL){ fprintf(stderr, "%s:%s: Error: File %s not found.\n", __FILE__, __FUNCTION__, inFilename); return NULL; } int width = tif_Width(tif); int height = tif_Height(tif); int is3d = tif_Is3d(tif); int layers = tif_Layers(tif); int depth = tif_BytesPerPixel(tif); // bytes per pixel int iplDepth = convertDepthTiffToVgl(depth); // depth \in {IPL_DEPTH_8U, ...} int nChannels = tif_nChannels(tif); // number of channels if (is3d) { img = vglCreate3dImage(cvSize(width,height), iplDepth, nChannels, layers, 0); } else { img = vglCreateImage(cvSize(width,height), iplDepth, nChannels); } char* imageData = img->getImageData(); int widthStep = img->getWidthStep(); char* buffer = (char*) tif_Malloc(tif); int pixelsPerLine = img->getWidth()*img->getNChannels(); int bytesPerLine = pixelsPerLine*depth; int i = 0; int offset = 0; do { /* for(int i = 0; i < height; i++) { printf("i = %d\n", i); TIFFReadScanline(tif, buffer, i); memcpy(imageData + offset, buffer, bytesPerLine); offset += widthStep; } */ TIFFReadRGBAImage(tif, width, height, (uint32*) buffer, 0); memcpy(imageData + offset, buffer, widthStep*height); offset += widthStep + height; } while(TIFFReadDirectory(tif)); TIFFClose(tif); vglSetContext(img, VGL_RAM_CONTEXT); return img; }
/** Function for loading TIFF images. Function for loading TIFF images. Supports RGB (8 bits) and grayscale (8 and 16 bits) images, 2D and 3D. */ VglImage* vglLoadTiff(char* inFilename) { TIFF* tif; VglImage* img; uint16 pageNumber, numberPages, subfileType; tif = TIFFOpen(inFilename, "r"); if (tif == NULL){ fprintf(stderr, "%s:%s: Error: File %s not found.\n", __FILE__, __FUNCTION__, inFilename); return NULL; } int width = tif_Width(tif); int height = tif_Height(tif); int is3d = tif_Is3d(tif); int layers = tif_Layers(tif); int depth = tif_BytesPerPixel(tif); // bytes per pixel int iplDepth = convertDepthTiffToVgl(depth); // depth \in {IPL_DEPTH_8U, ...} int nChannels = tif_nChannels(tif); // number of channels if (is3d) { img = vglCreate3dImage(cvSize(width,height), iplDepth, nChannels, layers, 0); } else { img = vglCreateImage(cvSize(width,height), iplDepth, nChannels); } char* imageData = img->getImageData(); int pixelsPerFrame = img->getWidth()*img->getHeight()*img->nChannels; int bytesPerFrame = pixelsPerFrame*depth; int j = 0; do{ char* buffer = (char*)tif_ReadData(tif); memcpy(imageData+j, buffer, bytesPerFrame); j += bytesPerFrame; }while(TIFFReadDirectory(tif)); TIFFClose(tif); vglSetContext(img, VGL_RAM_CONTEXT); return img; }
void vglCl3dMergeZByMean(VglImage* img_input, VglImage* img_output, int number_of_merges){ vglCheckContext(img_input, VGL_CL_CONTEXT); vglCheckContext(img_output, VGL_CL_CONTEXT); cl_int err; static cl_program program = NULL; if (program == NULL) { char* file_path = (char*) "CL/vglCl3dMergeZByMean.cl"; printf("Compiling %s\n", file_path); std::ifstream file(file_path); if(file.fail()) { std::string str("File not found: "); str.append(file_path); vglClCheckError(-1, (char*)str.c_str()); } std::string prog( std::istreambuf_iterator<char>( file ), ( std::istreambuf_iterator<char>() ) ); const char *source_str = prog.c_str(); #ifdef __DEBUG__ printf("Kernel to be compiled:\n%s\n", source_str); #endif program = clCreateProgramWithSource(cl.context, 1, (const char **) &source_str, 0, &err ); vglClCheckError(err, (char*) "clCreateProgramWithSource" ); err = clBuildProgram(program, 1, cl.deviceId, NULL, NULL, NULL ); vglClBuildDebug(err, program); } static cl_kernel kernel = NULL; if (kernel == NULL) { kernel = clCreateKernel( program, "vglCl3dMergeZByMean", &err ); vglClCheckError(err, (char*) "clCreateKernel" ); } err = clSetKernelArg( kernel, 0, sizeof( cl_mem ), (void*) &img_input->oclPtr ); vglClCheckError( err, (char*) "clSetKernelArg 0" ); err = clSetKernelArg( kernel, 1, sizeof( cl_mem ), (void*) &img_output->oclPtr ); vglClCheckError( err, (char*) "clSetKernelArg 1" ); err = clSetKernelArg( kernel, 2, sizeof( int ), (void*) &number_of_merges ); vglClCheckError( err, (char*) "clSetKernelArg 1" ); if (img_input->ndim <= 2){ printf("2D images not supported by this operation\n"); } else if (img_input->ndim == 3){ size_t worksize[] = { img_input->getWidth(), img_input->getHeight(), img_input->getLength() }; clEnqueueNDRangeKernel( cl.commandQueue, kernel, 3, NULL, worksize, 0, 0, 0, 0 ); } else{ printf("More than 3 dimensions not yet supported\n"); } vglClCheckError( err, (char*) "clEnqueueNDRangeKernel" ); vglSetContext(img_output, VGL_CL_CONTEXT); }
/** Calculate sum a = a + b and save carry */ int vglClMpIsZero(VglImage* num_a){ int isZero = 200; cl_mem isZero_oclPtr = NULL; vglCheckContext(num_a, VGL_CL_CONTEXT); cl_int err; isZero_oclPtr = clCreateBuffer(cl.context, CL_MEM_READ_WRITE, sizeof(isZero), NULL, &err); vglClCheckError( err, (char*) "clCreateNDImage" ); err = clEnqueueWriteBuffer(cl.commandQueue, isZero_oclPtr, CL_TRUE, 0, sizeof(isZero), &isZero, 0, NULL, NULL); vglClCheckError( err, (char*) "clEnqueueWriteBuffer" ); clFinish(cl.commandQueue); static cl_program program = NULL; if (program == NULL) { char* file_path = (char*) "CL_MP/vglClMpIsZero.cl"; printf("Compiling %s\n", file_path); std::ifstream file(file_path); if(file.fail()) { fprintf(stderr, "%s:%s: Error: File %s not found.\n", __FILE__, __FUNCTION__, file_path); exit(1); } std::string prog( std::istreambuf_iterator<char>( file ), ( std::istreambuf_iterator<char>() ) ); const char *source_str = prog.c_str(); #ifdef __DEBUG__ printf("Kernel to be compiled:\n%s\n", source_str); #endif program = clCreateProgramWithSource(cl.context, 1, (const char **) &source_str, 0, &err ); vglClCheckError(err, (char*) "clCreateProgramWithSource" ); err = clBuildProgram(program, 1, cl.deviceId, NULL, NULL, NULL ); vglClBuildDebug(err, program); } static cl_kernel kernel = NULL; if (kernel == NULL) { kernel = clCreateKernel( program, "vglClMpIsZero", &err ); vglClCheckError(err, (char*) "clCreateKernel" ); } err = clSetKernelArg( kernel, 0, sizeof( cl_mem ), (void*) &num_a->oclPtr ); vglClCheckError( err, (char*) "clSetKernelArg 0" ); err = clSetKernelArg( kernel, 1, sizeof( cl_mem ), (void*) &isZero_oclPtr ); vglClCheckError( err, (char*) "clSetKernelArg 1" ); if (num_a->ndim <= 2){ size_t worksize[] = { num_a->shape[VGL_WIDTH], num_a->shape[VGL_HEIGHT], 1 }; clEnqueueNDRangeKernel( cl.commandQueue, kernel, 2, NULL, worksize, 0, 0, 0, 0 ); } else if (num_a->ndim == 3){ size_t worksize[] = { num_a->shape[VGL_WIDTH], num_a->shape[VGL_HEIGHT], num_a->shape[VGL_LENGTH] }; clEnqueueNDRangeKernel( cl.commandQueue, kernel, 3, NULL, worksize, 0, 0, 0, 0 ); } else{ printf("More than 3 dimensions not yet supported\n"); } vglClCheckError( err, (char*) "clEnqueueNDRangeKernel" ); err = clEnqueueReadBuffer(cl.commandQueue, isZero_oclPtr, CL_TRUE, 0, sizeof(isZero), &isZero, 0, NULL, NULL); vglClCheckError( err, (char*) "clEnqueueReadNDImage" ); vglSetContext(num_a, VGL_CL_CONTEXT); return isZero; }
void vglClDownloadForce(VglImage* img) { vglSetContext(img,VGL_CL_CONTEXT); vglClToGl(img); vglDownloadFaster(img); }
void vglClUploadForce(VglImage* img) { vglSetContext(img,VGL_RAM_CONTEXT); vglUpload(img); vglGlToCl(img); }