[画像の色調を変える, トーンカーブを画像に描いて], Python(パイソン) 使って作ってみた ,ソースプログラムリスト あり

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

-----

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

----------
1 はじめに

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

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

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

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

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

----------
2 どのようなものを作ったのか

画像の色調を変えたい、という時に、しばしば、画像処理ソフトウェアに搭載されている、[トーンカーブ]という機能を用いて、処理をすることがある。

そこで、そのような処理が可能となるような、もの(以降、[モジュール群]と呼ぶ)を、作ってみた。

そして、以下の手順に沿って、その[モジュール群]を、使ってみた。

(1) [トーンカーブ画像]を、作成した

Windowsに付属の、描画用ソフトウェア([Microsoft Paint]と、いう名前なのだろうか?)を使って、画像を作成した。

画像は、青成分チャンネル、緑成分チャンネル、赤成分チャンネルに対応して、3個、作成し、それぞれの画像の中に、それぞれのチャンネルの色調変換を指定するための曲線(トーンカーブ)を、書き込んだ。

(2)[モジュール群]を、動かした

所定の位置(ディレクトリー)に、処理対象となる画像を、格納した後に、[モジュール群]を動かした。(その詳細については、後述)。

処理対象となる画像を、複数個、所定の位置に格納しても可、となるようにした。[モジュール群]は、それらの複数個の画像を、いっぺんに処理してくれた。

-----

以下に、[モジュール群]の使用例を、記す。

-----
処理例1

作製した[トーンカーブ画像]は、下記のようなものである。

これを適用して、[モジュール群]を、動かした結果、下記のように、色調変更が行われた。[青、上げて、緑を、下げて]、という感じの色調変更になっている。

-----
処理例2

作製した[トーンカーブ画像]は、下記のようなものである。

これを適用して、[モジュール群]を、動かした結果、下記のように、色調変更が行われた。

-----
処理例3

作製した[トーンカーブ画像]は、下記のようなものである。

これを適用して、[モジュール群]を、動かした結果、下記のように、色調変更が行われた。

----------
3 [モジュール群]は、二段構えで、使用されるように、なっている

[モジュール群]を使っての色調変換の処理は、下記のようにして、行われるように、した。

手順(1) [トーンカーブ画像]から、トーンカーブを表す曲線の座標を読み取り、それを、テキストデータとして、ファイルに書き込む

 (このように、[トーンカーブを表す曲線]の座標を、テキストファイルに書き込んでおくと、後日、同様の色調変換を行いたい、というような時に、そのテキストファイルを、そのまま使えるから、便利だろうと、思い、このような方式にした。)
 
手順(2) 作成された、テキストデータ中の、数値を使って、処理対象となる画像の色を、1ピクセルずつ、順に、変えていく

----------
4 [モジュール群]を構成するモジュールと、上記の手順との対応関係

以下に示すような、対応関係に、なっている。

手順(1) を行うための、モジュール
 MakeToneCurveTextData

手順(2) を行うための、モジュール
 VaryColorUsingToneCurve

下記モジュールは、手順(1)、手順(2) の双方で、使用される
 OneCurveData
 
-----

これらのモジュールは、必要に応じて、下記に解説されているモジュールを、使用している。

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

[テキストデータ を処理する] ためのクラス, Python(パイソン) 使って作ってみた ,ソースプログラムリスト あり

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

=============================

以降に、各モジュールの内容を、記す。

----------
5 [ MakeToneCurveTextData モジュール ]

ファイル名 [ MakeToneCurveTextData.py ]

----------


import  OneCurveData

#------------------------------

module_name = "MakeToneCurveTextData"
function_name = "make_ToneCurveTextData"

#------------------------------
path_of_directory_ToneCurve_Data  \
        = "E:ForPython/ForTest/Test_A/ToneCurve/"
        #--------------------------
path_of_sub_directory_ImageData = "ImageData/"
list_file_name_ToneCurve_ImageData = [ "ToneCurve_Blue.png"     \
                                                           ,  "ToneCurve_Green.png"     \
                                                           ,  "ToneCurve_Red.png"  ]
        #--------------------------
path_of_sub_directory_TextData = "TextData/"
list_file_name_ToneCurve_TextData = [ "ToneCurve_Blue.txt"       \
                                                         ,  "ToneCurve_Green.txt"    \
                                                         ,  "ToneCurve_Red.txt"  ]
        #-------------------------------
list_from_to_x_y_of_detecting_area_in_ImageData = [  \
                                 [ 50  ,  50 ]  ,  [ 305  ,  305 ]       \
                                                                                ]
        # ----------------------------------
list_from_to_color_value = [ \
                 [ [ 150  ,  255 ]  ,  [ 0     , 100 ] , [ 0     , 100 ]  ]    \
             ,   [ [ 0      ,  100 ]  ,  [ 130 , 255 ] , [ 0     , 100 ]  ]    \
             ,   [ [ 0      ,  100 ]  ,  [ 0     , 100 ] , [ 150 , 255 ]  ]    \
                                        ]                    
     #---------------------------------
for  i in range ( 0 , 3 ) :
        
    abs_path_of_ImageData      \
        = path_of_directory_ToneCurve_Data  \
              + path_of_sub_directory_ImageData   \
              + list_file_name_ToneCurve_ImageData [ i ]
    abs_path_of_TextData    \
        = path_of_directory_ToneCurve_Data  \
              + path_of_sub_directory_TextData   \
              + list_file_name_ToneCurve_TextData [ i ]
      #-----------------------------------------
    ins_OneCurveData = OneCurveData    \
                                         .OneCurveData ( )
    ins_OneCurveData   \
            .detect_Curve_in_ImageData_and_make_TextData (   \
                      abs_path_of_ImageData
                   ,  abs_path_of_TextData

                   ,  "X"    # arg_direction_of_detecting
                    , list_from_to_x_y_of_detecting_area_in_ImageData
                    , list_from_to_color_value [ i ]
                                                          )
       #---------------------------------------------------

