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 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); }
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 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); }