<?xml version="1.0" encoding="UTF-8"?>
<hdevelop file_version="1.1" halcon_version="13.0">
<procedure name="main">
<interface/>
<body>
<c> * Copyright (c) MVTec Software GmbH.  All rights reserved.</c>
<c> *</c>
<c> * All use of this software is subject to the terms of the revised BSD</c>
<c> * license.  You should have received a copy of this license along</c>
<c> * with this source code in a file named "license.txt."</c>
<c></c>
<c>***********************************************************</c>
<c>* Display, read, write theoretical trackers for a sequence.</c>
<c>*</c>
<c>* Currently works or DAVIS and vot2016 datasets</c>
<c>*************************************</c>
<c></c>
<c>*******************************************************</c>
<c>* Setup Filenames and Flags</c>
<c>*******************************************************</c>
<c>* Set the path (relative or absolut) to the ROOT of VOT or DAVIS data here</c>
<c>* Set the location of the images relative to the ROOT path here</c>
<c>* for  DAVIS '480p' and '1080p' are possible</c>
<l>ImageFolder := '../../data/DAVIS/DAVIS/JPEGImages/' + '480p'</l>
<l>* ImageFolder :=  '../../data/votseg/images'</l>
<c></c>
<c>* Set the name of groundtruth file</c>
<l>FileName := 'tracker_boxes.csv'</l>
<c></c>
<c>* FLAG if the theoretical trackers should be generated or read from disk</c>
<l>gen_theoretical_tracker := false</l>
<c></c>
<c>* FLAGS to determine which tracker should be written to the csv file</c>
<l>write_theoretical_no_scale := false</l>
<l>write_theoretical_no_rotation := false</l>
<l>write_theoretical_rect2 := false</l>
<l>write_theoretical_smallest_rectangle1 := false</l>
<c></c>
<c>* FLAG to switch the display on and off </c>
<l>Display := true</l>
<c></c>
<c>*</c>
<c>* The display colors for the different theoretical tracker:</c>
<l>NoScaleColor := '#ffff00'</l>
<l>NoRotColor := '#f08080'</l>
<l>Rect2Color := '#0000ff'</l>
<l>SmallestRect1Color := '#ff0000'</l>
<c>*******************************************************</c>
<c>* Walk through the directory</c>
<c>*******************************************************</c>
<l>dev_update_off ()</l>
<l>dev_close_window ()</l>
<l>dev_open_window (0, 0, 512, 512, 'black', WindowHandle)</l>
<l>dev_open_window (0, 705, 300, 300, 'black', WindowHandle1)</l>
<c>* To prevent regions being clipped at the image border, we set </c>
<c>* 'clip_region' to false</c>
<l>get_system ('clip_region', ClipRegion)</l>
<l>set_system ('clip_region', 'false')</l>
<l>ColSeparator := ';'</l>
<l>StringQuote  := '"'</l>
<c>*</c>
<l>list_files (ImageFolder, 'directories', ImageFolders)</l>
<l>for Idx := 0 to |ImageFolders|-1 by 1</l>
<l>    CSVDataOut := []</l>
<l>    CSVInfoOut := []</l>
<l>    InputFile := ImageFolders[Idx] + FileName</l>
<c>    * Read the Groundtruth Data</c>
<l>    mvtec_csv_read_data (InputFile, ColSeparator, StringQuote, CSVData, CSVInfo)</l>
<l>    mvtec_csv_get_row(CSVData,CSVInfo,0,HeaderCategories)</l>
<l>    mvtec_csv_get_attribute (CSVInfo, 'rows', Rows)</l>
<l>    NumImages := Rows-1</l>
<c>    *</c>
<l>    tuple_find (HeaderCategories, 'Sequence', SequenceIdx)</l>
<l>    tuple_find (HeaderCategories, 'ImageFile', ImageFileIdx)</l>
<l>    tuple_find (HeaderCategories, 'SegmentationFile', SegmemtationFileIdx)</l>
<l>    tuple_find (HeaderCategories, 'theoretical_box_no_scale', NoScaleIdx)</l>
<l>    tuple_find (HeaderCategories, 'theoretical_box_axis_aligned', NoRotationIdx)</l>
<l>    tuple_find (HeaderCategories, 'theoretical_box_rot', Rect2Idx)</l>
<l>    tuple_find (HeaderCategories, 'theoretical_smallest_rectangle1', SmallestRect1Idx)</l>
<c>    *</c>
<c>    * Generate the output header - differentiate if data is already present or not</c>
<l>    HeaderCategoriesOut := HeaderCategories</l>
<l>    if (NoScaleIdx == -1)</l>
<l>        HeaderCategoriesOut := [HeaderCategoriesOut,'theoretical_box_no_scale']</l>
<l>    endif</l>
<l>    if (NoRotationIdx == -1)</l>
<l>        HeaderCategoriesOut := [HeaderCategoriesOut,'theoretical_box_axis_aligned']</l>
<l>    endif</l>
<l>    if (Rect2Idx == -1)</l>
<l>        HeaderCategoriesOut := [HeaderCategoriesOut,'theoretical_box_rot']</l>
<l>    endif</l>
<l>    if (SmallestRect1Idx == -1)</l>
<l>        HeaderCategoriesOut := [HeaderCategoriesOut,'theoretical_smallest_rectangle1']</l>
<l>    endif</l>
<l>    mvtec_csv_append_row (CSVDataOut, CSVInfoOut, HeaderCategoriesOut, CSVDataOut, CSVInfoOut)</l>
<c>    *</c>
<l>    initial_box_coordinate := []</l>
<c>    ******************************************************</c>
<c>    * Walk through csv file and display the Groundtruth</c>
<c>    ******************************************************</c>
<l>    for ImageIdx := 0 to NumImages-1 by 1</l>
<l>        mvtec_csv_get_row(CSVData,CSVInfo,ImageIdx+1,Obj)</l>
<l>        Sequence := Obj[SequenceIdx]</l>
<l>        ImageName := Obj[ImageFileIdx]</l>
<l>        SegmentationFile := number(Obj[SegmemtationFileIdx])</l>
<c>        *</c>
<c>        ****************</c>
<c>        * Read Image</c>
<c>        ****************</c>
<l>        read_image (Image, ImageFolders[Idx] + ImageName)</l>
<c>        </c>
<c>        ************************</c>
<c>        * Read Segmentation</c>
<c>        ************************</c>
<l>        SegmentationPath := ImageFolders[Idx] + SegmentationFile</l>
<c>        * The images folder may be a link. We compress it for linix</c>
<l>        compress_path_linux (SegmentationPath, SegmentationPath)</l>
<l>        read_image (Segmentation, SegmentationPath)</l>
<l>        threshold (Segmentation, RegionSeg, 1, 255)</l>
<c>        </c>
<c>        * The vot2016 dataset sometimes has empty segmentations...</c>
<l>        area_center (RegionSeg, Area, _, _)</l>
<c>        </c>
<c>        **************************************</c>
<c>        * Generate the theoretical trackers</c>
<c>        **************************************</c>
<l>        InitExhaustiveScale := (ImageIdx == 0)</l>
<l>        if (gen_theoretical_tracker)</l>
<l>            gen_theoretical_trackers (RegionSeg, InitExhaustiveScale, initial_box_coordinate, \
                                      overlap_no_scale, overlap_no_rotiation, \
                                      overlap_rect2, box_coordinate_no_scale, box_coordinate_no_rotation, \
                                      box_coordinate_rect2, initial_box_coordinate)</l>