----------

このモジュールによって作成されるテキストデータの内容の一例は、下記のようなものである。

-----


==========================| | | | | | 
abs_path_of_ImageData = |E:ForPython/ForTest/Test_A/ToneCurve/ImageData/ToneCurve_Blue.png| | | | | 
direction_of_detecting = |X| | | | | 
list_from_to_x_y_of_detecting_area_in_ImageData = |50|50|305|305| | 
list_from_to_color_value = |150|255|0|100|0|100
==========================| | | | | | 
Y|0|0|0.0|0.0|50|50
Y|1|0|0.00392156862745098|0.0|51|50
Y|2|0|0.00784313725490196|0.0|52|50
Y|3|0|0.011764705882352941|0.0|53|50
Y|4|2|0.01568627450980392|0.00784313725490196|54|52
Y|5|3|0.0196078431372549|0.011764705882352941|55|53
Y|6|5|0.023529411764705882|0.0196078431372549|56|55
Y|7|7|0.027450980392156862|0.027450980392156862|57|57
・・・・・・・・・・・・・・・・・・・・・・・・
・・・・・・・・・・・・・・・・・・・・・・・・
Y|250|238|0.9803921568627451|0.9333333333333333|300|288
Y|251|239|0.984313725490196|0.9372549019607843|301|289
Y|252|239|0.9882352941176471|0.9372549019607843|302|289
Y|253|241|0.9921568627450981|0.9450980392156862|303|291
Y|254|241|0.996078431372549|0.9450980392156862|304|291
Y|255|242|1.0|0.9490196078431372|305|292

-----

----------
6 [ VaryColorUsingToneCurve モジュール ]

ファイル名 [ VaryColorUsingToneCurve.py ]

----------


import  sys
import  os
import  cv2

import  OneCurveData
import  ImageDataTwoDimensionHandlingV3
import  ErrorProcess

#------------------------------

MODULE_NAME = "VaryColorUsingToneCurve"
function_name = "VaryColorUsingToneCurve"

global_ins_ImageDataTwoDimensionHandling_for_input = ""
global_width_of_ImageData_for_input = 0
global_height_of_ImageData_for_input = 0
global_ins_ImageDataTwoDimensionHandling_for_output = ""

global_path_of_top_directory  = "E:ForPython/ForTest/Test_A/"
global_path_of_sub_directory_input_ImageData = "Input/"
global_list_abs_path_of_input_ImageData = [ ]
global_number_of_files_in_directory = 0
global_abs_path_of_input_ImageData = ""

global_path_of_sub_directory_output_ImageData = "Output/"

global_path_of_sub_directory_ToneCurve = "ToneCurve/"
global_path_of_sub_directory_ImageData = "ImageData/"
global_path_of_sub_directory_TextData = "TextData/"

global_list_OneCurveData = ""
global_value_of_JPEG_QUALITY = [ cv2.IMWRITE_JPEG_QUALITY , 90 ]

#------------------------------------------------
def  main ( )  :

    function_name = "main"

    print ( "==================================" )
    print ( "Enter into , Module = " + MODULE_NAME
                      + " , function = " + function_name )
    print ( "==================================" )

    prepare_ToneCurve_Data  (  )
    number_of_all_files_for_input =  \
            make_list_of_all_file_path_for_input ( )
    for  file_order_number  in  range ( 1  ,  ( number_of_all_files_for_input + 1 ) ) :

        print ( "==================================" )
        print ( "Module = " + MODULE_NAME
                      + " , function = " + function_name )
        print ( "file_order_number = " , file_order_number )
        print ( "==================================" )

        prepare_input_and_output_ImageData (  file_order_number  )
        vary_color (  )
        output_ImageData ( )

    print ( "==================================" )
    print ( "Exit from , Module = " + MODULE_NAME
                      + " , function = " + function_name )
    print ( "==================================" )

#-------------------------------
def  vary_color ( )  :

    function_name = "vary_color"

    print ( "==================================" )
    print ( "Enter into , Module = " + MODULE_NAME
                      + " , function = " + function_name )
    print ( "==================================" )

    global  global_width_of_ImageData_for_input
    global  global_height_of_ImageData_for_input

    for x in range ( 0 , global_width_of_ImageData_for_input )  :
        for y in range ( 0 , global_height_of_ImageData_for_input )  :
            vary_color_of_one_pixel ( x , y )

    print ( "==================================" )
    print ( "Exit from , Module = " + MODULE_NAME
                      + " , function = " + function_name )
    print ( "==================================" )

#-------------------------------
def  vary_color_of_one_pixel (   \
                       arg_x_on_ImageData_space
                     , arg_y_on_ImageData_space
                                           )  :

    function_name = "vary_color_of_one_pixel"

    # print ( "==================================" )
    # print ( "Enter into , Module = " + MODULE_NAME
    #                   + " , function = " + function_name )
    # print ( "==================================" )

    global  global_ins_ImageDataTwoDimensionHandling_for_input
    global  global_ins_ImageDataTwoDimensionHandling_for_output

    list_color_before_varying =  \
           global_ins_ImageDataTwoDimensionHandling_for_input    \
                  .get_data_of_one_pixel ( \
                           MODULE_NAME
                         , function_name
                              # in the commonly used mathematical XY cordinates
                         , arg_x_on_ImageData_space
                         , arg_y_on_ImageData_space
                                                     )
        #-------------------------------
    list_color_after_varying  = [ 0 , 0 , 0 ]
    for  color_chanel_index  in range ( 0 , 3 )  :
        list_color_after_varying [ color_chanel_index ]    \
              = decide_color_for_one_color_channel (  \
                          list_color_before_varying [ color_chanel_index ]
                       ,  ( color_chanel_index + 1 )     # color_channel_number
                                                                       )

    # print ( "==================================" )
    # print ( "Module = " + MODULE_NAME
    #                   + " , function = " + function_name )
    # print ( "list_color_after_varying = " , list_color_after_varying )
    # print ( "==================================" )

         #-------------------------------
    global_ins_ImageDataTwoDimensionHandling_for_output    \
                  .put_data_of_one_pixel ( \
                           MODULE_NAME
                         , function_name
                              # in the commonly used mathematical XY cordinates
                         , arg_x_on_ImageData_space
                         , arg_y_on_ImageData_space
                         
                         , list_color_after_varying [ 0 ]  #arg_color_data_Blue
                         , list_color_after_varying [ 1 ]  #arg_color_data_Green
                         , list_color_after_varying [ 2 ]  #arg_color_data_Red
                                                     )

