[[画像処理, OpenCV2 使用] を 行う, 機能追加版, 多角形(内部ぬりつぶし)描画機能を追加] , Python(パイソン) 使って作ってみた ,ソースプログラムリスト あり

Python(パイソン) プログラム作ってみた インデックス へ

-----

2024.9.21 presented in [note] ( //note.com/runningWater/ )

----------
これ以降に記述されている内容は、このようなコンピューター・プログラムを制作した、というような事を、ただ、述べているに過ぎない。

以下の記述を読んだ人が、それを単に参考にする、というのであれば、問題は無いと、思われる。

しかし、記述されている内容に沿って、その人が、そこに記されているのと同様の制作や作業を行った際に、その制作、作業、コンピューターの作動の結果、使用されたコンピューター等、様々な方面において、何らかの問題が発生しない、という保証は、全くない。

その制作、作業、コンピューターの作動の結果、その人や、その人が所属している組織、その人が使用した様々な機器、インフラストラクチャー等の、身の上にどのような事が起ころうとも、私は一切、責任を負わない。

このプログラムは、Python(パイソン) 言語を使って、記述されている。
----------

既に発表ずみの、下記プログラムに、機能追加した。

Python(パイソン) ,OpenCV2 ,ソースプログラムリスト あり ,プログラム作ってみた ,[画像処理, OpenCV2 使用] を 行う

追加した主な機能は、下記である。

 画像中に、多角形(内部をぬりつぶしした)を、挿入したい、というような時に、使える機能
 
 ( def insert_polygon_filled_into_ImageData )
 
OpenCV2 の [ fillConvexPoly ] を使って、この機能を実現している。

エラー処理に関しては、下記のコンテツに記述したものを利用している。

Python(パイソン) ,ソースプログラムリスト あり ,プログラム作ってみた ,[エラー処理] を 行う

ファイル名 [ ImageDataTwoDimensionHandlingV3.py ]

----------


import cv2
import numpy as np

import ErrorProcess

#===========================================
class  ImageDataTwoDimensionHandlingV3 :

    CV_CLASS_NAME = "ImageDataTwoDimensionHandlingV3"

    #------------------------------------------------------------
        # definition of constructer

    def __init__( self
                     , arg_requester_module
                     , arg_requester_function
                ):

        methode_name = "constructer"

        print( "==================================" )
        print( "Enter into , Class = "
                   + ImageDataTwoDimensionHandlingV3.CV_CLASS_NAME
                      + " , methode = " + methode_name )
        print ( "    arg_requester_module = " + arg_requester_module
                   + " , arg_requester_function = " + arg_requester_function )
        print( "==================================" )

        self.iv_image_data_contained_or_not = "N"
        self.iv_image_data_Lab_prepared = "N"

        self.iv_list_of_ErrorMessage = [
                       "******** Error !!! **********"
                     , " "
                     , " "
                     , " "
                     , " "
                     , " "
                       ]

        print( "==================================" )
        print( "Exit from , Class = "
                   + ImageDataTwoDimensionHandlingV3.CV_CLASS_NAME
                      + " , methode = " + methode_name )
        print ( "    arg_requester_module = " + arg_requester_module
                   + " , arg_requester_function = " + arg_requester_function )
        print( "==================================" )

        return

    #------------------------------------------------------------
    def  set_image_data ( self
                     , arg_requester_module
                     , arg_requester_function
                     , arg_image_data
                      ) :

        methode_name = "set_image_data"

        print( "==================================" )
        print( "Enter into , Class = "
                   + ImageDataTwoDimensionHandlingV3.CV_CLASS_NAME
                      + " , methode = " + methode_name )
        print ( "    arg_requester_module = " + arg_requester_module
                   + " , arg_requester_function = " + arg_requester_function )
        print( "==================================" )

             #-------- set image_data ----------
        self.iv_image_data_contained = \
                 arg_image_data
             #-----------------------------------
               #get size and chanel number ( height,  width,  channel )
        image_height, image_width, image_channel \
                    = self.iv_image_data_contained.shape[:3]
        self.iv_image_data_contained_width = image_width
        self.iv_image_data_contained_height = image_height

        print ( "==================================" )
        print ( "Class = "
                   + ImageDataTwoDimensionHandlingV3.CV_CLASS_NAME
                      + " , methode = " + methode_name )
        print ( "image data has been set" )
        print ( "type of image data = " \
                , type ( self.iv_image_data_contained ) )
        print ( "self.iv_image_data_contained_width = "
                    , self.iv_image_data_contained_width )
        print ( "self.iv_image_data_contained_height = "
                    , self.iv_image_data_contained_height )
        print ( "==================================" )

        if ( ( self.iv_image_data_contained_width < 1 )
              or
             ( self.iv_image_data_contained_height < 1 )
           ) :
            self.list_of_ErrorMessage [ 1 ] = "from upper , arg_requester_module = " + arg_requester_module
            self.self.iv_list_of_ErrorMessage [ 2 ] = "from upper , arg_requester_function = " + arg_requester_function
            self.self.iv_list_of_ErrorMessage [ 3 ]  = "loaded image data = " + self.iv_abs_path_of_ImageDataFile
            self.self.iv_list_of_ErrorMessage [ 4 ] =  "width = " + self.iv_image_data_contained_width
            self.self.iv_list_of_ErrorMessage [ 5 ] =  "height = " + self.iv_image_data_contained_height
            ErrorProcess.do_error_process (
                         #arg_requester_module
                   ImageDataTwoDimensionHandlingV3.CV_CLASS_NAME
                         #arg_requester_function
                 , methode_name
                         #arg_terminate_process_Y_or_N
                 , "Y"
                         #arg_self.iv_list_of_ErrorMessage
                 , self.self.iv_list_of_ErrorMessage
                     )
            return

            #----------------------------------------
        self.iv_image_data_contained_or_not = "Y"
            #----------------------------------------

        print( "==================================" )
        print( "Exit from , Class = "
                   + ImageDataTwoDimensionHandlingV3.CV_CLASS_NAME
                      + " , methode = " + methode_name )
        print ( "    arg_requester_module = " + arg_requester_module
                   + " , arg_requester_function = " + arg_requester_function )
        print( "==================================" )

        return

    #------------------------------------------------------------
    def load_image_data ( self
                     , arg_requester_module
                     , arg_requester_function
                     , arg_abs_path_of_ImageDataFile
                      ) :

        methode_name = "load_image_data"

        self.iv_abs_path_of_ImageDataFile = arg_abs_path_of_ImageDataFile

        print( "==================================" )
        print( "Enter into , Class = "
                   + ImageDataTwoDimensionHandlingV3.CV_CLASS_NAME
                      + " , methode = " + methode_name )
        print ( "    arg_requester_module = " + arg_requester_module
                   + " , arg_requester_function = " + arg_requester_function )
        print( "==================================" )

        print( "self.iv_abs_path_of_ImageDataFile = "
                       + self.iv_abs_path_of_ImageDataFile )
        print( "==================================" )

             #-------- load image data using OpneCV2 ----------
        self.iv_image_data_contained = \
                 cv2.imread ( self.iv_abs_path_of_ImageDataFile )
             #-----------------------------------
               #get size and chanel number ( height,  width,  channel )
        image_height, image_width, image_channel \
                    = self.iv_image_data_contained.shape[:3]
        self.iv_image_data_contained_width = image_width
        self.iv_image_data_contained_height = image_height

        print ( "==================================" )
        print ( "Class = "
                   + ImageDataTwoDimensionHandlingV3.CV_CLASS_NAME
                      + " , methode = " + methode_name )
        print ( "image data has been loaded" )
        print ( "type of image data = " \
                , type ( self.iv_image_data_contained ) )
        print ( "self.iv_image_data_contained_width = "
                    , self.iv_image_data_contained_width )
        print ( "self.iv_image_data_contained_height = "
                    , self.iv_image_data_contained_height )
        print ( "==================================" )

        if ( ( self.iv_image_data_contained_width < 1 )
              or
             ( self.iv_image_data_contained_height < 1 )
           ) :
            self.list_of_ErrorMessage [ 1 ] = "from upper , arg_requester_module = " + arg_requester_module
            self.self.iv_list_of_ErrorMessage [ 2 ] = "from upper , arg_requester_function = " + arg_requester_function
            self.self.iv_list_of_ErrorMessage [ 3 ]  = "loaded image data = " + self.iv_abs_path_of_ImageDataFile
            self.self.iv_list_of_ErrorMessage [ 4 ] =  "width = " + self.iv_image_data_contained_width
            self.self.iv_list_of_ErrorMessage [ 5 ] =  "height = " + self.iv_image_data_contained_height
            ErrorProcess.do_error_process (
                         #arg_requester_module
                   ImageDataTwoDimensionHandlingV3.CV_CLASS_NAME
                         #arg_requester_function
                 , methode_name
                         #arg_terminate_process_Y_or_N
                 , "Y"
                         #arg_self.iv_list_of_ErrorMessage
                 , self.self.iv_list_of_ErrorMessage
                     )
            return

            #----------------------------------------
        self.iv_image_data_contained_or_not = "Y"
            #----------------------------------------

        print( "==================================" )
        print( "Exit from , Class = "
                   + ImageDataTwoDimensionHandlingV3.CV_CLASS_NAME
                      + " , methode = " + methode_name )
        print ( "    arg_requester_module = " + arg_requester_module
                   + " , arg_requester_function = " + arg_requester_function )
        print( "==================================" )

        return

            #------------------------------------------------
    def write_loaded_image_data_to_directed_path (
                          self
                        ,  arg_requester_module
                        , arg_requester_function
                        , arg_path_of_output_file ) :

        methode_name = "write_loaded_image_data_to_directed_path"

        print( "==================================" )
        print( "Enter into , Class = "
                   + ImageDataTwoDimensionHandlingV3.CV_CLASS_NAME
                      + " , methode = " + methode_name )
        print ( "    arg_requester_module = " + arg_requester_module
                   + " , arg_requester_function = " + arg_requester_function )
        print ( "    arg_path_of_output_file = " + arg_path_of_output_file )
        print( "==================================" )

        if ( self.iv_image_data_contained_or_not == "N" ) :
            error_process_image_data_not_loaded (
                   arg_requester_module
                 , arg_requester_function
                 , self.iv_abs_path_of_ImageDataFile
                                            )

             #------ write image data to directed path ------
        cv2.imwrite ( arg_path_of_output_file
                      , self.iv_image_data_contained )
             #-------------------------------------------
        return

        #------------------------------------------------
    def get_width_of_ImageData ( self ) :

        return ( self.iv_image_data_contained_width )
        #------------------------------------------------
    def get_height_of_ImageData ( self ) :

        return ( self.iv_image_data_contained_height )

        #------------------------------------------------
    def get_data_of_one_pixel ( self
                         , arg_requester_module
                         , arg_requester_function
                              # in the commonly used mathematical XY cordinates
                         , arg_x_on_ImageData_space
                         , arg_y_on_ImageData_space
                               ) :

        methode_name = "get_data_of_one_pixel"

        if ( self.iv_image_data_contained_or_not == "N" ) :
            error_process_image_data_not_loaded (
                   arg_requester_module
                 , arg_requester_function
                 , self.iv_abs_path_of_ImageDataFile
                                            )

         #------------------------------------------
         # conversion
         #    commonly used mathematical XY cordinates
         #       --> commonly used Computer Graphics XY cordinates
        x_on_ImageData_space_computer_graphics = arg_x_on_ImageData_space
        y_on_ImageData_space_computer_graphics = \
              self.iv_image_data_contained_height  \
                        - arg_y_on_ImageData_space - 1

        if ( ( x_on_ImageData_space_computer_graphics < 0 )
             or
             ( x_on_ImageData_space_computer_graphics >= \
                        self.iv_image_data_contained_width )
             or
             ( y_on_ImageData_space_computer_graphics < 0 )
             or
             ( y_on_ImageData_space_computer_graphics >= \
                      self.iv_image_data_contained_height )
           ) :
            print( "==================================" )
            print( "Class = "
                   + ImageDataTwoDimensionHandlingV3.CV_CLASS_NAME
                      + " , methode = " + methode_name )
            print ( "arg_requester_module = " , arg_requester_module )
            print ( "arg_requester_function = " , arg_requester_function )
            print ( "Error !!!  Over Area")
            print ( "arg_x_on_ImageData_space = " , arg_x_on_ImageData_space )
            print ( "arg_y_on_ImageData_space = " , arg_y_on_ImageData_space )
            print ( "self.iv_image_data_contained_width = " \
                         , self.iv_image_data_contained_width )
            print ( "self.iv_image_data_contained_height = " \
                         , self.iv_image_data_contained_height )
            print ( "self.iv_abs_path_of_ImageDataFile = " \
                         + self.iv_abs_path_of_ImageDataFile )
            print( "==================================" )

            self.iv_list_of_ErrorMessage [ 0 ] = \
              "**** Error !!!  Area Over ****"
            ErrorProcess.do_error_process (
                         #arg_requester_module
                   ImageDataTwoDimensionHandlingV3.CV_CLASS_NAME
                         #arg_requester_function
                 , methode_name
                         #arg_terminate_process_Y_or_N
                 , "Y"
                         #arg_self.iv_list_of_ErrorMessage
                 , self.iv_list_of_ErrorMessage
                     )

            #--------------------------
        color_value = self.iv_image_data_contained   \
                              [
                               y_on_ImageData_space_computer_graphics
                             , x_on_ImageData_space_computer_graphics
                              ]

        return color_value

        #------------------------------------------------
    def put_data_of_one_pixel ( self
                         , arg_requester_module
                         , arg_requester_function
                              # in the commonly used mathematical XY cordinates
                         , arg_x_on_ImageData_space
                         , arg_y_on_ImageData_space
                         , arg_color_data_Blue
                         , arg_color_data_Green
                         , arg_color_data_Red
                               ) :

        methode_name = "put_data_of_one_pixel"

        if ( self.iv_image_data_contained_or_not == "N" ) :
            error_process_image_data_not_loaded (
                   arg_requester_module
                 , arg_requester_function
                 , self.iv_abs_path_of_ImageDataFile
                                            )
         #------------------------------------------

         # conversion
         #    commonly used mathematical XY cordinates
         #       --> commonly used Computer Graphics XY cordinates
        x_on_ImageData_space_computer_graphics    \
                  = arg_x_on_ImageData_space
        y_on_ImageData_space_computer_graphics    \
                  = self.iv_image_data_contained_height     \
                        - arg_y_on_ImageData_space - 1

              # blue color element
        self.iv_image_data_contained [
                      y_on_ImageData_space_computer_graphics
                    , x_on_ImageData_space_computer_graphics
                    , 0 ]            \
               = arg_color_data_Blue
              # /green color element
        self.iv_image_data_contained [
                      y_on_ImageData_space_computer_graphics
                    , x_on_ImageData_space_computer_graphics
                    , 1 ]            \
               = arg_color_data_Green
              # red color element
        self.iv_image_data_contained [
                      y_on_ImageData_space_computer_graphics
                    , x_on_ImageData_space_computer_graphics
                    , 2 ]               \
               = arg_color_data_Red

        return

        #------------------------------------------------
    def get_loaded_image_data (
                             self
                           , arg_requester_module
                           , arg_requester_function
                               ) :

        methode_name = "get_loaded_image_data"

        print( "==================================" )
        print( "Enter into , Class = "
                   + ImageDataTwoDimensionHandlingV3.CV_CLASS_NAME
                      + " , methode = " + methode_name )
        print ( "    arg_requester_module = " + arg_requester_module
                   + " , arg_requester_function = " + arg_requester_function )
        print ( "    self.iv_abs_path_of_ImageDataFile = " \
                     + self.iv_abs_path_of_ImageDataFile )
        print( "==================================" )

        if ( self.iv_image_data_contained_or_not == "N" ) :
            error_process_image_data_not_loaded (
                   arg_requester_module
                 , arg_requester_function
                 , self.iv_abs_path_of_ImageDataFile
                                            )
        else :
            print( "==================================" )
            print( "Class = "
                   + ImageDataTwoDimensionHandlingV3.CV_CLASS_NAME
                      + " , methode = " + methode_name )
            print ( "self.iv_abs_path_of_ImageDataFile = " \
                     + self.iv_abs_path_of_ImageDataFile )
            print ( "width_of_ImageData = " + \
                            str ( self.iv_image_data_contained_width ) )
            print ( "height_of_ImageData = " + \
                            str ( self.iv_image_data_contained_height ) )

        print( "==================================" )
        print( "Exit from , Class = "
                   + ImageDataTwoDimensionHandlingV3.CV_CLASS_NAME
                      + " , methode = " + methode_name )
        print ( "    arg_requester_module = " + arg_requester_module
                   + " , arg_requester_function = " + arg_requester_function )
        print ( "    self.iv_abs_path_of_ImageDataFile = " \
                     + self.iv_abs_path_of_ImageDataFile )
        print( "==================================" )

        return ( self.iv_image_data_contained )

        #------------------------------------------------
    def convert_BGR_to_Lab ( self ) :

        methode_name = "convert_BGR_to_Lab"

        if ( self.iv_image_data_contained_or_not == "N" ) :
            error_process_image_data_not_loaded (
                   arg_requester_module
                 , arg_requester_function
                 , self.iv_abs_path_of_ImageDataFile
                                            )

                  #-------- load image data using OpneCV2 ----------
        self.iv_image_data_using_Lab =   \
                   cv2.imread ( self.iv_abs_path_of_ImageDataFile )
        self.iv_image_data_using_Lab =   \
                   cv2.cvtColor (
                                 #loaded image data , BGR format
                         self.iv_image_data_contained
                                #direction  BGR --> Lab
                       , cv2.COLOR_BGR2Lab )
        self.iv_image_data_Lab_prepared = "Y"

        #------------------------------------------------
    def get_data_of_one_pixel_Lab_format (
                           self
                         , arg_requester_module
                         , arg_requester_function
                              # in the commonly used mathematical XY cordinates
                         , arg_x_on_ImageData_space
                         , arg_y_on_ImageData_space
                               ) :

        methode_name = "get_data_of_one_pixel_Lab_format"

        if ( self.iv_image_data_Lab_prepared == "N" ) :
            error_process_image_data_not_loaded (
                   arg_requester_module
                 , arg_requester_function
                 , self.iv_abs_path_of_ImageDataFile
                                            )

         # conversion
         #    commonly used mathematical XY cordinates
         #       --> commonly used Computer Graphics XY cordinates
        x_on_ImageData_space_computer_graphics   \
                  = arg_x_on_ImageData_space
        y_on_ImageData_space_computer_graphics   \
                  = self.iv_image_data_contained_height   \
                        - arg_y_on_ImageData_space - 1

        color_value_Lab = self.iv_image_data_using_Lab   \
                              [
                               y_on_ImageData_space_computer_graphics
                             , x_on_ImageData_space_computer_graphics
                              ]

        return color_value_Lab


        #------------------------------------------------
    def insert_polygon_filled_into_ImageData ( self  \
                   , arg_requester_module
                   , arg_requester_function
                              # in the commonly used mathematical XY cordinates
                   , arg_list_cordinates_of_vertex

                   , arg_color_Blue
                   , arg_color_Green
                   , arg_color_Red
                                   ) :

        methode_name = "insert_polygon_filled_into_ImageData"

        size_of_list_cordinates_of_vertex \
              = len ( arg_list_cordinates_of_vertex  )

        list_cordinates_of_vertex_inverse_y = [ ]

        for i in range ( size_of_list_cordinates_of_vertex ) :
            list_element = arg_list_cordinates_of_vertex [ i ]
            list_element_x = list_element [ 0 ]
            list_element_y = list_element [ 1 ]
            y_inverse = self.iv_image_data_contained_height - list_element_y - 1

            list_cordinates_of_vertex_inverse_y  \
                    .append ( [ list_element_x ,  y_inverse  ] )

        np_array_cordinates_of_vertex_inverse_y  \
                       = np.array (list_cordinates_of_vertex_inverse_y )

        # print( "==================================" )
        # print( "Class = "
        #            + ImageDataTwoDimensionHandlingV3.CV_CLASS_NAME
        #               + " , methode = " + methode_name )
        # print ( "list_cordinates_of_vertex_inverse_y = "  )
        # print ( list_cordinates_of_vertex_inverse_y )
        # print ( "np_array_cordinates_of_vertex_inverse_y = "  )
        # print ( np_array_cordinates_of_vertex_inverse_y )
        # print ( "arg_color_Blue = " , arg_color_Blue )
        # print ( "arg_color_Green = " , arg_color_Green )
        # print ( "arg_color_Red = " , arg_color_Red )
        # print( "==================================" )

        cv2.fillConvexPoly (    \
                 self.iv_image_data_contained
               , np_array_cordinates_of_vertex_inverse_y
               , (  arg_color_Blue     \
                   , arg_color_Green   \
                   , arg_color_Red    \
                  )
                               )

        #------------------------------------------------
    def insert_text_into_ImageData (
                     self
                   , arg_requester_module
                   , arg_requester_function
                              # in the commonly used mathematical XY cordinates
                   , arg_x_on_ImageData_space
                   , arg_y_on_ImageData_space

                   , arg_text_inserted

                   , arg_font_color_Blue
                   , arg_font_color_Green
                   , arg_font_color_Red
                                   ) :

        methode_name = "insert_text_into_ImageData"

        if ( self.iv_image_data_contained_or_not == "N" ) :
            error_process_image_data_not_loaded (
                   arg_requester_module
                 , arg_requester_function
                 , self.iv_abs_path_of_ImageDataFile
                                            )

         #------------------------------------------
         # conversion
         #    commonly used mathematical XY cordinates
         #       --> commonly used Computer Graphics XY cordinates
        x_on_ImageData_space_computer_graphics = arg_x_on_ImageData_space
        y_on_ImageData_space_computer_graphics = \
              self.iv_image_data_contained_height  \
                        - arg_y_on_ImageData_space - 1

        if ( ( x_on_ImageData_space_computer_graphics < 0 )
             or
             ( x_on_ImageData_space_computer_graphics >= \
                        self.iv_image_data_contained_width )
             or
             ( y_on_ImageData_space_computer_graphics < 0 )
             or
             ( y_on_ImageData_space_computer_graphics >= \
                      self.iv_image_data_contained_height )
           ) :
            print( "==================================" )
            print( "Class = "
                   + ImageDataTwoDimensionHandlingV3.CV_CLASS_NAME
                      + " , methode = " + methode_name )
            print ( "arg_requester_module = " , arg_requester_module )
            print ( "arg_requester_function = " , arg_requester_function )
            print ( "Error !!!  Over Area")
            print ( "arg_x_on_ImageData_space = " , arg_x_on_ImageData_space )
            print ( "arg_y_on_ImageData_space = " , arg_y_on_ImageData_space )
            print ( "self.iv_image_data_contained_width = " \
                         , self.iv_image_data_contained_width )
            print ( "self.iv_image_data_contained_height = " \
                         , self.iv_image_data_contained_height )
            print ( "self.iv_abs_path_of_ImageDataFile = " \
                         + self.iv_abs_path_of_ImageDataFile )
            print( "==================================" )

            self.iv_list_of_ErrorMessage [ 0 ] = \
              "**** Error !!!  Area Over ****"
            ErrorProcess.do_error_process (
                         #arg_requester_module
                   ImageDataTwoDimensionHandlingV3.CV_CLASS_NAME
                         #arg_requester_function
                 , methode_name
                         #arg_terminate_process_Y_or_N
                 , "Y"
                         #arg_self.iv_list_of_ErrorMessage
                 , self.iv_list_of_ErrorMessage
                     )

            #--------------------------
        cv2.putText (
                self.iv_image_data_contained
              , arg_text_inserted
              , (
                   x_on_ImageData_space_computer_graphics
                 , y_on_ImageData_space_computer_graphics
                 )
              , cv2.FONT_HERSHEY_PLAIN   #font_style
              , 1.5                      #font_size
              , (
                   arg_font_color_Blue
                 , arg_font_color_Green
                 , arg_font_color_Red
                 )
               , 2                      #font_thickness
               , cv2.LINE_AA            #line_type
                       )
        return

        #------------------------------------------------
    def insert_rectangle_figure_into_ImageData (
                     self
                   , arg_requester_module
                   , arg_requester_function
                              # in the commonly used mathematical XY cordinates
                   , arg_From_x_on_ImageData_space
                   , arg_From_y_on_ImageData_space
                   , arg_To_x_on_ImageData_space
                   , arg_To_y_on_ImageData_space

                       # -1 = Fill inside
                   , arg_thickness_of_figure

                   , arg_fill_inside_Y_or_N

                   , arg_color_Blue
                   , arg_color_Green
                   , arg_color_Red
                                   ) :

        methode_name = "insert_rectangle_into_ImageData"

        if ( self.iv_image_data_contained_or_not == "N" ) :
            error_process_image_data_not_loaded (
                   arg_requester_module
                 , arg_requester_function
                 , self.iv_abs_path_of_ImageDataFile
                                            )

         #------------------------------------------
         # conversion
         #    commonly used mathematical XY cordinates
         #       --> commonly used Computer Graphics XY cordinates
        from_x_on_ImageData_space_computer_graphics = \
               arg_From_x_on_ImageData_space
        from_y_on_ImageData_space_computer_graphics = \
              self.iv_image_data_contained_height  \
                        - arg_From_y_on_ImageData_space - 1

        to_x_on_ImageData_space_computer_graphics = \
               arg_To_x_on_ImageData_space
        to_y_on_ImageData_space_computer_graphics = \
              self.iv_image_data_contained_height  \
                        - arg_To_y_on_ImageData_space - 1

        if (
                      #---------- about from x , y
             ( from_x_on_ImageData_space_computer_graphics < 0 )
             or
             ( from_x_on_ImageData_space_computer_graphics >= \
                        self.iv_image_data_contained_width )
             or
             ( from_y_on_ImageData_space_computer_graphics < 0 )
             or
             ( from_y_on_ImageData_space_computer_graphics >= \
                      self.iv_image_data_contained_height )
             or
                       #---------- about to x , y
             ( to_x_on_ImageData_space_computer_graphics < 0 )
             or
             ( to_x_on_ImageData_space_computer_graphics >= \
                        self.iv_image_data_contained_width )
             or
             ( to_y_on_ImageData_space_computer_graphics < 0 )
             or
             ( to_y_on_ImageData_space_computer_graphics >= \
                      self.iv_image_data_contained_height )
           ) :
            print( "==================================" )
            print( "Class = "
                   + ImageDataTwoDimensionHandlingV3.CV_CLASS_NAME
                      + " , methode = " + methode_name )
            print ( "arg_requester_module = " , arg_requester_module )
            print ( "arg_requester_function = " , arg_requester_function )
            print ( "Error !!!  Over Area")
            print ( "arg_From_x_on_ImageData_space = " , arg_From_x_on_ImageData_space )
            print ( "arg_From_y_on_ImageData_space = " , arg_From_y_on_ImageData_space )
            print ( "arg_To_x_on_ImageData_space = " , arg_To_x_on_ImageData_space )
            print ( "arg_To_y_on_ImageData_space = " , arg_To_y_on_ImageData_space )
            print ( "self.iv_image_data_contained_width = " \
                         , self.iv_image_data_contained_width )
            print ( "self.iv_image_data_contained_height = " \
                         , self.iv_image_data_contained_height )
            print ( "self.iv_abs_path_of_ImageDataFile = " \
                         + self.iv_abs_path_of_ImageDataFile )
            print( "==================================" )

            self.iv_list_of_ErrorMessage [ 0 ] = \
              "**** Error !!!  Area Over ****"
            ErrorProcess.do_error_process (
                         #arg_requester_module
                   ImageDataTwoDimensionHandlingV3.CV_CLASS_NAME
                         #arg_requester_function
                 , methode_name
                         #arg_terminate_process_Y_or_N
                 , "Y"
                         #arg_self.iv_list_of_ErrorMessage
                 , self.iv_list_of_ErrorMessage
                     )
            #------------------------------
        thickness_of_drawing = arg_thickness_of_figure
        if ( arg_fill_inside_Y_or_N == "Y" ) :
            thickness_of_drawing = -1
            #--------------------------
        print( "==================================" )
        print( "Class = "
                   + ImageDataTwoDimensionHandlingV3.CV_CLASS_NAME
                      + " , methode = " + methode_name )
        print ( "from_x_on_ImageData_space_computer_graphics = " \
                           , from_x_on_ImageData_space_computer_graphics )
        print ( "from_y_on_ImageData_space_computer_graphics = " \
                           , from_y_on_ImageData_space_computer_graphics )
        print ( "to_x_on_ImageData_space_computer_graphics = " \
                           , to_x_on_ImageData_space_computer_graphics )
        print ( "to_y_on_ImageData_space_computer_graphics = " \
                           , to_y_on_ImageData_space_computer_graphics )

        print ( "arg_color_Blue = " , arg_color_Blue )
        print ( "arg_color_Green = " , arg_color_Green )
        print ( "arg_color_Red = " , arg_color_Red )
        print ( "thickness_of_drawing = " , thickness_of_drawing )
        print( "==================================" )


        cv2.rectangle (
                    self.iv_image_data_contained
                  , (
                       from_x_on_ImageData_space_computer_graphics
                     , from_y_on_ImageData_space_computer_graphics
                    )
                  , (
                       to_x_on_ImageData_space_computer_graphics
                     , to_y_on_ImageData_space_computer_graphics
                    )
                  , (
                      arg_color_Blue
                    , arg_color_Green
                    , arg_color_Red
                    )
                  , thickness_of_drawing
                  , cv2.LINE_AA   # line type
                 )

        return

        #------------------------------------------------
    def insert_line_figure_into_ImageData (
                     self
                   , arg_requester_module
                   , arg_requester_function
                              # in the commonly used mathematical XY cordinates
                   , arg_From_x_on_ImageData_space
                   , arg_From_y_on_ImageData_space
                   , arg_To_x_on_ImageData_space
                   , arg_To_y_on_ImageData_space

                   , arg_thickness_of_line_figure

                   , arg_color_Blue
                   , arg_color_Green
                   , arg_color_Red
                                   ) :

        methode_name = "insert_line_figure_into_ImageData"

        if ( self.iv_image_data_contained_or_not == "N" ) :
            error_process_image_data_not_loaded (
                   arg_requester_module
                 , arg_requester_function
                 , self.iv_abs_path_of_ImageDataFile
                                            )

         #------------------------------------------
         # conversion
         #    commonly used mathematical XY cordinates
         #       --> commonly used Computer Graphics XY cordinates
        from_x_on_ImageData_space_computer_graphics = \
               arg_From_x_on_ImageData_space
        from_y_on_ImageData_space_computer_graphics = \
              self.iv_image_data_contained_height  \
                        - arg_From_y_on_ImageData_space - 1

        to_x_on_ImageData_space_computer_graphics = \
               arg_To_x_on_ImageData_space
        to_y_on_ImageData_space_computer_graphics = \
              self.iv_image_data_contained_height  \
                        - arg_To_y_on_ImageData_space - 1

        if (
                      #---------- about from x , y
             ( from_x_on_ImageData_space_computer_graphics < 0 )
             or
             ( from_x_on_ImageData_space_computer_graphics >= \
                        self.iv_image_data_contained_width )
             or
             ( from_y_on_ImageData_space_computer_graphics < 0 )
             or
             ( from_y_on_ImageData_space_computer_graphics >= \
                      self.iv_image_data_contained_height )
             or
                       #---------- about to x , y
             ( to_x_on_ImageData_space_computer_graphics < 0 )
             or
             ( to_x_on_ImageData_space_computer_graphics >= \
                        self.iv_image_data_contained_width )
             or
             ( to_y_on_ImageData_space_computer_graphics < 0 )
             or
             ( to_y_on_ImageData_space_computer_graphics >= \
                      self.iv_image_data_contained_height )
           ) :
            print( "==================================" )
            print( "Class = "
                   + ImageDataTwoDimensionHandlingV3.CV_CLASS_NAME
                      + " , methode = " + methode_name )
            print ( "arg_requester_module = " , arg_requester_module )
            print ( "arg_requester_function = " , arg_requester_function )
            print ( "Error !!!  Over Area")
            print ( "arg_From_x_on_ImageData_space = " , arg_From_x_on_ImageData_space )
            print ( "arg_From_y_on_ImageData_space = " , arg_From_y_on_ImageData_space )
            print ( "arg_To_x_on_ImageData_space = " , arg_To_x_on_ImageData_space )
            print ( "arg_To_y_on_ImageData_space = " , arg_To_y_on_ImageData_space )
            print ( "self.iv_image_data_contained_width = " \
                         , self.iv_image_data_contained_width )
            print ( "self.iv_image_data_contained_height = " \
                         , self.iv_image_data_contained_height )
            print ( "self.iv_abs_path_of_ImageDataFile = " \
                         + self.iv_abs_path_of_ImageDataFile )
            print( "==================================" )

            self.iv_list_of_ErrorMessage [ 0 ] = \
              "**** Error !!!  Area Over ****"
            ErrorProcess.do_error_process (
                         #arg_requester_module
                   ImageDataTwoDimensionHandlingV3.CV_CLASS_NAME
                         #arg_requester_function
                 , methode_name
                         #arg_terminate_process_Y_or_N
                 , "Y"
                         #arg_self.iv_list_of_ErrorMessage
                 , self.iv_list_of_ErrorMessage
                     )

            #--------------------------
        cv2.line (
                    self.iv_image_data_contained
                  , (
                       from_x_on_ImageData_space_computer_graphics
                     , from_y_on_ImageData_space_computer_graphics
                    )
                  , (
                       to_x_on_ImageData_space_computer_graphics
                     , to_y_on_ImageData_space_computer_graphics
                    )
                  , (
                      arg_color_Blue
                    , arg_color_Green
                    , arg_color_Red
                    )
                  , arg_thickness_of_line_figure
                  , cv2.LINE_AA   # line type
                 )

        return

        #------------------------------------------------
    def error_process_image_data_not_loaded (
                     self
                 , arg_requester_module
                 , arg_requester_function
                 , arg_abs_path_of_ImageDataFile
                                            ) :

        methode_name = "error_process_image_data_not_loaded"

        print( "==================================" )
        print( "Class = "
                   + ImageDataTwoDimensionHandlingV3.CV_CLASS_NAME
                      + " , methode = " + methode_name )
        print ( "Error !!!  image data not loaded or prepared")
        print ( "arg_requester_module = " , arg_requester_module )
        print ( "arg_requester_function = " , arg_requester_function )
        print ( "arg_abs_path_of_ImageDataFile = " \
                         , arg_abs_path_of_ImageDataFile )
        print( "==================================" )

        self.self.iv_list_of_ErrorMessage [ 0 ] = \
              "**** Error !!!  image data not loaded or prepared ****"
        ErrorProcess.do_error_process (
                         #arg_requester_module
                   ImageDataTwoDimensionHandlingV3.CV_CLASS_NAME
                         #arg_requester_function
                 , methode_name
                         #arg_terminate_process_Y_or_N
                 , "Y"
                         #arg_self.iv_list_of_ErrorMessage
                 , self.self.iv_list_of_ErrorMessage
                     )

----------

Python(パイソン) プログラム作ってみた インデックス へ


いいなと思ったら応援しよう!