[画像中の、特定の色で塗りつぶされている部分の、境界部分の位置座標を、検出し、それを、テキストファイルに出力する] , Python(パイソン) 使って作ってみた ,ソースプログラムリスト あり

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

-----

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

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

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

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

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

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

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

このプログラムを、適用できないような形状をしている(画像中の)部分は、あるだろう。

使用例を使って、説明しよう。

下記のような画像を、テスト用に、用いた。

Fig 1

テスト用に作成したモジュール(後述)を起動したら、
そのモジュールから、[ MakeLocationDataFileOfCurve ](後述)が呼び出され、
そのモジュールが、上記画像の中の、赤色の部分を調べ、その境界に位置する箇所(複数)の、座標値を、算出し、それを、指定されたテキストファイルに、書き出した。

境界に位置する箇所の特定は、以下のようにして、行われた。

適当な位置に、[ 検出基準点 detecting_Center ] を、設定し、そこから、線を伸ばす。
その線の角度を変えながら、順次、その線が、赤色部分に[交差する箇所]を、調べていく。

下図では、緑色の箇所が、[検出基準点]を、黄色の矢印の先端が、[交差する箇所] を、示している。

Fig 2

----------

下記が、書き出されたテキストファイルの内容の一部である。

----------


==========================| | | | 
abs_path of Source Image Data File = |E:ForPython/ForTest/Test_11/ForTest/TestImageData_2.png| | | 
width, height , of ImageData = |1000|1000| | 
location_of_detecting_Center = |500|500| | 
detecting_line_length_incliment = |1.0| | | 
color_value_from = |0|0|200| 
color_value_to = |50|50|255| 
==========================| | | | 
2|0.0|0.0|1.0|0.0|1|1.0|332.0|0.0|832.0|500.0|389.0|0.0|889.0|500.0
2|0.5|0.008726646259971648|0.9999619230641713|0.008726535498373935|1|1.0(次行へ続く)
|332.98732038036906|2.9059363209585203|832.987320380369|502.9059363209585(次行へ続く)
|389.9851499950268|3.4033488443658344|889.9851499950269|503.40334884436584
2|1.0|0.017453292519943295|0.9998476951563913|0.01745240643728351|1|1.0(次行へ続く)
|332.9492824870783|5.811651343615409|832.9492824870783|505.8116513436154(次行へ続く)
|389.9406011109926|6.80643851054057|889.9406011109926|506.80643851054054

----------
テキストファイルの最初の方には、対象となった画像のファイル名や、赤色部分の検出のために設定した、パラメーター値が、記録されている。

その次に、検出された位置に関する情報が、順次、書き込まれている。("|" が、項目間の区切り記号)。その内容は、左から順に、

最終処理フェーズ番号
赤色部分の検出に使用した、角度(単位 度)
赤色部分の検出に使用した、角度(単位 ラジアン)
上記角度に対応する cos 値
上記角度に対応する sin 値
赤色部分の検出に使用した、線の長さの、初期値
赤色部分の検出に使用した、線の長さの、増分
赤色部分の内側の境界に位置する点の、x座標(設定した検出基準点からの相対座標値で)
赤色部分の内側の境界に位置する点の、y座標(設定した検出基準点からの相対座標値で)
赤色部分の内側の境界に位置する点の、x座標(画像上の座標値で)
赤色部分の内側の境界に位置する点の、y座標(画像上の座標値で)
赤色部分の外側の境界に位置する点の、x座標(設定した検出基準点からの相対座標値で)
赤色部分の外側の境界に位置する点の、y座標(設定した検出基準点からの相対座標値で)
赤色部分の外側の境界に位置する点の、x座標(画像上の座標値で)
赤色部分の外側の境界に位置する点の、y座標(画像上の座標値で)

----------

このプログラムは、適宜、下記で説明されているモジュールを、使用している。

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

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

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

----------
3 MakeLocationDataFileOfCurve

ファイル名 [ MakeLocationDataFileOfCurve.py ]

----------


import  math

import  ImageDataTwoDimensionHandlingV3
import  TextFileHandling

grobal_MODULE_NAME = "MakeLocationDataFileOfCurve"

#==============================
def  check_color_of_directed_pixel (   \
                     arg_ins_ImageDataTwoDimensionHandling
                  ,  arg_x  ,  arg_y
                  ,  arg_list_color_value_from
                  ,  arg_list_color_value_to
                                   ) :

    function_name = "check_color_of_directed_pixel"