#-------------------------------
def   decide_color_for_one_color_channel (  \
                     arg_color_before_varying_of_one_color_channel
                  ,  arg_color_channel_number )  :

    function_name = "decide_color_for_one_color_channel"

    # print ( "==================================" )
    # print ( "Enter into , Module = " + MODULE_NAME
    #                   + " , function = " + function_name )
    # print ( "==================================" )

    global  global_list_OneCurveData
        #----------------------------------------
    list_of_ErrorMessage = [ " " , " " , " " , " " ]

    index_of_ToneCurve_data = \
           arg_color_before_varying_of_one_color_channel

    ins_OneCurveData = global_list_OneCurveData  \
                                        [ arg_color_channel_number - 1 ]
    color_after_varying_of_one_color_channel    \
        = ins_OneCurveData    \
                .get_x_and_y_in_TextData (  index_of_ToneCurve_data )
    value_y = color_after_varying_of_one_color_channel [ 1 ]

    # print ( "==================================" )
    # print ( "Module = " + MODULE_NAME
    #                    + " , function = " + function_name )
    # print ( " index_of_ToneCurve_data = " ,   \
    #                     index_of_ToneCurve_data )
    # print ( "color_after_varying_of_one_color_channel = " ,   \
    #                    color_after_varying_of_one_color_channel )
    # print ( "value_y = " ,  value_y )
    # print ( "==================================" )
                #--------------------------
    if ( ( value_y < 0 )                                  \
          or                                                    \
         ( value_y > 255 )                              \
       )  :
        list_of_ErrorMessage [ 0 ] =  "color value , invalid"
        list_of_ErrorMessage [ 1 ] = "arg_color_channel_number = "   \
                                                     + str ( arg_color_channel_number )
        list_of_ErrorMessage [ 2 ] = \
                     "arg_color_before_varying_of_one_color_channel = " \
                         + str (  arg_color_before_varying_of_one_color_channel )
        list_of_ErrorMessage [ 3 ] = \
                     "value_y = " \
                         + str ( value_y )
        ErrorProcess   \
                  .do_error_process (
                               MODULE_NAME
                             , function_name
                                       #arg_terminate_process_Y_or_N
                              , "Y"
                                              #arg_self.iv_list_of_ErrorMessage
                              , list_of_ErrorMessage
                                                           )
        #--------------------------
    return  value_y

#-------------------------------
def  output_ImageData (  ) :

    function_name = "output_ImageData"

    print ( "==================================" )
    print ( "Enter into , Module = " + MODULE_NAME
                      + " , function = " + function_name )
    print ( "==================================" )

    global  global_path_of_top_directory
    global  global_path_of_sub_directory_output_ImageData
    global  global_ins_ImageDataTwoDimensionHandling_for_output
    global  global_value_of_JPEG_QUALITY
    
    global  global_abs_path_of_input_ImageData

    file_name_of_input_ImageData =  \
               os.path.basename ( global_abs_path_of_input_ImageData )
    file_name_of_output_ImageData =  file_name_of_input_ImageData

    list_of_file_name_parts = os.path.splitext (  file_name_of_output_ImageData )
    part_1_of_file_name_output_ImageData = list_of_file_name_parts [ 0 ]
    part_2_of_file_name_output_ImageData = list_of_file_name_parts [ 1 ]

    abs_path_of_output_file = global_path_of_top_directory                                      \
                                               +  global_path_of_sub_directory_output_ImageData \
                                               +  file_name_of_output_ImageData
       #--------------------------------------
    list_of_ErrorMessage = [ " " , " " , " " , " " ]
    #---------------------------------------------------
    if (                                                         \
             ( part_2_of_file_name_output_ImageData == ".JPEG" )      \
         or ( part_2_of_file_name_output_ImageData == ".JPG" )      \
         or ( part_2_of_file_name_output_ImageData == ".jpg" )      \
         or ( part_2_of_file_name_output_ImageData == ".jpeg" )      \
       ) :
        image_data = global_ins_ImageDataTwoDimensionHandling_for_output   \
                                   .get_loaded_image_data (    \
                                           MODULE_NAME   # arg_requester_module
                                         , function_name    #arg_requester_function
                                                                      )
          #******** Use Directly , function of OpenCV2 ************
        cv2.imwrite ( \
                             abs_path_of_output_file
                         ,   image_data
                         ,   global_value_of_JPEG_QUALITY
                           )
        return
       #---------------------------------------------------
    if (                                                         \
             ( part_2_of_file_name_output_ImageData == ".PNG" )      \
         or ( part_2_of_file_name_output_ImageData == ".png" )      \
       ) :
            #---------------------------------
        global_ins_ImageDataTwoDimensionHandling_for_output  \
                 .write_loaded_image_data_to_directed_path ( \
                            MODULE_NAME
                         ,  function_name
                         ,  abs_path_of_output_file
                                                                                   )
        return
         #---------------------------------------
    list_of_ErrorMessage [ 0 ] =  "can not write , this file format"
    list_of_ErrorMessage [ 1 ] = "file ext = " + part_2_of_file_name_output_ImageData
    list_of_ErrorMessage [ 2 ] = "abs_path_of_output_file = "  \
                    +  abs_path_of_output_file
    list_of_ErrorMessage [ 3 ] = " "
    ErrorProcess   \
                  .do_error_process (
                               MODULE_NAME
                             , function_name
                                       #arg_terminate_process_Y_or_N
                              , "Y"
                                              #arg_self.iv_list_of_ErrorMessage
                              , list_of_ErrorMessage
                                                           )
