population_sacha/Small_functions.cpp
// Conditionally replacing one color by another. Condition: surface area

Conditionally replacing one color by another. Condition: surface area
Conditionally replacing one color by another . Condition : surface area
void conditionally_replace_color_1_by_color_2(Mat2UI8 &img_original, unsigned char old_color, unsigned char new_color,
						   float area_threshold)
{
	int i;
	int j;
	int label;
	int number_of_labels;
	Mat2UI8 bl_img_old_color_only;
	Mat2UI32 region_labels;
	VecF64 region_areas;
	VecI32 bl_labels_to_replace;
	log_steps("Conditionally replacing one color by another. Condition: surface area");
	bl_img_old_color_only = Mat2UI8 (img_original.sizeI(),img_original.sizeJ());
	bl_img_old_color_only.fill (int_black_pixel_color);
	for(int i=0;i<img_original.sizeI();i++)
	{
		for(int j=0;j<img_original.sizeJ();j++)
		{
			if(img_original(i,j)==old_color)
			{
				bl_img_old_color_only(i,j) = true;
			}
		}
	}
	region_labels = pop::Processing::clusterToLabel (bl_img_old_color_only, 1);	
	number_of_labels = pop::Analysis::maxValue (region_labels)+1;	
	region_areas = pop::Analysis::areaByLabel (region_labels);	
	bl_labels_to_replace = VecI32 (number_of_labels, false);
	for (label=1; label<number_of_labels; label++)
	{
		if (region_areas(label-1) < area_threshold)		
			bl_labels_to_replace(label)=true;
	}
	for(int i=0;i<img_original.sizeI();i++)
	{
		for(int j=0;j<img_original.sizeJ();j++)
		{
			label=region_labels(i,j);
			if(bl_labels_to_replace(label))
				img_original(i,j)=new_color;
		}
	}
	log_steps();
}
void conditionally_replace_color_1_by_color_2 ( Mat2UI8 & img_original , unsigned char old_color , unsigned char new_color , float area_threshold ) { int i ; int j ; int label ; int number_of_labels ; Mat2UI8 bl_img_old_color_only ; Mat2UI32 region_labels ; VecF64 region_areas ; VecI32 bl_labels_to_replace ; log_steps ( " " ) ; bl_img_old_color_only = Mat2UI8 ( img_original . sizeI ( ) , img_original . sizeJ ( ) ) ; bl_img_old_color_only . fill ( int_black_pixel_color ) ; for ( int i = 0 ; i < img_original . sizeI ( ) ; i ++ ) { for ( int j = 0 ; j < img_original . sizeJ ( ) ; j ++ ) { if ( img_original ( i , j ) == old_color ) { bl_img_old_color_only ( i , j ) = true ; } } } region_labels = pop :: Processing :: clusterToLabel ( bl_img_old_color_only , 1 ) ; number_of_labels = pop :: Analysis :: maxValue ( region_labels ) + 1 ; region_areas = pop :: Analysis :: areaByLabel ( region_labels ) ; bl_labels_to_replace = VecI32 ( number_of_labels , false ) ; for ( label = 1 ; label < number_of_labels ; label ++ ) { if ( region_areas ( label - 1 ) < area_threshold ) bl_labels_to_replace ( label ) = true ; } for ( int i = 0 ; i < img_original . sizeI ( ) ; i ++ ) { for ( int j = 0 ; j < img_original . sizeJ ( ) ; j ++ ) { label = region_labels ( i , j ) ; if ( bl_labels_to_replace ( label ) ) img_original ( i , j ) = new_color ; } } log_steps ( ) ; }
Conditionally replacing one color by another.
Conditionally replacing one color by another .
// Declaration
 // Initialization
 // Copying only the required color in a matrix to be able to analyze the surfaces
 // Calculating the areas of the regions to be able to apply the area threshold
 // Numerates from left to right, from top to bottom. Labels are counted from 0
 // Including the label 0
 // Calculate the area according to region_labels. Areas are counted from 1. Very confusing
 // Marking the labels that should be replaced by analyzing regions areas
 // -1 because label 1 is the 0-th element
 // Replacing the color in the original image