#    print( "==================================" )
#    print( "Enter into , Module = "
#                   + grobal_MODULE_NAME
#                      + " , function = " + function_name )
#    print ( "    arg_x = " , arg_x )
#    print ( "    arg_y = " , arg_y )
#    print( "==================================" )

    list_return_value = [ "Y" , 0 , 0 , 0 , 0 , 0 ]

    list_color_value =  arg_ins_ImageDataTwoDimensionHandling    \
                                .get_data_of_one_pixel ( \
                                             grobal_MODULE_NAME
                                          ,  function_name
                                          ,  int ( arg_x )
                                          ,  int ( arg_y )
                                                        )

    list_return_value [ 4 ] = int ( arg_x )
    list_return_value [ 5 ] = int ( arg_y )
    list_return_value [ 1 ] = list_color_value [ 0 ]
    list_return_value [ 2 ] = list_color_value [ 1 ]
    list_return_value [ 3 ] = list_color_value [ 2 ]

    for index_color in range ( 0 , 3 )  :
        if (                                          \
              ( list_color_value [ index_color ]   \
                         < arg_list_color_value_from [ index_color ] )  \
              or  \
              ( list_color_value [ index_color ]   \
                         > arg_list_color_value_to [ index_color ] )  \
           ) :
            list_return_value [ 0 ] = "N"

            return   list_return_value

    return   list_return_value

    #==============================
def  check_color_of_directed_line (   \
                    arg_ins_ImageDataTwoDimensionHandling
                 ,  arg_angle_degree_X_to_Y
                 ,  arg_location_of_line_start
                 ,  arg_incliment_value_of_line_length
                 ,  arg_list_color_value_from
                 ,  arg_list_color_value_to
                                   ) :

    function_name = " check_color_of_directed_line"

    list_return_value = [ 0.0 ] * 15

       #----------------------------------
    width_of_ImageData \
           = arg_ins_ImageDataTwoDimensionHandling   \
                    .get_width_of_ImageData ( )
    height_of_ImageData \
           = arg_ins_ImageDataTwoDimensionHandling   \
                    .get_height_of_ImageData ( )

       #----------------------------------
    angle_radian_X_to_Y = arg_angle_degree_X_to_Y * ( math.pi / 180.0 )
    cos_angle_radian_X_to_Y = math.cos ( angle_radian_X_to_Y )
    sin_angle_radian_X_to_Y = math.sin ( angle_radian_X_to_Y )

    list_return_value [ 1 ] = arg_angle_degree_X_to_Y
    list_return_value [ 2 ] = angle_radian_X_to_Y
    list_return_value [ 3 ] = cos_angle_radian_X_to_Y
    list_return_value [ 4 ] = sin_angle_radian_X_to_Y

              #---------------------------------------
    detect_line_length_from = 1
    detect_line_length_incliment = arg_incliment_value_of_line_length
    list_return_value [ 5 ] = detect_line_length_from
    list_return_value [ 6 ] = detect_line_length_incliment
              #---------------------------------------
    detect_line_length = detect_line_length_from
    list_return_value [ 7 ] = -99999.0
    list_return_value [ 8 ] =  -99999.0
    list_return_value [ 9 ] =  -99999.0
    list_return_value [ 10 ] =  -99999.0
    list_return_value [ 11] = -99999.0
    list_return_value [ 12 ] =  -99999.0
    list_return_value [ 13 ] =  -99999.0
    list_return_value [ 14 ] =  -99999.0
              #---------------------------------------
    phase_number = 1

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

        list_return_value [ 0 ] = phase_number

        x_difference_location_of_line_start \
                        = detect_line_length * cos_angle_radian_X_to_Y
        y_difference_location_of_line_start \
                        = detect_line_length * sin_angle_radian_X_to_Y
        x_on_ImageData = arg_location_of_line_start [ 0 ] \
                            + x_difference_location_of_line_start
        y_on_ImageData = arg_location_of_line_start  [ 1 ] \
                            + y_difference_location_of_line_start

        if ( ( x_on_ImageData < 0 )   \
             or                                    \
             ( x_on_ImageData >= width_of_ImageData )   \
             or                                    \
             ( y_on_ImageData < 0 )   \
             or                                    \
             ( y_on_ImageData >= height_of_ImageData )   \
           )   :
            process_continue = "N"
            break

            #-----------------------------------
        result_of_check_color = check_color_of_directed_pixel (   \
                          arg_ins_ImageDataTwoDimensionHandling
                       ,  x_on_ImageData  ,  y_on_ImageData
                       ,  arg_list_color_value_from
                       ,  arg_list_color_value_to )

        if ( result_of_check_color [ 0 ] == "Y" ) :
                               #location ( of directed color ) was found
            w_phase_number = phase_number

            if ( w_phase_number == 1 ) :
                list_return_value [ 7 ] = x_difference_location_of_line_start
                list_return_value [ 8 ] = y_difference_location_of_line_start
                list_return_value [ 9 ] = x_on_ImageData
                list_return_value [ 10 ] = y_on_ImageData
                phase_number += 1

            if ( phase_number == 2 ) :
                list_return_value [ 11 ] = x_difference_location_of_line_start
                list_return_value [ 12 ] = y_difference_location_of_line_start
                list_return_value [ 13 ] =  x_on_ImageData
                list_return_value [ 14 ] =  y_on_ImageData

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

        detect_line_length += detect_line_length_incliment

         #----------------------------------------
    return   list_return_value

    #==============================