#-------------------------------
def  make_list_of_all_file_path_for_input ( ) :

    function_name = "make_list_of_all_file_name_for_input"

    print ( "==================================" )
    print ( "Enter into , Module = " + MODULE_NAME
                      + " , function = " + function_name )
    print ( "==================================" )

    global  global_path_of_top_directory
    global  global_path_of_sub_directory_input_ImageData
    global  global_list_abs_path_of_input_ImageData
    global  global_number_of_files_in_directory

    list_dir_and_file = []
    global_list_abs_path_of_input_ImageData = [ ]

    directory_of_input_ImageData = global_path_of_top_directory   \
                                                      + global_path_of_sub_directory_input_ImageData
             #-------------------
    list_dir_and_file = \
            os.listdir ( directory_of_input_ImageData )
    for one_contained  in  list_dir_and_file :
        abs_path_of_contained = directory_of_input_ImageData + one_contained
        if ( ( os.path.isfile ( abs_path_of_contained ) ) == True ) :
                        # this is file
            global_list_abs_path_of_input_ImageData   \
                          .append ( abs_path_of_contained )
      #----------------------
    global_number_of_files_in_directory =   \
                 len ( global_list_abs_path_of_input_ImageData )

    print ( "==================================" )
    print ( "Exit from , Module = " + MODULE_NAME
                      + " , function = " + function_name )
    print ( "directory_of_input_ImageData = "  \
                          ,  directory_of_input_ImageData )
    print ( "global_list_abs_path_of_input_ImageData = " )
    print (  global_list_abs_path_of_input_ImageData )
    print ( "global_number_of_files_in_directory = "  \
                          , global_number_of_files_in_directory )
    print ( "==================================" )

    return  global_number_of_files_in_directory

#-------------------------------
def  prepare_input_and_output_ImageData (  arg_file_order_number  )  :

    function_name = "prepare_input_and_output_ImageData"

    print ( "==================================" )
    print ( "Enter into , Module = " + MODULE_NAME
                      + " , function = " + function_name )
    print ( "arg_file_order_number = " , arg_file_order_number )
    print ( "==================================" )

    global  global_ins_ImageDataTwoDimensionHandling_for_input
    global  global_width_of_ImageData_for_input
    global  global_height_of_ImageData_for_input
    global  global_ins_ImageDataTwoDimensionHandling_for_output

    global  global_list_abs_path_of_input_ImageData
    global  global_abs_path_of_input_ImageData
            #---------------------------------
    global_abs_path_of_input_ImageData = \
             global_list_abs_path_of_input_ImageData     \
                                      [ arg_file_order_number - 1 ]
            #------------------------------
    global_ins_ImageDataTwoDimensionHandling_for_input = \
              ImageDataTwoDimensionHandlingV3   \
                    .ImageDataTwoDimensionHandlingV3 ( \
                           MODULE_NAME
                         , function_name
                                                                             )
    global_ins_ImageDataTwoDimensionHandling_for_input  \
                 .load_image_data ( \
                           MODULE_NAME
                         , function_name
                         , global_abs_path_of_input_ImageData
                                             )
    global_width_of_ImageData_for_input = \
          global_ins_ImageDataTwoDimensionHandling_for_input \
                    .get_width_of_ImageData ( )
    global_height_of_ImageData_for_input = \
          global_ins_ImageDataTwoDimensionHandling_for_input \
                    .get_height_of_ImageData ( )
             #------------------------------
    global_ins_ImageDataTwoDimensionHandling_for_output = \
        ImageDataTwoDimensionHandlingV3   \
            .ImageDataTwoDimensionHandlingV3 ( \
                           MODULE_NAME
                         , function_name
                                                                    )
    global_ins_ImageDataTwoDimensionHandling_for_output  \
                 .load_image_data ( \
                           MODULE_NAME
                         , function_name
                              #**************************
                         , global_abs_path_of_input_ImageData
                              #**************************
                                  )

    print ( "==================================" )
    print ( "Exit from , Module = " + MODULE_NAME
                      + " , function = " + function_name )
    print ( "==================================" )

#-------------------------------
def  prepare_ToneCurve_Data  (  )  :

    function_name = "prepare_ToneCurve_Data"

    print ( "==================================" )
    print ( "Enter into , Module = " + MODULE_NAME
                      + " , function = " + function_name )
    print ( "==================================" )

    global  global_list_OneCurveData

    global  global_path_of_top_directory
    global  global_path_of_sub_directory_ToneCurve
    global  global_path_of_sub_directory_ImageData
    global  global_path_of_sub_directory_TextData
                 #----------------------
    list_file_name_ToneCurve_TextData = [ "ToneCurve_Blue.txt"     \
                                                             ,  "ToneCurve_Green.txt"
                                                             ,  "ToneCurve_Red.txt"  ]
                 #----------------------
    global_list_OneCurveData = [ ]
    for  i in range ( 0 , 3 ) :
                #---------------------
        abs_path_of_TextData    \
            = global_path_of_top_directory  \
                  + global_path_of_sub_directory_ToneCurve  \
                  + global_path_of_sub_directory_TextData   \
                  + list_file_name_ToneCurve_TextData [ i ]
        ins_OneCurveData = OneCurveData    \
                                               .OneCurveData ( )
        ins_OneCurveData   \
                .load_TextData ( abs_path_of_TextData )
                #----------------------------
        global_list_OneCurveData   \
               .append (  ins_OneCurveData )

#==============================
if __name__ == "__main__":
    main()


-----

----------
7 [ OneCurveData モジュール ]

ファイル名 [ OneCurveData.py ]

----------


