Commit fc7b7d50 authored by Yann Jacquelet's avatar Yann Jacquelet
Browse files

Prepare kmean demo for theo. Work in progress.

	* green/mln/clustering/kmean1d.hh: New library file.

	* green/demo/clustering/kmean1d: New directory.
	* green/demo/clustering/kmean1d/Makefile.am: New makefile.
	* green/demo/clustering/kmean1d/kmean1d.cc: New demo.

	* green/tests/clustering/kmean1d: New directory.
	* green/tests/clustering/kmean1d/Makefile.am: New makefile.
	* green/tests/clustering/kmean1d/kmean1d.cc: New unitary tests.

git-svn-id: https://svn.lrde.epita.fr/svn/oln/trunk@4562 4aad255d-cdde-0310-9447-f3009e2ae8c0
parent dd520144
2009-09-28 Yann Jacquelet <jacquelet@lrde.epita.fr>
Prepare kmean demo for theo. Work in progress.
* green/mln/clustering/kmean1d.hh: New library file.
* green/demo/clustering/kmean1d: New directory.
* green/demo/clustering/kmean1d/Makefile.am: New makefile.
* green/demo/clustering/kmean1d/kmean1d.cc: New demo.
* green/tests/clustering/kmean1d: New directory.
* green/tests/clustering/kmean1d/Makefile.am: New makefile.
* green/tests/clustering/kmean1d/kmean1d.cc: New unitary tests.
2009-09-24 Fabien Freling <fabien.freling@lrde.epita.fr>
Add benchmark for disk-mapped image and work for color types.
......
#
# Generic Makefile
#
#########
# TOOLS #
#########
INCLUDES= -I$(HOME)/svn/oln/trunk/milena/sandbox/green
CXXFLAGS= -ggdb -O0 -Wall -W -pedantic -ansi -pipe $(INCLUDES)
ECHO= echo
RM= rm
MKDIR= mkdir -p
CP= cp
SOURCE_PATTERN= green/demo
BUILD__PATTERN= green/build/demo
ifeq ($(findstring $(BUILD__PATTERN),$(PWD)), $(BUILD__PATTERN))
# Case where make is done from build directory.
SOURCE_DIR= $(subst $(BUILD__PATTERN),$(SOURCE_PATTERN),$(PWD))
BUILD__DIR= $(PWD)/
else
# Case where make is done from source directory.
SOURCE_DIR= $(PWD)/
BUILD__DIR= $(subst $(SOURCE_PATTERN),$(BUILD__PATTERN),$(PWD))
endif
SRC= $(notdir $(wildcard $(SOURCE_DIR)/*.cc))
OLD= $(notdir $(wildcard $(SOURCE_DIR)/*~))
OBJ= $(patsubst %.cc,%.o,$(SRC))
SOURCE_MAKEFILE=Makefile.am
BUILD__MAKEFILE=Makefile
TARGET_FILE= $(notdir $(PWD))
SOURCE_FILES= $(notdir $(wildcard $(SOURCE_DIR)/*.*))
BUILD__FILES= $(filter-out $(SRC) $(SOURCE_MAKEFILE), $(SOURCE_FILES))
BUILD__F_PATH= $(addprefix $(BUILD__DIR)/,$(BUILD__FILES))
SOURCE_F_PATH= $(addprefix $(SOURCE_DIR)/,$(SOURCE_FILES))
BUILD__M_PATH= $(addprefix $(BUILD__DIR)/,$(BUILD__MAKEFILE))
SOURCE_M_PATH= $(addprefix $(SOURCE_DIR)/,$(SOURCE_MAKEFILE))
TARGET_F_PATH= $(addprefix $(BUILD__DIR)/,$(TARGET_FILE))
OBJ_F_PATH= $(addprefix $(BUILD__DIR)/,$(OBJ))
SRC_F_PATH= $(addprefix $(SOURCE_DIR)/,$(SRC))
OLD_F_PATH= $(addprefix $(SOURCE_DIR)/,$(OLD))
#############
# BOOTSTRAP #
#############
bootstrap: $(BUILD__DIR) $(BUILD__F_PATH) $(BUILD__M_PATH)
# Create, if nessary, the destination directory
$(BUILD__DIR):
$(MKDIR) $(BUILD__DIR)
# Copy, if nessary, all the files, except the Makefile.am
$(BUILD__F_PATH): $(SOURCE_F_PATH)
$(CP) $(addprefix $(SOURCE_DIR),$(@F)) $@
# Copy if nessary, the Makefile.am into Makefile
$(BUILD__M_PATH): $(SOURCE_M_PATH)
$(CP) $(SOURCE_M_PATH) $(BUILD__M_PATH)
#######
# ALL #
#######
# We assume that the call is done from the build directory.
# With the directive vpath, hidden files are found in the source directory.
all: $(TARGET_F_PATH)
$(TARGET_F_PATH): $(OBJ_F_PATH)
$(LINK.cc) $< $(LOADLIBES) $(LDLIBS) -o $@
$(OBJ_F_PATH):$(SRC_F_PATH)
$(COMPILE.cc) $(OUTPUT_OPTION) $<
#########
# CLEAN #
#########
# Force every time the deletion
clean: clean_target clean_obj clean_dst clean_old #clean_make
clean_target:
-@$(RM) $(TARGET_F_PATH) &> /dev/null
clean_obj:
-@$(RM) $(OBJ_F_PATH) &> /dev/null
clean_dst:
-@$(RM) $(BUILD_F_PATH) &> /dev/null
clean_make:
-@$(RM) $(BUILD_M_PATH) &> /dev/null
clean_old:
-@$(RM) $(OLD_F_PATH) &> /dev/null
#########
# PRINT #
#########
print: print_tools print_bootstrap
print_tools:
@$(ECHO) "HOME = $(HOME)"
@$(ECHO) "INCLUDES = $(INCLUDES)"
@$(ECHO) "CXXFLAGS = $(CXXFLAGS)"
@$(ECHO) "ECHO = $(ECHO)"
@$(ECHO) "RM = $(RM)"
@$(ECHO) "MKDIR = $(MKDIR)"
@$(ECHO) "CP = $(CP)"
@$(ECHO)
print_bootstrap:
@$(ECHO) "PWD = $(PWD)"
@$(ECHO) "SOURCE_PATTERN = $(SOURCE_PATTERN)"
@$(ECHO) "BUILD__PATTERN = $(BUILD__PATTERN)"
@$(ECHO) "SOURCE_DIR = $(SOURCE_DIR)"
@$(ECHO) "BUILD__DIR = $(BUILD__DIR)"
@$(ECHO) "SOURCE_MAKEFILE = $(SOURCE_MAKEFILE)"
@$(ECHO) "BUILD__MAKEFILE = $(BUILD__MAKEFILE)"
@$(ECHO) "TARGET_FILE = $(TARGET_FILE)"
@$(ECHO) "SOURCE_FILES = $(SOURCE_FILES)"
@$(ECHO) "SOURCE_F_PATH = $(SOURCE_F_PATH)"
@$(ECHO) "BUILD__FILES = $(BUILD__FILES)"
@$(ECHO) "BUILD__F_PATH = $(BUILD__F_PATH)"
@$(ECHO) "BUILD__M_PATH = $(BUILD__M_PATH)"
@$(ECHO) "SOURCE_M_PATH = $(SOURCE_M_PATH)"
@$(ECHO) "SRC = $(SRC)"
@$(ECHO) "OBJ = $(OBJ)"
@$(ECHO) "OLD = $(OLD)"
@$(ECHO) "SRC_F_PATH = $(SRC_F_PATH)"
@$(ECHO) "OBJ_F_PATH = $(OBJ_F_PATH)"
@$(ECHO) "OLD_F_PATH = $(OLD_F_PATH)"
@$(ECHO)
// DEMO ON KMEAN1D
#include <mln/clustering/kmean1d.hh>
#include <iostream>
#include <sstream>
#include <mln/img_path.hh>
#include <mln/pw/value.hh>
#include <mln/value/int_u8.hh>
#include <mln/value/label_8.hh>
#include <mln/value/rgb8.hh>
#include <mln/core/macros.hh>
#include <mln/core/image/image2d.hh>
#include <mln/core/image/dmorph/image_if.hh>
#include <mln/io/pgm/load.hh>
#include <mln/io/pgm/save.hh>
#include <mln/io/ppm/save.hh>
#include <mln/io/plot/save_image_sh.hh>
void demo(const unsigned watch_dog, const unsigned n_times)
{
typedef mln::clustering::kmean1d<double,8,3> t_kmean;
typedef mln::value::label8 t_label8;
typedef mln::value::rgb8 t_rgb8;
typedef mln::value::int_u8 t_int_u8;
typedef mln::image2d<int_u8> t_image2d_int_u8;
typedef mln::image2d<rgb8> t_image2d_rgb8;
t_image2d_int_u8 house;
mln::io::pgm::load(house, OLENA_IMG_PATH"/house.pgm");
t_kmean kmean(house, watch_dog, n_times);
kmean.launch_n_times();
t_kmean::t_color_dbg color_img = kmean.get_color_dbg();
t_kmean::t_mean_dbg mean_img = kmean.get_mean_dbg();
t_kmean::t_label_dbg label_img = kmean.get_label_dbg();
t_kmean::t_variance_cnv variance_cnv = kmean.get_variance_cnv();
t_kmean::t_mean_cnv mean_cnv = kmean.get_mean_cnv();
mln::io::pgm::save(mean_img, "mean.pgm");
mln::io::ppm::save(color_img, "color.pgm");
mln::io::pgm::save(label_img, "label.pgm");
mln::io::plot::save_image_sh(mean_cnv, "mean_cnv.sh");
mln::io::plot::save_image_sh(variance_cnv, "variance_cnv.sh");
}
void usage(const int argc, const char *args[])
{
std::cout << "----------------------------------------" << std::endl;
std::cout << "argc : " << argc << std::endl;
for (int i = 0; i < argc; ++i)
std::cout << "args[i] : " << args[i] << std::endl;
std::cout << "----------------------------------------" << std::endl;
std::cout << "usage: kmean1d watch_dog n_times k image" << std::endl;
std::cout << "unsigned watch_dog (convergence loop)" << std::endl;
std::cout << "unsigned n_times (number of launching)" << std::endl;
// std::cout << "unsigned k (number of centers)" << std::endl;
// std::cout << "pbm image (points to work with)" << std::endl;
std::cout << "----------------------------------------" << std::endl;
}
int main(const int argc, const char *args[])
{
if (3 == argc)
{
std::istringstream arg1(args[1]);
std::istringstream arg2(args[2]);
// std::istringstream arg3(args[3]);
// std::istringstream arg3(args[4]);
unsigned watch_dog;
unsigned n_times;
// const char *image;
// unsigned k;
arg1 >> watch_dog;
arg2 >> n_times;
if (!arg1.fail() && !arg2.fail())
demo(watch_dog, n_times);
else
usage(argc, args);
}
else
usage(argc, args);
return 0;
}
This diff is collapsed.
#
# Generic Makefile
#
#########
# TOOLS #
#########
INCLUDES= -I$(HOME)/svn/oln/trunk/milena/sandbox/green
CXXFLAGS= -ggdb -O0 -Wall -W -pedantic -ansi -pipe $(INCLUDES)
ECHO= echo
RM= rm
MKDIR= mkdir -p
CP= cp
SOURCE_PATTERN= green/tests
BUILD__PATTERN= green/build/tests
ifeq ($(findstring $(BUILD__PATTERN),$(PWD)), $(BUILD__PATTERN))
# Case where make is done from build directory.
SOURCE_DIR= $(subst $(BUILD__PATTERN),$(SOURCE_PATTERN),$(PWD))
BUILD__DIR= $(PWD)/
else
# Case where make is done from source directory.
SOURCE_DIR= $(PWD)/
BUILD__DIR= $(subst $(SOURCE_PATTERN),$(BUILD__PATTERN),$(PWD))
endif
SRC= $(notdir $(wildcard $(SOURCE_DIR)/*.cc))
OLD= $(notdir $(wildcard $(SOURCE_DIR)/*~))
OBJ= $(patsubst %.cc,%.o,$(SRC))
SOURCE_MAKEFILE=Makefile.am
BUILD__MAKEFILE=Makefile
TARGET_FILE= $(notdir $(PWD))
SOURCE_FILES= $(notdir $(wildcard $(SOURCE_DIR)/*.*))
BUILD__FILES= $(filter-out $(SRC) $(SOURCE_MAKEFILE), $(SOURCE_FILES))
BUILD__F_PATH= $(addprefix $(BUILD__DIR)/,$(BUILD__FILES))
SOURCE_F_PATH= $(addprefix $(SOURCE_DIR)/,$(SOURCE_FILES))
BUILD__M_PATH= $(addprefix $(BUILD__DIR)/,$(BUILD__MAKEFILE))
SOURCE_M_PATH= $(addprefix $(SOURCE_DIR)/,$(SOURCE_MAKEFILE))
TARGET_F_PATH= $(addprefix $(BUILD__DIR)/,$(TARGET_FILE))
OBJ_F_PATH= $(addprefix $(BUILD__DIR)/,$(OBJ))
SRC_F_PATH= $(addprefix $(SOURCE_DIR)/,$(SRC))
OLD_F_PATH= $(addprefix $(SOURCE_DIR)/,$(OLD))
#############
# BOOTSTRAP #
#############
bootstrap: $(BUILD__DIR) $(BUILD__F_PATH) $(BUILD__M_PATH)
# Create, if nessary, the destination directory
$(BUILD__DIR):
$(MKDIR) $(BUILD__DIR)
# Copy, if nessary, all the files, except the Makefile.am
$(BUILD__F_PATH): $(SOURCE_F_PATH)
$(CP) $(addprefix $(SOURCE_DIR),$(@F)) $@
# Copy if nessary, the Makefile.am into Makefile
$(BUILD__M_PATH): $(SOURCE_M_PATH)
$(CP) $(SOURCE_M_PATH) $(BUILD__M_PATH)
#######
# ALL #
#######
# We assume that the call is done from the build directory.
# With the directive vpath, hidden files are found in the source directory.
all: $(TARGET_F_PATH)
$(TARGET_F_PATH): $(OBJ_F_PATH)
$(LINK.cc) $< $(LOADLIBES) $(LDLIBS) -o $@
$(OBJ_F_PATH):$(SRC_F_PATH)
$(COMPILE.cc) $(OUTPUT_OPTION) $<
#########
# CLEAN #
#########
# Force every time the deletion
clean: clean_target clean_obj clean_dst clean_old #clean_make
clean_target:
-@$(RM) $(TARGET_F_PATH) &> /dev/null
clean_obj:
-@$(RM) $(OBJ_F_PATH) &> /dev/null
clean_dst:
-@$(RM) $(BUILD_F_PATH) &> /dev/null
clean_make:
-@$(RM) $(BUILD_M_PATH) &> /dev/null
clean_old:
-@$(RM) $(OLD_F_PATH) &> /dev/null
#########
# PRINT #
#########
print: print_tools print_bootstrap
print_tools:
@$(ECHO) "HOME = $(HOME)"
@$(ECHO) "INCLUDES = $(INCLUDES)"
@$(ECHO) "CXXFLAGS = $(CXXFLAGS)"
@$(ECHO) "ECHO = $(ECHO)"
@$(ECHO) "RM = $(RM)"
@$(ECHO) "MKDIR = $(MKDIR)"
@$(ECHO) "CP = $(CP)"
@$(ECHO)
print_bootstrap:
@$(ECHO) "PWD = $(PWD)"
@$(ECHO) "SOURCE_PATTERN = $(SOURCE_PATTERN)"
@$(ECHO) "BUILD__PATTERN = $(BUILD__PATTERN)"
@$(ECHO) "SOURCE_DIR = $(SOURCE_DIR)"
@$(ECHO) "BUILD__DIR = $(BUILD__DIR)"
@$(ECHO) "SOURCE_MAKEFILE = $(SOURCE_MAKEFILE)"
@$(ECHO) "BUILD__MAKEFILE = $(BUILD__MAKEFILE)"
@$(ECHO) "TARGET_FILE = $(TARGET_FILE)"
@$(ECHO) "SOURCE_FILES = $(SOURCE_FILES)"
@$(ECHO) "SOURCE_F_PATH = $(SOURCE_F_PATH)"
@$(ECHO) "BUILD__FILES = $(BUILD__FILES)"
@$(ECHO) "BUILD__F_PATH = $(BUILD__F_PATH)"
@$(ECHO) "BUILD__M_PATH = $(BUILD__M_PATH)"
@$(ECHO) "SOURCE_M_PATH = $(SOURCE_M_PATH)"
@$(ECHO) "SRC = $(SRC)"
@$(ECHO) "OBJ = $(OBJ)"
@$(ECHO) "OLD = $(OLD)"
@$(ECHO) "SRC_F_PATH = $(SRC_F_PATH)"
@$(ECHO) "OBJ_F_PATH = $(OBJ_F_PATH)"
@$(ECHO) "OLD_F_PATH = $(OLD_F_PATH)"
@$(ECHO)
// UNARY TESTS ON KMEAN1D
#include <mln/clustering/kmean1d.hh>
#include <iostream>
#include <mln/pw/value.hh>
#include <mln/value/int_u8.hh>
#include <mln/value/rgb8.hh>
#include <mln/literal/colors.hh>
#include <mln/algebra/vec.hh>
#include <mln/algebra/mat.hh>
#include <mln/core/macros.hh>
#include <mln/core/contract.hh>
#include <mln/core/image/image2d.hh>
#include <mln/core/image/dmorph/image_if.hh>
#include <mln/io/ppm/load.hh>
#include <mln/io/pgm/load.hh>
#include <mln/io/pgm/save.hh>
#include <mln/io/ppm/save.hh>
#include <mln/img_path.hh>
#include <mln/data/transform.hh>
#include <mln/trait/value/print.hh>
#include <mln/trait/image/print.hh>
#define SIZE_IMAGE 512
#define SIZE_SAMPLE1 (512*512)
#define SIZE_SAMPLE2 4
#define NB_CENTER 2
#define DIM_POINT 3
#define TYPE_POINT double
/*
void test_instantiation()
{
typedef mln::value::int_u8 int_u8;
typedef mln::image2d<int_u8> image2d_int_u8;
image2d_int_u8 house;
mln::io::pgm::load(house, OLENA_IMG_PATH"/house.pgm");
mln::clustering::kmean1d<double, 8, 3> kmean(house);
// test the compilation
std::cout << "test instantiation : ok" << std::endl;
}
void test_init_center()
{
typedef mln::value::int_u8 int_u8;
typedef mln::image2d<int_u8> image2d_int_u8;
image2d_int_u8 house;
mln::io::pgm::load(house, OLENA_IMG_PATH"/house.pgm");
mln::clustering::kmean1d<double, 8, 3> kmean(house);
kmean.init_center();
std::cout << "test init center : ok" << std::endl;
}
*/
void test_init_loop()
{
typedef mln::value::int_u8 int_u8;
typedef mln::image2d<int_u8> image2d_int_u8;
image2d_int_u8 house;
mln::io::pgm::load(house, OLENA_IMG_PATH"/house.pgm");
mln::clustering::kmean1d<double, 8, 3> kmean(house);
kmean.launch_n_times();
std::cout << "test loop : ok" << std::endl;
}
/*
struct rgb8_to_4colors : mln::Function_v2v<rgb8_to_4colors>
{
typedef mln::value::rgb8 result;
mln::value::rgb8 operator()(const mln::value::rgb8& color) const
{
mln::value::rgb8 result;
unsigned hash = (color.red() + color.green() + color.blue()) % 4;
switch (hash)
{
case 0: result = mln::literal::lime; break;
case 1: result = mln::literal::brown; break;
case 2: result = mln::literal::teal; break;
case 3: result = mln::literal::purple; break;
}
return result;
}
};
void print_color(const mln::value::rgb8& color)
{
std::cout << "{r=" << color.red() << ", ";
std::cout << "g=" << color.green() << ", ";
std::cout << "b=" << color.blue() << "}" << std::endl;
}
void fill_image_with_4colors(mln::image2d<mln::value::rgb8>& img)
{
const mln::value::rgb8 lime = mln::literal::lime;
const mln::value::rgb8 brown = mln::literal::brown;
const mln::value::rgb8 teal = mln::literal::teal;
const mln::value::rgb8 purple = mln::literal::purple;
img = mln::data::transform(img, rgb8_to_4colors());
//print_color(lime);
//print_color(brown);
//print_color(teal);
//print_color(purple);
}
bool is_equivalent(const mln::image2d<mln::value::rgb8>& img,
const MAT_POINT1& point)
{
mln_piter_(mln::image2d<mln::value::rgb8>) pi(img.domain());
bool result = true;
unsigned index = -1;
for_all(pi)
{
bool test = true;
++index;
test = test && (point(index,0) == img(pi).red());
test = test && (point(index,1) == img(pi).green());
test = test && (point(index,2) == img(pi).blue());
if (!test)
{
std::cout << pi;
std::cout << "{r=" << img(pi).red() << ", ";
std::cout << "g=" << img(pi).green() << ", ";
std::cout << "b=" << img(pi).blue() << "}";
std::cout << index;
std::cout << "[r=" << point(index,0) << ", ";
std::cout << "g=" << point(index,1) << ", ";
std::cout << "b=" << point(index,2) << "]" << std::endl;
mln_assertion(test);
}
result &= test;
}
return result;
}
void test_init_point()
{
typedef mln::value::rgb8 rgb8;
mln::image2d<rgb8> img_ref;
mln::clustering::k_mean<SIZE_SAMPLE1,NB_CENTER, DIM_POINT, TYPE_POINT> kmean;
mln::io::ppm::load(img_ref, "/usr/local/share/olena/images/lena.ppm");
//mln::io::ppm::save(img_ref, "verif.ppm");
fill_image_with_4colors(img_ref);
kmean.init_point(img_ref);
mln_assertion(true == is_equivalent(img_ref, kmean.get_point()));
std::cout << "Test init point : ok" << std::endl;
}
void set_point(MAT_POINT2& point,
const unsigned index,
const mln::value::rgb8& color)
{
point(index,0) = color.red();