bool ImStaffSegment::AnalyzeSegment() { wxASSERT_MSG( m_opImMap, wxT("MAP Image cannot be NULL") ); int i; if ( !GetImagePlane( &m_opImMain ) ) return false; // margins m_opImTmp1 = imImageCreate( m_opImMain->width + 2, m_opImMain->height + 2, m_opImMain->color_space, m_opImMain->data_type ); if (!m_opImTmp1) return this->Terminate( ERR_MEMORY ); imProcessAddMargins( m_opImMain, m_opImTmp1, 1, 1); SwapImages( &m_opImMain, &m_opImTmp1 ); // close m_opImTmp1 = imImageClone( m_opImMain ); if (!m_opImTmp1) return this->Terminate( ERR_MEMORY ); imProcessBinMorphClose( m_opImMain, m_opImTmp1, 5, 1); SwapImages( &m_opImMain, &m_opImTmp1 ); m_opIm = imImageCreate(m_opImMain->width, m_opImMain->height, IM_GRAY, IM_USHORT); int region_count = imAnalyzeFindRegions ( m_opImMain, m_opIm, 8, 1); int* area = (int*)malloc( region_count * sizeof(int) ); memset(area, 0, region_count * sizeof(int) ); float* perim = (float*)malloc( region_count * sizeof(float) ); memset(perim, 0, region_count * sizeof(float) ); imAnalyzeMeasureArea( m_opIm, area, 1 ); imAnalyzeMeasurePerimeter( m_opIm, perim, 1 ); float c = 0; for (i = 0; i < region_count; i++ ) { c += pow(perim[i],2) / (4 * AX_PI * area[i]) * (area[i] / m_opIm->width); } //a /= median( area, region_count); //p /= medianf( perim, region_count); //wxLogMessage("compactness %f", c / region_count ); this->m_compactness = c; free( area ); free( perim ); return this->Terminate( ERR_NONE ); }
void imProcessFillHoles(const imImage* image, imImage* NewImage, int connect) { // finding regions in the inverted image will isolate only the holes. imProcessNegative(image, NewImage); imImage *region_image = imImageCreate(image->width, image->height, IM_GRAY, IM_USHORT); if (!region_image) return; int holes_count = imAnalyzeFindRegions(NewImage, region_image, connect, 0); if (!holes_count) { imImageCopy(image, NewImage); imImageDestroy(region_image); return; } imushort* region_data = (imushort*)region_image->data[0]; imbyte* dst_data = (imbyte*)NewImage->data[0]; for (int i = 0; i < image->count; i++) { if (*region_data) *dst_data = 1; else *dst_data = !(*dst_data); // Fix negative data. region_data++; dst_data++; } imImageDestroy(region_image); }
void imProcessCrossCorrelation(const imImage* src_image1, const imImage* src_image2, imImage* dst_image) { imImage *tmp_image = imImageCreate(src_image2->width, src_image2->height, src_image2->color_space, IM_COMPLEX); if (!tmp_image) return; if (src_image2->data_type != IM_COMPLEX) imConvertDataType(src_image2, tmp_image, 0, 0, 0, 0); else imImageCopy(src_image2, tmp_image); if (src_image1->data_type != IM_COMPLEX) imConvertDataType(src_image1, dst_image, 0, 0, 0, 0); else imImageCopy(src_image1, dst_image); imProcessFFTraw(tmp_image, 0, 1, 1); // forward, centered, normalized imProcessFFTraw(dst_image, 0, 1, 1); imProcessMultiplyConj(dst_image, tmp_image, dst_image); imProcessFFTraw(dst_image, 1, 1, 1); // inverse, uncentered, normalized imProcessSwapQuadrants(dst_image, 0); // from origin to center imImageDestroy(tmp_image); }
static imImage* iKernelCreate(int w, int h, int* data, const char* desc) { imImage* kernel = imImageCreate(w, h, IM_GRAY, IM_INT); int* kernel_data = (int*)kernel->data[0]; memcpy(kernel_data, data, kernel->size); imImageSetAttribute(kernel, "Description", IM_BYTE, -1, (void*)desc); return kernel; }
int imProcessGrayMorphDilate(const imImage* src_image, imImage* dst_image, int kernel_size) { imImage* kernel = imImageCreate(kernel_size, kernel_size, IM_GRAY, IM_INT); imImageSetAttribute(kernel, "Description", IM_BYTE, -1, (void*)"Dilate"); // Kernel is all zeros int ret = imProcessGrayMorphConvolve(src_image, dst_image, kernel, 1); imImageDestroy(kernel); return ret; }
void imProcessRemoveByArea(const imImage* image, imImage* NewImage, int connect, int start_size, int end_size, int inside) { imImage *region_image = imImageCreate(image->width, image->height, IM_GRAY, IM_USHORT); if (!region_image) return; int region_count = imAnalyzeFindRegions(image, region_image, connect, 1); if (!region_count) { imImageClear(NewImage); imImageDestroy(region_image); return; } if (end_size == 0) end_size = image->width*image->height; int outside; if (inside) { /* remove from inside */ inside = 0; outside = 1; } else { /* remove from outside */ inside = 1; outside = 0; } int* area_data = (int*)malloc(region_count*sizeof(int)); imAnalyzeMeasureArea(region_image, area_data, region_count); imushort* region_data = (imushort*)region_image->data[0]; imbyte* img_data = (imbyte*)NewImage->data[0]; for (int i = 0; i < image->count; i++) { if (*region_data) { int area = area_data[(*region_data) - 1]; if (area < start_size || area > end_size) *img_data = (imbyte)outside; else *img_data = (imbyte)inside; } else *img_data = 0; region_data++; img_data++; } free(area_data); imImageDestroy(region_image); }
int imProcessGrayMorphConvolve(const imImage* src_image, imImage* dst_image, const imImage *kernel, int ismax) { int ret = 0; int counter = imProcessCounterBegin("Gray Morphological Convolution"); const char* msg = (const char*)imImageGetAttribute(kernel, "Description", NULL, NULL); if (!msg) msg = "Processing..."; imCounterTotal(counter, src_image->depth*src_image->height, msg); imImage* fkernel = NULL; if ((src_image->data_type == IM_FLOAT || src_image->data_type == IM_DOUBLE) && kernel->data_type != src_image->data_type) { fkernel = imImageCreate(kernel->width, kernel->height, IM_GRAY, src_image->data_type); imProcessConvertDataType(kernel, fkernel, 0, 0, 0, IM_CAST_DIRECT); kernel = fkernel; } for (int i = 0; i < src_image->depth; i++) { switch(src_image->data_type) { case IM_BYTE: ret = DoGrayMorphConvolve((imbyte*)src_image->data[i], (imbyte*)dst_image->data[i], src_image->width, src_image->height, kernel, counter, ismax, (int)0); break; case IM_SHORT: ret = DoGrayMorphConvolve((short*)src_image->data[i], (short*)dst_image->data[i], src_image->width, src_image->height, kernel, counter, ismax, (int)0); break; case IM_USHORT: ret = DoGrayMorphConvolve((imushort*)src_image->data[i], (imushort*)dst_image->data[i], src_image->width, src_image->height, kernel, counter, ismax, (int)0); break; case IM_INT: ret = DoGrayMorphConvolve((int*)src_image->data[i], (int*)dst_image->data[i], src_image->width, src_image->height, kernel, counter, ismax, (int)0); break; case IM_FLOAT: ret = DoGrayMorphConvolve((float*)src_image->data[i], (float*)dst_image->data[i], src_image->width, src_image->height, kernel, counter, ismax, (float)0); break; case IM_DOUBLE: ret = DoGrayMorphConvolve((double*)src_image->data[i], (double*)dst_image->data[i], src_image->width, src_image->height, kernel, counter, ismax, (double)0); break; } if (!ret) break; } if (fkernel) imImageDestroy(fkernel); imProcessCounterEnd(counter); return ret; }
void check_new_file(Ihandle* dlg) { Ihandle* canvas = IupGetDialogChild(dlg, "CANVAS"); imImage* image = (imImage*)IupGetAttribute(canvas, "IMAGE"); if (!image) { Ihandle* config = (Ihandle*)IupGetAttribute(canvas, "CONFIG"); int width = IupConfigGetVariableIntDef(config, "NewImage", "Width", 640); int height = IupConfigGetVariableIntDef(config, "NewImage", "Height", 480); image = imImageCreate(width, height, IM_RGB, IM_BYTE); new_file(dlg, image); } }
/*****************************************************************************\ im.ImageCreate(width, height, color_space, data_type) \*****************************************************************************/ static int imluaImageCreate (lua_State *L) { int width = luaL_checkint(L, 1); int height = luaL_checkint(L, 2); int color_space = luaL_checkint(L, 3); int data_type = luaL_checkint(L, 4); imImage *image; if (!imImageCheckFormat(color_space, data_type)) luaL_error(L, "invalid combination of color space and data type."); image = imImageCreate(width, height, color_space, data_type); imlua_pushimage(L, image); return 1; }
static int bt1_cb(Ihandle* self) { imImage* image; int w, h; void* gldata; int ii = tabs_get_index(); IupGetIntInt(plot[ii], "DRAWSIZE", &w, &h); gldata = malloc(w*h*3); image = imImageCreate(w, h, IM_RGB, IM_BYTE); IupMglPlotPaintTo(plot[ii], "RGB", w, h, 0, gldata); imConvertPacking(gldata, image->data[0], w, h, 3, 3, IM_BYTE, 1); imProcessFlip(image, image); imFileImageSave("../mglplot.png", "PNG", image); free(gldata); imImageDestroy(image); (void)self; return IUP_DEFAULT; }
imImage* imImageCreateFromOpenGLData(int width, int height, int glformat, const void* gldata) { int color_space, has_alpha, depth; imImage* image; switch(glformat) { case GL_RGBA: color_space = IM_RGB; has_alpha = 1; depth = 4; break; case GL_RGB: color_space = IM_RGB; has_alpha = 0; depth = 3; break; case GL_LUMINANCE_ALPHA: color_space = IM_GRAY; depth = 2; has_alpha = 1; case GL_LUMINANCE: color_space = IM_GRAY; depth = 1; has_alpha = 0; break; default: return NULL; } image = imImageCreate(width, height, color_space, IM_BYTE); if (!image) return NULL; if (has_alpha) imImageAddAlpha(image); imConvertPacking(gldata, image->data[0], image->width, image->height, depth, depth, IM_BYTE, 1); return image; }
int item_new_action_cb(Ihandle* item_new) { if (save_check(item_new)) { Ihandle* canvas = IupGetDialogChild(item_new, "CANVAS"); Ihandle* config = (Ihandle*)IupGetAttribute(canvas, "CONFIG"); int width = IupConfigGetVariableIntDef(config, "NewImage", "Width", 640); int height = IupConfigGetVariableIntDef(config, "NewImage", "Height", 480); if (IupGetParam("New Image", NULL, NULL, "Width: %i[1,]\nHeight: %i[1,]\n", &width, &height, NULL)) { imImage* image = imImageCreate(width, height, IM_RGB, IM_BYTE); IupConfigSetVariableInt(config, "NewImage", "Width", width); IupConfigSetVariableInt(config, "NewImage", "Height", height); new_file(item_new, image); } } return IUP_DEFAULT; }
int imConvertToBitmap(const imImage* src_image, imImage* dst_image, int cpx2real, float gamma, int absolute, int cast_mode) #endif { assert(src_image); assert(dst_image); if (!imImageMatchSize(src_image, dst_image) || !imImageIsBitmap(dst_image)) return IM_ERR_DATA; #ifdef IM_PROCESS int counter = imProcessCounterBegin("Building Bitmap"); #else int counter = imCounterBegin("Building Bitmap"); #endif int ret; if (src_image->data_type == IM_BYTE) { // NO data type conversion, only color mode conversion #ifdef IM_PROCESS ret = imProcessConvertColorSpace(src_image, dst_image); #else ret = imConvertColorSpace(src_image, dst_image); #endif } else { if (src_image->color_space == IM_RGB || src_image->color_space == IM_GRAY) { // data type conversion, but NO color mode conversion #ifdef IM_PROCESS ret = imProcessConvertDataType(src_image, dst_image, cpx2real, gamma, absolute, cast_mode); #else ret = imConvertDataType(src_image, dst_image, cpx2real, gamma, absolute, cast_mode); #endif } else { // data type conversion AND color mode conversion imImage* temp_image = imImageCreate(src_image->width, src_image->height, dst_image->color_space, src_image->data_type); if (!temp_image) ret = IM_ERR_MEM; else { // first convert color_mode in the bigger precision #ifdef IM_PROCESS ret = imProcessConvertColorSpace(src_image, temp_image); #else ret = imConvertColorSpace(src_image, temp_image); #endif if (ret == IM_ERR_NONE) { // second just convert data type #ifdef IM_PROCESS ret = imProcessConvertDataType(temp_image, dst_image, cpx2real, gamma, absolute, cast_mode); #else ret = imConvertDataType(temp_image, dst_image, cpx2real, gamma, absolute, cast_mode); #endif } imImageDestroy(temp_image); } } } #ifdef IM_PROCESS imProcessCounterEnd(counter); #else imCounterEnd(counter); #endif return ret; }
void imAnalyzeMeasureHoles(const imImage* image, int connect, int* count_data, int* area_data, float* perim_data) { int i; imImage *inv_image = imImageCreate(image->width, image->height, IM_BINARY, IM_BYTE); imbyte* inv_data = (imbyte*)inv_image->data[0]; imushort* img_data = (imushort*)image->data[0]; // finds the holes in the inverted image for (i = 0; i < image->count; i++) { if (*img_data) *inv_data = 0; else *inv_data = 1; img_data++; inv_data++; } imImage *holes_image = imImageClone(image); if (!holes_image) return; int holes_count = imAnalyzeFindRegions(inv_image, holes_image, connect, 0); imImageDestroy(inv_image); if (!holes_count) { imImageDestroy(holes_image); return; } // measure the holes area int* holes_area = (int*)malloc(holes_count*sizeof(int)); imAnalyzeMeasureArea(holes_image, holes_area, holes_count); float* holes_perim = 0; if (perim_data) { holes_perim = (float*)malloc(holes_count*sizeof(int)); imAnalyzeMeasurePerimeter(holes_image, holes_perim, holes_count); } imushort* holes_data = (imushort*)holes_image->data[0]; img_data = (imushort*)image->data[0]; // holes do not touch the border for (int y = 1; y < image->height-1; y++) { int offset_up = (y+1)*image->width; int offset = y*image->width; int offset_dw = (y-1)*image->width; for (int x = 1; x < image->width-1; x++) { int hole_index = holes_data[offset+x]; if (hole_index && holes_area[hole_index-1]) // a hole not yet used { // if the hole has not been used, // it is the first time we encounter a pixel of this hole. // then it is a pixel from the hole border. // now find which region this hole is inside. // a 4 connected neighbour is necessarilly a valid region or 0. int region_index = 0; if (img_data[offset_up + x]) region_index = img_data[offset_up + x]; else if (img_data[offset + x+1]) region_index = img_data[offset + x+1]; else if (img_data[offset + x-1]) region_index = img_data[offset + x-1]; else if (img_data[offset_dw+x]) region_index = img_data[offset_dw+x]; if (!region_index) continue; if (count_data) count_data[region_index-1]++; if (area_data) area_data[region_index-1] += holes_area[hole_index-1]; if (perim_data) perim_data[region_index-1] += holes_perim[hole_index-1]; holes_area[hole_index-1] = 0; // mark hole as used } } } if (holes_perim) free(holes_perim); free(holes_area); imImageDestroy(holes_image); }