#=============================
import  TextFileHandling
import  ImageDataTwoDimensionHandlingV3

#=============================
class  OneCurveData :

    CV_CLASS_NAME = "OneCurveData"

    CV_CHARACTER_USED_FOR_DELIMITER = "|"

#---------------------------------------
        # definition of constructer
    def __init__( self ) :

        methode_name = "constructer"

        print ( "==================================" )
        print ( "Enter into , Class = " + OneCurveData.CV_CLASS_NAME
                      + " , methode = " + methode_name )
        print ( "==================================" )

        print ( "==================================" )
        print ( "Exit from , Class = " + OneCurveData.CV_CLASS_NAME
                      + " , methode = " + methode_name )
        print ( "==================================" )

    #------------------------------------------------------------
    def  detect_Curve_in_ImageData_and_make_TextData ( self  \
                   ,  arg_abs_path_of_ImageData
                   ,  arg_abs_path_of_TextData

                   ,  arg_direction_of_detecting
                    , arg_list_from_to_x_y_of_detecting_area_in_ImageData
                    , arg_list_from_to_color_value
                                                                                   ) :

        methode_name = "detect_Curve_in_ImageData_and_make_TextData"

        print ( "==================================" )
        print ( "Enter into , Class = " + OneCurveData.CV_CLASS_NAME
                      + " , methode = " + methode_name )
        print ( "==================================" )

        self.iv_abs_path_of_ImageData = arg_abs_path_of_ImageData
        self.iv_abs_path_of_TextData = arg_abs_path_of_TextData
              #--------------------------
        self.iv_direction_of_detecting = arg_direction_of_detecting
        self.iv_list_from_to_x_y_of_detecting_area_in_ImageData \
                     = [ [ 0 , 0 ] , [ 0 , 0 ]  ]
        for i in range ( 0 , 2 ) :
            for j in range ( 0 , 2 ) :
                self.iv_list_from_to_x_y_of_detecting_area_in_ImageData [ i ]  [ j ] \
                    =  arg_list_from_to_x_y_of_detecting_area_in_ImageData [ i ]  [ j ]
             #-----------------------
        self.iv_width_of_detecting_area_in_ImageData =   \
                 self.iv_list_from_to_x_y_of_detecting_area_in_ImageData [ 1 ] [ 0 ]    \
                  -  self.iv_list_from_to_x_y_of_detecting_area_in_ImageData [ 0 ] [ 0 ]
        self.iv_height_of_detecting_area_in_ImageData =   \
                 self.iv_list_from_to_x_y_of_detecting_area_in_ImageData [ 1 ] [ 1 ]    \
                  -  self.iv_list_from_to_x_y_of_detecting_area_in_ImageData [ 0 ] [ 1 ]
              #--------------------------
        self.iv_list_from_to_color_value \
                     = [ [ 0 , 0 ] , [ 0 , 0 ] , [ 0 , 0  ] ]
        for i in range ( 0 , 3 ) :
            for j in range ( 0 , 2 ) :
                self.iv_list_from_to_color_value [ i ]  [ j ] \
                       =  arg_list_from_to_color_value [ i ]  [ j ]

        print ( "==================================" )
        print ( "Class = " + OneCurveData.CV_CLASS_NAME
                      + " , methode = " + methode_name )
        print ( "self.iv_abs_path_of_ImageData = " , self.iv_abs_path_of_ImageData )
        print ( "self.iv_abs_path_of_TextData = "   , self.iv_abs_path_of_TextData )
        print ( "self.iv_list_from_to_x_y_of_detecting_area_in_ImageData = " )
        print ( self.iv_list_from_to_x_y_of_detecting_area_in_ImageData )
        print ( "==================================" )

        self.prepare_ImageData ( )
        self.prepare_TextData ( )
        self.detect_Curve_and_output_TextData ( )
        self.terminate_TextData ( )

        print ( "==================================" )
        print ( "Exit from , Class = " + OneCurveData.CV_CLASS_NAME
                      + " , methode = " + methode_name )
        print ( "==================================" )