def  detect_Curve (   \
                          arg_ins_ImageDataTwoDimensionHandling
                       ,  arg_abs_path_of_ImageData

                       ,  arg_ins_TextFileHandling

                       ,  arg_list_location_of_detecting_Center
                       ,  arg_detecting_angle_degree_incliment
                       ,  arg_detecting_line_length_incliment
                       ,  arg_list_color_value_from
                       ,  arg_list_color_value_to
                   ) :

    function_name = "detect_Curve"

    arg_ins_TextFileHandling      \
          .do_open_process_output_mode (    \
                     grobal_MODULE_NAME    #arg_requester_module
                  ,  function_name       #arg_requester_function
                                      )
        #--------------------------------------
    list_for_Text = [ " " , " " , " " , " "  , " " ]

                 #-----------------------------------
    list_for_Text [ 0 ] = "=========================="
    arg_ins_TextFileHandling      \
               .do_write_one_line_process ( \
                     grobal_MODULE_NAME    #arg_requester_module
                  ,  function_name       #arg_requester_function
                  ,  list_for_Text
                                          )
             #-----------------------------------
    list_for_Text [ 0 ] = "abs_path of Source Image Data File = "
    list_for_Text [ 1 ] = arg_abs_path_of_ImageData
    arg_ins_TextFileHandling      \
               .do_write_one_line_process ( \
                     grobal_MODULE_NAME    #arg_requester_module
                  ,  function_name       #arg_requester_function
                  ,  list_for_Text
                                          )
             #--------------------------------------
    width_of_ImageData \
           = arg_ins_ImageDataTwoDimensionHandling   \
                    .get_width_of_ImageData ( )
    height_of_ImageData \
           = arg_ins_ImageDataTwoDimensionHandling   \
                    .get_height_of_ImageData ( )
    list_for_Text [ 0 ] = "width, height , of ImageData = "
    list_for_Text [ 1 ] = str ( width_of_ImageData )
    list_for_Text [ 2 ] = str ( height_of_ImageData )
    arg_ins_TextFileHandling      \
               .do_write_one_line_process ( \
                     grobal_MODULE_NAME    #arg_requester_module
                  ,  function_name       #arg_requester_function
                  ,  list_for_Text
                                          )
             #-----------------------------------
    list_for_Text [ 0 ] = "location_of_detecting_Center = "
    list_for_Text [ 1 ] = str ( arg_list_location_of_detecting_Center [ 0 ] )
    list_for_Text [ 2 ] = str ( arg_list_location_of_detecting_Center [ 1 ] )
    arg_ins_TextFileHandling      \
               .do_write_one_line_process ( \
                     grobal_MODULE_NAME    #arg_requester_module
                  ,  function_name       #arg_requester_function
                  ,  list_for_Text
                                          )
             #-----------------------------------
    list_for_Text [ 0 ] = "detecting_line_length_incliment = "
    list_for_Text [ 1 ] = str ( arg_detecting_line_length_incliment )
    list_for_Text [ 2 ] = " "
    arg_ins_TextFileHandling      \
               .do_write_one_line_process ( \
                     grobal_MODULE_NAME    #arg_requester_module
                  ,  function_name       #arg_requester_function
                  ,  list_for_Text
                                          )
              #---------------------------
    list_for_Text [ 0 ] = "color_value_from = "
    list_for_Text [ 1 ] = str ( arg_list_color_value_from [ 0 ] )
    list_for_Text [ 2 ] = str ( arg_list_color_value_from [ 1 ] )
    list_for_Text [ 3 ] = str ( arg_list_color_value_from [ 2 ] )
    arg_ins_TextFileHandling      \
               .do_write_one_line_process ( \
                     grobal_MODULE_NAME    #arg_requester_module
                  ,  function_name       #arg_requester_function
                  ,  list_for_Text
                                          )
              #---------------------------
    list_for_Text [ 0 ] = "color_value_to = "
    list_for_Text [ 1 ] = str ( arg_list_color_value_to [ 0 ] )
    list_for_Text [ 2 ] = str ( arg_list_color_value_to [ 1 ] )
    list_for_Text [ 3 ] = str ( arg_list_color_value_to [ 2 ] )
    arg_ins_TextFileHandling      \
               .do_write_one_line_process ( \
                     grobal_MODULE_NAME    #arg_requester_module
                  ,  function_name       #arg_requester_function
                  ,  list_for_Text
                                          )
               #-----------------------------------
    list_for_Text [ 0 ] = "=========================="
    list_for_Text [ 1 ] = " "
    list_for_Text [ 2 ] = " "
    list_for_Text [ 3 ] = " "
    arg_ins_TextFileHandling      \
               .do_write_one_line_process ( \
                     grobal_MODULE_NAME    #arg_requester_module
                  ,  function_name       #arg_requester_function
                  ,  list_for_Text
                                          )
               #-----------------------------------------

    detect_angle_degree = 0.0
    process_continue = "Y"

    while ( process_continue == "Y" ) :

        print( "==================================" )
        print( " Module = "                    \
                   + grobal_MODULE_NAME           \
                      + " , function = " + function_name )
        print ( "detect_angle_degree = " , detect_angle_degree )
        print( "==================================" )

        list_result_of_check_color   \
             = check_color_of_directed_line (   \
                          arg_ins_ImageDataTwoDimensionHandling
                       ,  detect_angle_degree
                       ,  arg_list_location_of_detecting_Center
                       ,  arg_detecting_line_length_incliment
                       ,  arg_list_color_value_from
                       ,  arg_list_color_value_to
                                             )
        arg_ins_TextFileHandling      \
               .do_write_one_line_process ( \
                     grobal_MODULE_NAME    #arg_requester_module
                  ,  function_name       #arg_requester_function
                  ,  list_result_of_check_color
                                          )

           #---------------------------------------------------
        w_next_angle = detect_angle_degree \
                            + arg_detecting_angle_degree_incliment
        if ( w_next_angle > 360.0 )   :
            process_continue = "N"
            break
            #-------------------------------------------------
        detect_angle_degree = w_next_angle