<l>            mvtec_tuple_to_string (box_coordinate_no_scale, '0.3f', box_coordinate_no_scale_string)</l>
<l>            mvtec_tuple_to_string (box_coordinate_no_rotation, '0.3f', box_coordinate_no_rotation_string)</l>
<l>            mvtec_tuple_to_string (box_coordinate_rect2, '0.3f', box_coordinate_rect2_string)</l>
<c>            *</c>
<c>            * Generate the smallest enclosing rectangle1</c>
<l>            smallest_rectangle1 (RegionSeg, Row1, Column1, Row2, Column2)</l>
<l>            convert_rectangle1_params_to_box_coordinates (Row1, Column1, Row2, Column2, \
                                                          box_coordinate_smallest_rectangle1)</l>
<l>            mvtec_tuple_to_string (box_coordinate_smallest_rectangle1, '0.3f', box_coordinate_smallest_rectangle1_string)</l>
<l>        else</l>
<c>            * If we are not meant to generate new data, check if we can find existing data</c>
<l>            tuple_find (HeaderCategories, 'theoretical_box_no_scale', NoScaleIdx)</l>
<l>            tuple_find (HeaderCategories, 'theoretical_box_axis_aligned', NoRotationIdx)</l>
<l>            tuple_find (HeaderCategories, 'theoretical_box_rot', Rect2Idx)</l>
<l>            tuple_find (HeaderCategories, 'theoretical_smallest_rectangle1', SmallestRect1Idx)</l>
<l>            mvtec_string_to_tuple (number(Obj[NoScaleIdx]), box_coordinate_no_scale)</l>
<l>            mvtec_string_to_tuple (number(Obj[NoRotationIdx]), box_coordinate_no_rotation)</l>
<l>            mvtec_string_to_tuple (number(Obj[Rect2Idx]), box_coordinate_rect2)</l>
<l>            mvtec_string_to_tuple (number(Obj[SmallestRect1Idx]), box_coordinate_smallest_rectangle1)</l>
<l>        endif</l>
<c>                </c>
<c>        ************</c>
<c>        * Display  *</c>
<c>        ************</c>
<l>        if (ImageIdx == 0)</l>
<l>            read_image (Image, ImageFolders[Idx] + ImageName)</l>
<l>            dev_set_window (WindowHandle)</l>
<l>            dev_display (Image)</l>
<l>        endif</l>
<l>        if (Display)</l>
<l>            dev_set_window (WindowHandle)</l>
<l>            gen_rectangle_b (RectangleNoScale, box_coordinate_no_scale)</l>
<l>            gen_rectangle_b (RectangleNoRotation, box_coordinate_no_rotation)</l>
<l>            gen_rectangle_b (Rectangle2, box_coordinate_rect2)</l>
<l>            gen_rectangle_b (SmallestRectangle1, box_coordinate_smallest_rectangle1)</l>
<c>    </c>
<l>            set_system ('flush_graphic', 'false')</l>
<l>            dev_display (Image)</l>
<l>            disp_region_alpha (RectangleNoScale, NoScaleColor, 70)</l>
<l>            disp_region_alpha (RectangleNoRotation, NoRotColor, 70)</l>
<l>            disp_region_alpha (Rectangle2, Rect2Color, 70)</l>
<l>            disp_region_alpha (SmallestRectangle1, SmallestRect1Color, 70)</l>
<l>            dev_set_draw ('margin')</l>
<l>            dev_set_line_width (2)</l>
<l>            dev_set_color ('black')</l>
<l>            set_system ('flush_graphic', 'true')</l>
<l>            dev_display (RegionSeg)       </l>
<l>        endif</l>
<l>        if (gen_theoretical_tracker)</l>
<l>            MessageStr := 'Generated data: ...' + (ImageIdx+1) + '/' + NumImages</l>
<l>        else</l>
<l>            MessageStr := 'Read data: ...' + (ImageIdx+1) + '/' + NumImages</l>
<l>        endif</l>
<l>        disp_message (WindowHandle, MessageStr, 'window', 12, 12, 'black', 'true')</l>
<c>        </c>
<c>        * Add the theoretical trackers to csv - differentiate if data is</c>
<c>        * already present or not</c>
<l>        if (write_theoretical_no_scale)</l>
<l>            if (NoScaleIdx # -1)</l>
<l>                Obj[NoScaleIdx] := box_coordinate_no_scale_string</l>
<l>            else</l>
<l>                Obj := [Obj, box_coordinate_no_scale_string]</l>
<l>            endif</l>
<l>        endif</l>
<l>        if (write_theoretical_no_rotation)</l>
<l>            if (NoRotationIdx # -1)</l>
<l>                Obj[NoRotationIdx] := box_coordinate_no_rotation_string</l>
<l>            else</l>
<l>                Obj := [Obj, box_coordinate_no_rotation_string]</l>
<l>            endif</l>
<l>        endif</l>
<l>        if (write_theoretical_rect2)</l>
<l>            if (Rect2Idx # -1)</l>
<l>                Obj[Rect2Idx] := box_coordinate_rect2_string</l>
<l>            else</l>
<l>                Obj := [Obj, box_coordinate_rect2_string]</l>
<l>            endif</l>
<l>        endif</l>
<l>        if (write_theoretical_smallest_rectangle1)</l>
<l>            if (SmallestRect1Idx # -1)</l>
<l>                Obj[SmallestRect1Idx] := box_coordinate_smallest_rectangle1_string</l>
<l>            else</l>
<l>                Obj := [Obj, box_coordinate_smallest_rectangle1_string]</l>
<l>            endif</l>
<l>        endif</l>
<c>        *</c>
<l>        mvtec_csv_append_row (CSVDataOut, CSVInfoOut, Obj, CSVDataOut, CSVInfoOut)</l>
<l>    endfor</l>
<c>    </c>
<c>    * Write the csv file</c>
<l>    if (write_theoretical_no_scale or write_theoretical_no_rotation or write_theoretical_rect2)</l>
<l>        mvtec_csv_write_data (CSVDataOut, CSVInfoOut, InputFile, ColSeparator)</l>
<l>    endif</l>
<l>endfor</l>
<c>*</c>
<l>set_system ('clip_region', ClipRegion)</l>
</body>
<docu id="main">
<parameters/>
</docu>
</procedure>
</hdevelop>