#-------------------------------------------------------
    def prepare_ImageData ( self ) :

        methode_name = "prepare_ImageData"

        print ( "==================================" )
        print ( "Enter into , Class = " + OneCurveData.CV_CLASS_NAME
                      + " , methode = " + methode_name )
        print ( "==================================" )

        self.ins_ImageDataTwoDimensionHandling = \
              ImageDataTwoDimensionHandlingV3   \
                    .ImageDataTwoDimensionHandlingV3 ( \
                           OneCurveData.CV_CLASS_NAME
                         , methode_name
                                                                             )
        self.ins_ImageDataTwoDimensionHandling  \
                 .load_image_data ( \
                           OneCurveData.CV_CLASS_NAME
                         , methode_name
                         , self.iv_abs_path_of_ImageData
                                             )
        self.iv_width_of_ImageData = \
                  self.ins_ImageDataTwoDimensionHandling \
                          .get_width_of_ImageData ( )
        self.iv_height_of_ImageData = \
                  self.ins_ImageDataTwoDimensionHandling \
                          .get_height_of_ImageData ( )

        print ( "==================================" )
        print ( "Module = " + OneCurveData.CV_CLASS_NAME
                      + " , methode = " + methode_name )
        print ( "self.iv_abs_path_of_ImageData = " \
                          , self.iv_abs_path_of_ImageData )
        print ( "self.iv_width_of_ImageData = " , self.iv_width_of_ImageData )
        print ( "self.iv_height_of_ImageData = " , self.iv_height_of_ImageData )
        print ( "==================================" )

        print ( "==================================" )
        print ( "Exit from , Class = " + OneCurveData.CV_CLASS_NAME
                      + " , methode = " + methode_name )
        print ( "==================================" )

      # ------------------------------------------------------------------------
    def  prepare_TextData ( self ) :

        methode_name = "prepare_TextData"

        print ( "==================================" )
        print ( "Enter into , Class = " + OneCurveData.CV_CLASS_NAME
                      + " , methode = " + methode_name )
        print ( "==================================" )

        self.ins_TextFileHandling = \
                 TextFileHandling   \
                        .TextFileHandling ( \
                                 OneCurveData.CV_CLASS_NAME
                               , methode_name
                               , self.iv_abs_path_of_TextData
                               , self.CV_CHARACTER_USED_FOR_DELIMITER
                                                  )
        self.ins_TextFileHandling   \
            .do_open_process_output_mode ( \
                                OneCurveData.CV_CLASS_NAME
                              , methode_name
                                                             )
               #--------------------------------------
        list_for_Text = [ " " , " " , " " , " "  , " " , " "  , " " ]

                 #-----------------------------------
        list_for_Text [ 0 ] = "=========================="
        self.ins_TextFileHandling   \
               .do_write_one_line_process ( \
                                  OneCurveData.CV_CLASS_NAME
                               ,  methode_name
                               ,  list_for_Text
                                                         )
             #-----------------------------------
        list_for_Text [ 0 ] = "abs_path_of_ImageData = "
        list_for_Text [ 1 ] = self.iv_abs_path_of_ImageData
        self.ins_TextFileHandling   \
               .do_write_one_line_process ( \
                                  OneCurveData.CV_CLASS_NAME
                               ,  methode_name
                               ,  list_for_Text
                                                         )
                #-----------------------------------
        list_for_Text [ 0 ] = "direction_of_detecting = "
        list_for_Text [ 1 ] = self.iv_direction_of_detecting
        self.ins_TextFileHandling   \
               .do_write_one_line_process ( \
                                  OneCurveData.CV_CLASS_NAME
                               ,  methode_name
                               ,  list_for_Text
                                                         )
               #-----------------------------------
        list_for_Text [ 0 ] = "list_from_to_x_y_of_detecting_area_in_ImageData = "
        list_for_Text [ 1 ] = str ( self.iv_list_from_to_x_y_of_detecting_area_in_ImageData [ 0 ] [ 0 ])
        list_for_Text [ 2 ] = str ( self.iv_list_from_to_x_y_of_detecting_area_in_ImageData [ 0 ] [ 1 ])
        list_for_Text [ 3 ] = str ( self.iv_list_from_to_x_y_of_detecting_area_in_ImageData [ 1 ] [ 0 ])
        list_for_Text [ 4 ] = str ( self.iv_list_from_to_x_y_of_detecting_area_in_ImageData [ 1 ] [ 1 ])
        self.ins_TextFileHandling   \
               .do_write_one_line_process ( \
                                  OneCurveData.CV_CLASS_NAME
                               ,  methode_name
                               ,  list_for_Text
                                                         )
             #-----------------------------------
        list_for_Text [ 0 ] = "list_from_to_color_value = "
        list_for_Text [ 1 ] = str ( self.iv_list_from_to_color_value [ 0 ] [ 0 ] )
        list_for_Text [ 2 ] = str ( self.iv_list_from_to_color_value [ 0 ] [ 1 ] )
        list_for_Text [ 3 ] = str ( self.iv_list_from_to_color_value [ 1 ] [ 0 ] )
        list_for_Text [ 4 ] = str ( self.iv_list_from_to_color_value [ 1 ] [ 1 ] )
        list_for_Text [ 5 ] = str ( self.iv_list_from_to_color_value [ 2 ] [ 0 ] )
        list_for_Text [ 6 ] = str ( self.iv_list_from_to_color_value [ 2 ] [ 1 ] )
        self.ins_TextFileHandling   \
               .do_write_one_line_process ( \
                                  OneCurveData.CV_CLASS_NAME
                               ,  methode_name
                               ,  list_for_Text
                                                         )
               #-----------------------------------
        list_for_Text [ 0 ] = "=========================="
        list_for_Text [ 1 ] = " "
        list_for_Text [ 2 ] = " "
        list_for_Text [ 3 ] = " "
        list_for_Text [ 4 ] = " "
        list_for_Text [ 5 ] = " "
        list_for_Text [ 6 ] = " "
        self.ins_TextFileHandling   \
               .do_write_one_line_process ( \
                                  OneCurveData.CV_CLASS_NAME
                               ,  methode_name
                               ,  list_for_Text
                                                         )

        print ( "==================================" )
        print ( "Exit from , Class = " + OneCurveData.CV_CLASS_NAME
                      + " , methode = " + methode_name )
        print ( "==================================" )

#------------------------------------------------
    def  terminate_TextData ( self ) :

        methode_name = "terminate_TextData"

        print ( "==================================" )
        print ( "Enter into , Class = " + OneCurveData.CV_CLASS_NAME
                      + " , methode = " + methode_name )
        print ( "==================================" )

        self.ins_TextFileHandling   \
                .do_close_process ( \
                                 OneCurveData.CV_CLASS_NAME
                               , methode_name
                                             )

        print ( "==================================" )
        print ( "Exit from , Class = " + OneCurveData.CV_CLASS_NAME
                      + " , methode = " + methode_name )
        print ( "==================================" )

