int main(int argc, char *argv[] )
{
    ros::init(argc, argv, "histogram_segmentation"); //initialize with a default name
    ros::NodeHandle nh("~"); //use node name as sufix of the namespace
    nh.param<int>("threshold", threshold, 254);
    nh.param<int>("dilate_iterations", dilate_iterations, 9);
    nh.param<int>("dilate_size", dilate_size, 7);
    nh.param<int>("erode_iterations", erode_iterations, 0);
    nh.param<int>("erode_size", erode_size, 3);

    if(argc < 2)
    {
        ROS_ERROR("Histogram segmentation needs a template to search for. Please provide it as a command line argument.");
        return -1;
    }

    std::string template_path(argv[1]);
    ROS_INFO("%s", template_path.c_str());
    cv::Mat raw_template = cv::imread(template_path);
    target_hist = pal_vision_util::calcHSVHist(raw_template);
    cv::normalize(target_hist, target_hist, 0, 255, cv::NORM_MINMAX, -1, cv::Mat());

    image_transport::ImageTransport it(nh);
    image_transport::Subscriber rect_sub = it.subscribe("/image", 1, &imageCb);
    image_pub = it.advertise("image_masked", 1);
    mask_pub = it.advertise("mask", 1);
    debug_pub = it.advertise("debug",1);

    dynamic_reconfigure::Server<pal_vision_segmentation::HistogramSegmentConfig> server;
    dynamic_reconfigure::Server<pal_vision_segmentation::HistogramSegmentConfig>::CallbackType f;
    f = boost::bind(&reconf_callback, _1, _2);
    server.setCallback(f);

    ros::spin();
}
int main(int argc, char **argv)
{
    
    
    /**********************************/
    /*        Program options         */
    /**********************************/
    // Need to specify elevation grid
    // Need to specify channel
    std::string map_file;
    std::string template_file;
    std::string output_file_depth;
    std::string output_file_proportion;
    
    bool ignore_na = false;
    bool do_max = false;
    bool majority = false;
    //
    
    namespace prog_opt = boost::program_options;
    namespace fs = boost::filesystem;
    namespace raster_util = blink::raster;
    
    prog_opt::options_description desc("Allowed options");
    desc.add_options()
    ("help,h", "produce help message")
    ("map,m", prog_opt::value<std::string>(&map_file), "path of the gdal capatible raster for aggregating")
    ("template,t", prog_opt::value<std::string>(&template_file), "path of the gdal capatible raster which works as a template (output raster will have same extent and cell size as template)")
    ("depth-out,d", prog_opt::value<std::string>(&output_file_depth)->default_value("aggregated-depth.tif"), "path where the output gtif raster of depth is saved")
    ("proportion-out,p", prog_opt::value<std::string>(&output_file_proportion)->default_value("aggregated-proportion.tif"), "path where the output gtif raster of proportion flooded is saved")
    ("maximum,x", "use maximum statistic, rather than average")
    ("majority,a", "only assign value if majority of cells are not noValue")
    ("ignore-na,i", "ignore pixels with nodata when taking statistic");
    prog_opt::variables_map vm;
    prog_opt::store(prog_opt::parse_command_line(argc, argv, desc), vm);
    prog_opt::notify(vm);
    if (vm.count("help"))
    {
        std::cout << desc << "\n";
        return 1;
    }
    if (vm.count("maximum"))
    {
        do_max = true;
    }
    if (vm.count("ignore-na"))
    {
        ignore_na = true;
    }
    if (vm.count("majority"))
    {
        majority = true;
    }
    
    fs::path map_path(map_file);
    fs::path template_path(template_file);
    fs::path output_path_depth(output_file_depth);
    fs::path output_path_proportion(output_file_proportion);
    
    
    // Check file exists
    if (!fs::exists(map_path))
    {
        std::stringstream ss;
        ss << map_path << " does not exist";
        throw std::runtime_error(ss.str());
        return (EXIT_FAILURE);
    }
    
    if (!fs::exists(template_path))
    {
        std::stringstream ss;
        ss << template_path << " does not exist";
        throw std::runtime_error(ss.str());
        return (EXIT_FAILURE);
    }
    
    
    /**********************************/
    /*         Read in maps           */
    /**********************************/
    std::cout << "\n\n*************************************\n";
    std::cout <<     "*             Read in maps          *\n";
    std::cout <<     "*************************************" << std::endl;
    auto template_m = raster_util::open_gdal_raster<double>(template_path, GA_ReadOnly);
    
    auto map = raster_util::open_gdal_raster<double>(map_path, GA_ReadOnly);
    
    
    /********************************************/
    /*       Assign memory for output map       */
    /********************************************/
    std::cout << "\n\n**************************************\n";
    std::cout <<     "*    Assign memory for output maps   *\n";
    std::cout <<     "**************************************" << std::endl;
    auto output_map_depth = raster_util::create_gdal_raster_from_model<double>(output_path_depth, template_m);
    auto output_map_proportion = raster_util::create_gdal_raster_from_model<double>(output_path_proportion, template_m);
    
    
    aggregateMaps(map, template_m, output_map_depth, output_map_proportion, ignore_na, do_max, majority);
    
    return (EXIT_SUCCESS);
}
Ejemplo n.º 3
0
void
FolderShaper::MoveCopyTemplate	(entry_ref * a_template_ref, bool a_move)
{
	BEntry		template_entry;
	
	template_entry.SetTo	(a_template_ref, true);			// traverse, if link (future proof)
	template_entry.GetRef	(a_template_ref);
	BString template_name =	a_template_ref->name;				// name of dropped template
	BPath	template_path	(& template_entry);
	
	BString template_path_string = template_path.Path();		// path of dropped template
	NameSafe(& template_path_string);

	BPath	templates_folder_path	(m_tmpl_dir, NULL);			// path of template folder
	BString	templates_folder_string	=	templates_folder_path.Path();
	//NameSafe(& templates_folder_string);	// not yet, we want the new name in first

	// create a new name that is unique in the Template folder
	BString new_target_name	=	a_template_ref->name;			

	int32	counter	=	2;
	m_tmpl_dir->Rewind();
	
	while(m_tmpl_dir->Contains(new_target_name.String()))
	{
		new_target_name = a_template_ref->name;
		new_target_name.Append(" ");
		new_target_name << counter;
		counter	++;
	}

	BString	target_template_path_string	=	templates_folder_string;
	target_template_path_string.	Append("/");
	target_template_path_string.	Append(new_target_name);
	NameSafe(& target_template_path_string);
	
	//	 DEBUG
	PRINT(("template_name: %s\n", template_name.String()));
	PRINT(("new_target_name: %s\n", new_target_name.String()));	
	
	PRINT(("template_path_string: %s\n", template_path_string.String()));	
	PRINT(("templates_folder_string: %s\n", templates_folder_string.String()));	
	PRINT(("target_template_path_string: %s\n", target_template_path_string.String()));	

	if (a_move)	// Move
	{
		// find a temp name that's in neither of the source/target folders
		BString unique_name = a_template_ref->name;	 
		BDirectory	source_dir(templates_folder_string.String());
		
		counter = 2;
		while(	source_dir.Contains(unique_name.String())
				||
				m_tmpl_dir->Contains(unique_name.String())	)
		{
			unique_name = a_template_ref->name;
			unique_name.Append(" ");
			unique_name << counter;
			counter	++;
		}
		
		PRINT(("unique_name: %s\n", unique_name.String()));
		
		template_entry.Rename(unique_name.String());
		template_entry.MoveTo(m_tmpl_dir);
		
	}
	else		// Copy
	{
		BString command = "copyattr -d -r ";
		command.Append(template_path_string);
		command.Append(" ");
		command.Append(target_template_path_string);
		system(command.String());
	}
}
Ejemplo n.º 4
0
banner_t * banner_new_from_file(const char *filename,
        int *num_options, cups_option_t **options)
{
    FILE *f;
    char *line = NULL;
    size_t len = 0;
    int linenr = 0;
    banner_t *banner = NULL;

    if (!strcmp(filename, "-"))
        f = stdin;
    else if (!(f = fopen(filename, "r"))) {
        perror("Error opening banner file");
        goto out;
    }

    if (getline(&line, &len, f) == -1 ||
        strncmp(line, "#PDF-BANNER", 11) != 0)
        goto out;

    banner = calloc(1, sizeof *banner);

    while (getline(&line, &len, f) != -1) {
        char *key, *value;

        linenr++;
        if (!parse_line(line, &key, &value))
            continue;

        if (!value) {
            fprintf(stderr, "error: line %d is missing a value\n", linenr);
            continue;
        }

        if (!strcasecmp(key, "template"))
            banner->template_file = template_path(value);
        else if (!strcasecmp(key, "header"))
            banner->header = strdup(value);
        else if (!strcasecmp(key, "footer"))
            banner->header = strdup(value);
        else if (!strcasecmp(key, "font")) {
            *num_options = cupsAddOption("banner-font",
                    strdup(value), *num_options, options);
        }
        else if (!strcasecmp(key, "font-size")) {
            *num_options = cupsAddOption("banner-font-size",
                    strdup(value), *num_options, options);
        }
        else if (!strcasecmp(key, "show"))
            banner->infos = parse_show(value);
        else if (!strcasecmp(key, "image") ||
                 !strcasecmp(key, "notice"))
            fprintf(stderr,
                    "note:%d: bannertopdf does not support '%s'\n",
                    linenr, key);
        else
            fprintf(stderr,
                    "error:%d: unknown keyword '%s'\n",
                    linenr, key);
    }

    /* load default template if none was specified */
    if (!banner->template_file)
        banner->template_file = template_path ("default.pdf");

out:
    free(line);
    if (f)
        fclose(f);
    return banner;
}
Ejemplo n.º 5
0
status_t 
FolderShaper::ShapeRef	(entry_ref * a_ref, const char * a_template, bool a_do_open)
{
	PRINT(("ShapeRef(%s, %s)\n", a_ref->name, a_template));

	status_t	status;

// store original_name
	
	BEntry	original_entry	(a_ref, true);
	original_entry.	GetRef	(a_ref);
	BString original_name 	= a_ref->name;
	BPath	original_path	(& original_entry);
	
	BString original_path_string = original_path.Path();
	NameSafe(& original_path_string);

	BPath	original_parent_path;
	original_path.GetParent(& original_parent_path);

	BDirectory parent_dir(original_parent_path.Path());

//	temp name
	BString new_name	=	a_ref->name;
	new_name.Prepend("New ");

	int32	counter	=	2;
	while(parent_dir.Contains(new_name.String()))
	{
		new_name = a_ref->name;
		new_name.Append(" (temporary ");
		new_name << counter;
		new_name.Append(")");		
		counter	++;
	}

	PRINT(("original_name: %s\n", original_name.String()));
	PRINT(("new_name: %s\n", new_name.String()));	
	
	PRINT(("original_path: %s\n", original_path.Path()));	
	PRINT(("original_path_string: %s\n", original_path_string.String()));	
	PRINT(("original_parent_path: %s\n", original_parent_path.Path()));	

	
	BPath	template_path	(m_tmpl_dir, a_template);

	BEntry	tmp_entry;		// traverse link to template
	if ((status = tmp_entry.SetTo(template_path.Path(), true)) != B_OK)
	{
		// message
		return status;
	}
	template_path.SetTo(& tmp_entry);
	
	BString	template_path_string 	=	template_path.Path();
	NameSafe(& template_path_string);
	PRINT(("template_path_string: %s\n", template_path_string.String()));		

	BPath	new_path	(original_parent_path.Path(), new_name.String());
	BString	new_path_string	=	new_path.Path();
	NameSafe(& new_path_string);
	PRINT(("new_path_string: %s\n", new_path_string.String()));			

	BString command;

	if(m_do_keep_position)
	{
		//	copy X/Y position attribute from Original Folder

		command = "copyattr -n _trk/pinfo_le ";
		command.Append(original_path_string);
		command.Append(" ");
		command.Append(template_path_string);
		
		PRINT(("copy X/Y position: \n%s\n", command.String()));
	
		system(command.String());
	}
	else
	{
		BNode template_node(& tmp_entry);
		template_node.RemoveAttr("_trk/pinfo_le");
	}
		
	
//	copy template -> "New name" (temp)

	command = "copyattr -d -r ";
	command.Append(template_path_string);
	command.Append(" ");
	command.Append(new_path_string);
	
	PRINT(("copy template -> temp: \n%s\n", command.String()));
	
	system(command.String());
	
//	New folder exists?

	BDirectory	new_directory;
	if((status = new_directory.SetTo(new_path.Path())) != B_OK)
	{
		ErrorMessage("new_directory.SetTo() ", status);	// ... probably want a BAlert
		return status;
	}
	
	BDirectory original_dir;
	if((status = original_dir.SetTo(original_path.Path())) != B_OK)
	{
		ErrorMessage("original_dir.SetTo() ", status);	// ... probably want a BAlert
		return status;
	}
	
//	Move stuff from original folder to new folder	
	BEntry	original_content_entry;
	while(original_dir.GetNextEntry(&original_content_entry) == B_OK)
	{
		if ((status = original_content_entry
			.MoveTo(& new_directory, NULL, ! m_do_clobber))	!= B_OK 
			&& (status != B_FILE_EXISTS || (! m_do_clobber) == true))
		{	
			ErrorMessage("original_content_entry.MoveTo() ", status);
			return status;							// ... definitely needs a fat BAlert
		}
	}

//	Move original folder to Trash
//	alt.
//  Delete

	bool was_open = false;
	
	if (m_do_move)
	{
		//	Is Folder Open ?
		was_open =	IsFolderOpen(a_ref);
		
		//	Close Original Folder	
		status	=	MoveRefToTrash(a_ref);	// This calls Tracker to move the ref.
											// If the folder is open, Tracker will close it.

		for (int j = 0; j < 250; j++)
		{
			PRINT(("loop: %d\n", j));
		
			if (original_entry.Exists() == false)
				break;
			
			snooze(20000);		// give Tracker some Time to Move
			// .... consider using node-monitoring instead to 
			// catch when Tracker has moved the folder to Trash
		}
	}
	else
	{
		status = original_entry.Remove();
		if (status != B_OK)
		{
			ErrorMessage("original_content_entry.MoveTo() ", status);
			// BAlert.. Could not remove original folder
			// return status;
		}
	}	
	
//	Rename New Folder -> Original Name
	BEntry	new_entry(new_path.Path());
	status = new_entry.Rename(original_name.String());
	if (status != B_OK)
		ErrorMessage("new_entry.Rename() ", status);
	
	entry_ref	new_ref;
	status = new_entry.GetRef(& new_ref);
	if (status != B_OK)
		ErrorMessage("new_entry.GetRef() ", status);
	
//	Open New Folder
	if (was_open) PRINT(("was_open == true\n"));
	if (! was_open) PRINT(("was_open == false\n"));
		
	if (((m_do_open == FS_IF_ORIGINAL_OPEN) && was_open) || m_do_open == FS_ALWAYS_OPEN)
	{
		// open folder
		command =	original_path_string;
		command.Prepend("/boot/system/Tracker ");
		command.Append(" &");
		
		system(command.String());
		
		// wait for new folder to open
		WaitForOpeningFolder(& new_ref);
		
		//	Clean Up All	
		if (m_do_clean_up)
			CleanUpWindow(& new_ref);
	}
	
	return B_OK;
}