#--------------------------------------------
    arg_ins_TextFileHandling      \
          .do_close_process ( \
                     grobal_MODULE_NAME    #arg_requester_module
                  ,  function_name       #arg_requester_function
                                    )


----------
4 TestMakeLocationDataFileOfCurve

ファイル名 [ TestMakeLocationDataFileOfCurve.py ]

----------


import  ImageDataTwoDimensionHandlingV3
import  TextFileHandling

import  MakeLocationDataFileOfCurve

#======================================

# Test

module_name = "___"
function_name = "Test"


#***************************************

abs_path_of_ImageData_1 =  "E:ForPython/ForTest/Test_11/ForTest/TestImageData_2.png"
abs_path_of_TextData_1  =  "E:ForPython/ForTest/Test_11/ForTest/TestImageData_2.txt"

         #--------------------------------------------
ins_1_ImageDataTwoDimensionHandling   \
    = ImageDataTwoDimensionHandlingV3   \
             .ImageDataTwoDimensionHandlingV3 ( \
                 module_name
              ,  function_name )

ins_1_ImageDataTwoDimensionHandling    \
         .load_image_data ( \
                       module_name
                    ,  function_name
                     , abs_path_of_ImageData_1
                            )
width_of_ImageData = ins_1_ImageDataTwoDimensionHandling  \
                              .get_width_of_ImageData (  )

height_of_ImageData = ins_1_ImageDataTwoDimensionHandling  \
                              .get_height_of_ImageData (  )

       #--------------------------------------------
ins_1_TextFileHandling = \
                  TextFileHandling   \
                      .TextFileHandling (  \
                               module_name
                            ,  function_name
                            , abs_path_of_TextData_1
                            , "|"    # arg_character_used_for_delimiter
                                        )
       #--------------------------------------------
list_color_value_from = [ 0 ,    0 ,  200 ]
list_color_value_to  =  [ 50 ,  50 ,  255 ]

list_location_of_detecting_Center = [ 500 , 500 ]
detecting_angle_degree_incliment = 0.5
detecting_line_length_incliment = 1.0

MakeLocationDataFileOfCurve   \
    .detect_Curve (   \
                          ins_1_ImageDataTwoDimensionHandling
                        , abs_path_of_ImageData_1
                        , ins_1_TextFileHandling

                        , list_location_of_detecting_Center
                        ,  detecting_angle_degree_incliment
                        ,  detecting_line_length_incliment
                        ,  list_color_value_from
                        ,  list_color_value_to
                   )
    #------------------------------------------

-----

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

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