#-------------------------------------------------------------------------
    def  detect_Curve_and_output_TextData ( self )  :

        methode_name = "detect_Curve_and_output_TextData"

        print ( "==================================" )
        print ( "Enter into , Class = " + OneCurveData.CV_CLASS_NAME
                      + " , methode = " + methode_name )
        print ( "==================================" )

        list_incliment_of_detecting_start_location = [ 0 , 0 ]
        if ( self.iv_direction_of_detecting == "X" ) :
                        #direction = along X-axis
            list_incliment_of_detecting_start_location [ 0 ] = 1
        else :
            list_incliment_of_detecting_start_location [ 1 ] = 1
       #---------------------------------
        list_detecting_start_location = [  \
                                 #x cordinate
                self.iv_list_from_to_x_y_of_detecting_area_in_ImageData [ 0 ] [ 0 ] \
                ,  \
                                 #y cordinate                
                self.iv_list_from_to_x_y_of_detecting_area_in_ImageData [ 0 ] [ 1 ] \
                                                     ]
                #---------------------------------------------------------
        result_of_detect = [ " " , 0 , 0 , 0.0 , 0.0 ]

        process_continue = "Y"
        while (   process_continue == "Y" ) :

            # print ( "==================================" )
            # print ( "Class = " + OneCurveData.CV_CLASS_NAME
            #           + " , methode = " + methode_name )
            # print ( "list_detecting_start_location = "
            #            , list_detecting_start_location )
            # print ( "==================================" )

            if (   \
                  ( list_detecting_start_location [ 0 ] > \
                                                                             #detecting_area,  to, x
                      self.iv_list_from_to_x_y_of_detecting_area_in_ImageData [ 1 ] [ 0 ] ) \
                  or   \
                  ( list_detecting_start_location [ 1 ] > \
                                                                             #detecting_area,  to, y
                      self.iv_list_from_to_x_y_of_detecting_area_in_ImageData [ 1 ] [ 1 ] ) \
                ) :
                 process_continue = "N"
                 break
                 #----------------------------
            result_of_detect = self.detect_along_one_line ( \
                                                self.iv_direction_of_detecting
                                             ,  list_detecting_start_location
                                                                                )
            self.ins_TextFileHandling   \
                    .do_write_one_line_process (    \
                                 OneCurveData.CV_CLASS_NAME
                               , methode_name   # arg_requester_function
                               , result_of_detect  # arg_list_values_for_output_one_line
                                                              )
                       #-----------------------
            list_detecting_start_location [ 0 ] += list_incliment_of_detecting_start_location [ 0 ]
            list_detecting_start_location [ 1 ] += list_incliment_of_detecting_start_location [ 1 ]

         #------------------------------------------------
    def detect_along_one_line ( self \
                               ,  arg_direction_of_detecting
                               ,  arg_list_detecting_start_location
                                            ) :

        methode_name = "detect_along_one_line"

        return_list = [ "N" , 0 , 0 ,  0.0 , 0.0 , 0 , 0 ]
                    #---------------------------------
        list_current_detecting_location_on_ImageData = [ \
                                        #x codinate
                     arg_list_detecting_start_location [ 0 ]   \
                                        #y codinate
                  ,  arg_list_detecting_start_location [ 1 ]   \
                                                                                 ]
                    #---------------------------------
        list_incliment_of_current_detecting_location = [ 0 , 0 ]
        if ( arg_direction_of_detecting == "X" ) :
                        #direction = along X-axis ( --> incliment y )
             list_incliment_of_current_detecting_location [ 1 ] = 1
        else :
                        #direction = along Y-axis ( --> incliment x )
             list_incliment_of_current_detecting_location [ 0 ] = 1
        #--------------------------------------------
        process_continue = "Y"
        while ( process_continue == "Y" ) :

            # print ( "==================================" )
            # print ( "Class = " + OneCurveData.CV_CLASS_NAME
             #          + " , methode = " + methode_name )
            # print ( "list_current_detecting_location_on_ImageData = "
            #            , list_current_detecting_location_on_ImageData )
            # print ( "==================================" )

            difference_x = list_current_detecting_location_on_ImageData [ 0 ]    \
                              -  self.iv_list_from_to_x_y_of_detecting_area_in_ImageData [ 0 ] [ 0 ]
            difference_y = list_current_detecting_location_on_ImageData [ 1 ]    \
                              -  self.iv_list_from_to_x_y_of_detecting_area_in_ImageData [ 0 ] [ 1 ]
            return_list [ 1 ] = difference_x
            return_list [ 2 ] = difference_y
              #---------------------------------
            rate_x = ( float ( difference_x ) )                 \
                         /                                               \
                         (  float (  self.iv_width_of_detecting_area_in_ImageData ) )
            rate_y = ( float (  difference_y ) )                 \
                         /                                               \
                         (  float (  self.iv_height_of_detecting_area_in_ImageData ) )
            return_list [ 3 ] = rate_x
            return_list [ 4 ] = rate_y
                         #-------------------------
            return_list [ 5 ] = list_current_detecting_location_on_ImageData [ 0 ]
            return_list [ 6 ] = list_current_detecting_location_on_ImageData [ 1 ]
                         #------------------------------------
            if (   \
                  ( list_current_detecting_location_on_ImageData [ 0 ] > \
                                                           #detecting_area,  to, x
                      self.iv_list_from_to_x_y_of_detecting_area_in_ImageData [ 1 ] [ 0 ] ) \
                or   \
                  ( list_current_detecting_location_on_ImageData [ 1 ] > \
                                                           #detecting_area,  to, x
                      self.iv_list_from_to_x_y_of_detecting_area_in_ImageData [ 1 ] [ 1 ] ) \
                ) :
                process_continue = "N"
                return  return_list
                 #----------------------------
            result_of_check_color_value = \
                    self.check_color_value ( \
                             list_current_detecting_location_on_ImageData )
            if (  result_of_check_color_value == "Y" ) :
                                 #found the location, that belongs to Curve
                         #------------------------------------
                return_list [ 0 ] = "Y"
                return  return_list
                                       #-----------------------
            list_current_detecting_location_on_ImageData [ 0 ] \
                       +=  list_incliment_of_current_detecting_location [ 0 ]
            list_current_detecting_location_on_ImageData [ 1 ] \
                       +=  list_incliment_of_current_detecting_location [ 1 ]

         #------------------------------------------------
    def  check_color_value ( self    \
                                      ,  arg_list_current_detecting_location ) :

        methode_name = "check_color_value"

        color_value = \
               self.ins_ImageDataTwoDimensionHandling \
                    .get_data_of_one_pixel ( \
                                OneCurveData.CV_CLASS_NAME
                             ,  methode_name
                              # in the commonly used mathematical XY cordinates
                             ,  arg_list_current_detecting_location [ 0 ]
                             ,  arg_list_current_detecting_location [ 1 ]
                                                       )
                   #-----------------
        return_value = "N"

        for i in range ( 0 , 3 ) :
                                                          # from value of color
            if (  color_value [ i ] <  self.iv_list_from_to_color_value [ i ] [ 0 ] ) :
                return  return_value
                                                          # to value of color
            if (  color_value [ i ] >  self.iv_list_from_to_color_value [ i ] [ 1 ] ) :
                return  return_value

        return_value = "Y"
        return  return_value

#==========================================
    def  load_TextData ( self       \
                         ,  arg_abs_path_of_TextData
                                 ) :

        methode_name = "load_TextData"

        print ( "==================================" )
        print ( "Enter into , Class = " + OneCurveData.CV_CLASS_NAME
                      + " , methode = " + methode_name )
        print ( "==================================" )

        self.ins_TextFileHandling = \
                 TextFileHandling   \
                        .TextFileHandling ( \
                                 OneCurveData.CV_CLASS_NAME
                               , methode_name
                               , arg_abs_path_of_TextData
                               , self.CV_CHARACTER_USED_FOR_DELIMITER
                                                  )
        self.ins_TextFileHandling   \
                 .do_open_process_input_mode (
                             OneCurveData.CV_CLASS_NAME
                           , methode_name
                                                             )
                #------ read 6 datas ( To put it in position, containing data for culculate )
        read_counter = 1
        while ( read_counter <= 6 ) :
            result_of_reading_TextData = \
                 self.ins_TextFileHandling   \
                        .do_read_one_line_process (
                                  OneCurveData.CV_CLASS_NAME
                                , methode_name
                                                               )
            read_counter += 1
       #-----------------------------------------------
        list_of_ErrorMessage = [ " " , " " , " " , " " ]

        self.iv_list_values_of_loaded_TextData = [ ]
        self.iv_length_of_list_values_of_loaded_TextData = 0

        process_continue = "Y"
        while ( process_continue == "Y" ) :
            result_of_reading_TextData = \
                 self.ins_TextFileHandling   \
                        .do_read_one_line_process (
                                  OneCurveData.CV_CLASS_NAME
                                , methode_name
                                                               )
            if ( result_of_reading_TextData [ 0 ] == "E" ) :
                           #reached to file end
                process_continue = "N"
                break
            #---------------------------------------------------
                #*********************************
            result_of_reading_TextData.pop ( 0 )
                #*********************************
            self.iv_list_values_of_loaded_TextData   \
                  .append ( result_of_reading_TextData )
             #----------------------------------
            found_or_not_CurveData =  \
                self.get_found_or_not_CurveData_in_TextData ( \
                               self.iv_length_of_list_values_of_loaded_TextData )
            if (  found_or_not_CurveData != "Y" ) :
                list_x_and_y_in_TextData = \
                        get_x_and_y_in_TextData ( \
                               self.iv_length_of_list_values_of_loaded_TextData )

                list_of_ErrorMessage [ 0 ] =  "abs_path_of_TextData = "   \
                                                                 + arg_abs_path_of_TextData
                list_of_ErrorMessage [ 1 ] = "found_or_not_CurveData = " \
                                                               + found_or_not_CurveData
                list_of_ErrorMessage [ 2 ] = "index = " + str (  \
                                self.iv_length_of_list_values_of_loaded_TextData )
                list_of_ErrorMessage [ 3 ] = "( x , y )  = ( "  \
                           + list_x_and_y_in_TextData [ 0 ]     \
                           + " , "                              \
                           + list_x_and_y_in_TextData [ 1 ]     \
                           + " )"
                ErrorProcess   \
                        .do_error_process (
                                         #arg_requester_module
                               OneCurveData.CV_CLASS_NAME
                                        #arg_requester_function
                             , methode_name
                                       #arg_terminate_process_Y_or_N
                              , "Y"
                                              #arg_self.iv_list_of_ErrorMessage
                              , list_of_ErrorMessage
                                                           )
           #-------------------------------------------------------
            self.iv_length_of_list_values_of_loaded_TextData += 1
           #-------------------------------------------------------------------

        print ( "==================================" )
        print ( "Exit from , Class = " + OneCurveData.CV_CLASS_NAME
                      + " , methode = " + methode_name )
        print ( "self.iv_length_of_list_values_of_loaded_TextData = "  \
                      , self.iv_length_of_list_values_of_loaded_TextData )
        print ( "==================================" )

#----------------------------------------------
    def  get_found_or_not_CurveData_in_TextData ( self  ,  arg_index ) :

        return_value = self.iv_list_values_of_loaded_TextData [ arg_index] [ 0 ]
        return  return_value

#----------------------------------------------
    def  get_x_and_y_in_TextData ( self  ,  arg_index ) :

        list_return = [  \
                                int ( self.iv_list_values_of_loaded_TextData [ arg_index] [ 1 ] )  \
                             ,  int ( self.iv_list_values_of_loaded_TextData [ arg_index] [ 2 ] ) \
                            ]
        return  list_return

#----------------------------------------------
    def  get_rate_x_and_rate_y_in_TextData ( self  ,  arg_index ) :

        list_return = [  \
                              float ( self.iv_list_values_of_loaded_TextData [ arg_index] [ 3 ] ) \
                          ,   float ( self.iv_list_values_of_loaded_TextData [ arg_index] [ 4 ] )  \
                            ]
        return  list_return

#----------------------------------------------
    def  get_x_and_y_on_ImageData_in_TextData ( self  ,  arg_index ) :

        list_return = [  \
                             int ( self.iv_list_values_of_loaded_TextData [ arg_index] [ 5 ] ) \
                         ,   int ( self.iv_list_values_of_loaded_TextData [ arg_index] [ 6 ] ) \
                            ]
        return  list_return


----------

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

この記事が気に入ったらサポートをしてみませんか?