[断面画像を重ね合わせて、物体を3次元表示], Python(パイソン) 使って作ってみた ,ソースプログラムリスト あり

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

-----

2024.8.23 presented in [note] ( //note.com/runningWater/ )
2024.9.14 プログラム改変に伴う、書き直しを行った

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

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

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

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

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

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

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

 ある物体を、様々な高さで輪切りにして、その断面を撮影した、というような画像の集合が、あるとして、
 それらを読み込み、合成し(高さの順に重ね合わせして)、その物体を、3次元表示する
 
と、いうようなプログラムを、作ってみた。

その動作の例を以降に記す。

[ある物体を、様々な高さで輪切りにして、その断面を撮影した、というような画像の集合]

として、50個の画像を作成し、それを、[テスト用画像集合] と、した。その一部を、下記に示す。

DrawingImageData_01.png

DrawingImageData_25.png

DrawingImageData_37.png

起動処理・サブグループ(後述)を、起動し

表示されるウィンドウに対して

[degree_XAxis_to_YAxis] , [degree_XAxis_to_ZAxis] に、それぞれの回転角度を入力し

[Set Angle] ボタンをクリックし

[Display Image] ボタンをクリックしたら、
下記のような表示状態となった。

Fig 1

[OK] ボタンをクリックしたら、3次元表示イメージを作成する処理が始まった。その後、下図に示したようなものが、画面上に表示された。

Fig 2

上図の右側に表示されているのが、[テスト用画像集合]を重ね合わせして作られた、3次元表示である。([山々に囲まれた中にある、湖]のようなイメージ)。

 [Z = n の平面] (Z軸に対して、垂直)に、[画像データ DrawingImageData_n.png] の、黒色でない部分が投影される、というような方式に、した。

 [degree_XAxis_to_YAxis] , [degree_XAxis_to_ZAxis] に、指定する角度を変えると、それに対応して、表示が変化するように、してある。

----------
3 このモジュール群の構成

以下のような、サブグループから、構成されている。

 起動処理・サブグループ
 パラメーター値・保持用・サブグループ
 仲介役・サブグループ
 データ入力用・サブグループ
 表示用・サブグループ
 バックヤード処理・サブグループ
 
これらに属するモジュールは、必要に応じて、下記に解説されているモジュールを、使用している。

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

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

Python(パイソン) ,ソースプログラムリスト あり ,プログラム作ってみた ,[XML データ を、読み込み、そこに記述されている内容を、利用可能な形にする] を 行う

[3次元の回転 回転後の位置座標算出]のためのクラスは、後述のようなものを使用した。( [MatrixFor3DRotaionV2] )

これらのサブグループについて、順次、以降に述べる。 

----------
4 起動処理・サブグループ

このサブグループは、下記に示すような、1個のモジュールより、構成される。このモジュールを起動することにより、モジュール群全体が、起動されることと、なる。

下記に、その内容を記した。

ファイル名 [StartProcess.py]

-----

import  ParametersContainer
import  DataInputFormV2
import  Mediator

#------------------------------
def func_start_process ( ) :

    MODULE_NAME = "StartProcess"

    function_name = "func_start_process"

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

        # get Parameters
    ParametersContainer    \
        .initialize ( )
    ParametersContainer    \
        .display_parameter_value ( )

    Mediator   \
         .initializing_process (  )

        # display form for input
    DataInputFormV2         \
        .display_form_for_intput ( )

    return

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

def main ( ) :
    func_start_process ( )

if __name__ == '__main__':
    main()

-----
 
----------
5 パラメーター値・保持用・サブグループ

下記に、その内容を記した。

[断面画像を重ね合わせて、物体を3次元表示]の , [パラメーター値・保持用・サブグループ] へ

----------
6 仲介役・サブグループ

下記に、その内容を記した。

[断面画像を重ね合わせて、物体を3次元表示]の , [仲介役・サブグループ] へ

----------
7 データ入力用・サブグループ

下記に、その内容を記した。

[断面画像を重ね合わせて、物体を3次元表示]の , [データ入力用・サブグループ] へ

----------
8 表示用・サブグループ

下記に、その内容を記した。

[断面画像を重ね合わせて、物体を3次元表示]の , [表示用・サブグループ] へ

----------
9 バックヤード処理・サブグループ

下記に、その内容を記した。

[断面画像を重ね合わせて、物体を3次元表示]の , [バックヤード処理・サブグループ] へ

----------
10 3次元の回転 回転後の位置座標算出

下記に、その内容を記した。

ファイル名 [ MatrixFor3DRotaionV2.py  ]

----------


import numpy as NUMPY
import math

#===========================================
class MatrixFor3DRotaionV2 :

    CV_CLASS_NAME = "MatrixFor3DRotaionV2"

    #------------------------------------------------------------
        # definition of constructer
    def __init__( self
                , arg_requester_module
                , arg_requester_function
                      #when  doing first rotation, around, what axis?
                , arg_first_rotation_around_axis
                      #when  doing first rotation, how dgree, used?
                , arg_first_rotation_angle_degree

                , arg_need_Y_N_second_rotation

                      #when  doing second rotation, around, what axis?
                , arg_second_rotation_around_axis
                      #when  doing second rotation, how dgree, used?
                , arg_second_rotation_angle_degree
                ) :

        methode_name = "constructer"

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

        self.iv_first_rotation_around_axis = arg_first_rotation_around_axis
        self.iv_first_rotation_angle_degree = \
                 arg_first_rotation_angle_degree
        self.iv_first_rotation_angle_radian  \
            = self.iv_first_rotation_angle_degree  \
                * ( math.pi / 180.0 )
        self.iv_ins_MatrixUsingNUMPY_for_first_rotation \
            = self.make_matrix ( \
                  self.iv_first_rotation_around_axis
                , self.iv_first_rotation_angle_radian
                           )
        self.iv_need_Y_N_second_rotation = arg_need_Y_N_second_rotation

        if ( self.iv_need_Y_N_second_rotation != "Y" ) :
            self.iv_ins_MatrixUsingNUMPY_for_rotation \
               = self.iv_ins_MatrixUsingNUMPY_for_first_rotation

            self.iv_second_rotation_around_axis = " "
            self.iv_second_rotation_angle_degree = 0
            self.iv_second_rotation_angle_radian = 0

               # ******************************************
            # print ( "self.iv_need_Y_N_second_rotation = " \
            #            , self.iv_need_Y_N_second_rotation )
               # ******************************************

            # print ( "self.iv_ins_MatrixUsingNUMPY_for_first_rotation = " )
            # print ( self.iv_ins_MatrixUsingNUMPY_for_first_rotation )
            # print ( "self.iv_ins_MatrixUsingNUMPY_for_rotation = " )
            # print (  self.iv_ins_MatrixUsingNUMPY_for_rotation )

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

            return

        else :   # need_Y_N_second_rotation
            self.iv_second_rotation_around_axis = \
                     arg_second_rotation_around_axis
            self.iv_second_rotation_angle_degree = \
                     arg_second_rotation_angle_degree
            self.iv_second_rotation_angle_radian  \
                = self.iv_second_rotation_angle_degree  \
                          * ( math.pi / 180.0 )

            self.iv_ins_MatrixUsingNUMPY_for_second_rotation \
                   = self.make_matrix ( \
                          self.iv_second_rotation_around_axis
                        , self.iv_second_rotation_angle_radian
                               )
                     # make matrix of multiplaying first and second rotation
            self.iv_ins_MatrixUsingNUMPY_for_rotation \
                = NUMPY.matmul (  \
                       self.iv_ins_MatrixUsingNUMPY_for_second_rotation
                     , self.iv_ins_MatrixUsingNUMPY_for_first_rotation
                               )

            # print ( "self.iv_need_Y_N_second_rotation = " \
            #                + self.iv_need_Y_N_second_rotation )

            # print ( "self.iv_ins_MatrixUsingNUMPY_for_second_rotation = " )
            # print ( self.iv_ins_MatrixUsingNUMPY_for_second_rotation )
            # print ( "self.iv_ins_MatrixUsingNUMPY_for_first_rotation = " )
            # print ( self.iv_ins_MatrixUsingNUMPY_for_first_rotation )
            # print ( "self.iv_ins_MatrixUsingNUMPY_for_rotation = " )
            # print (  self.iv_ins_MatrixUsingNUMPY_for_rotation )

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

            return

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

    #------------------------------------------------------------
    def  calculate_cordinate_after_rotation ( self   \
               , arg_list_cordinate_before_rotation
                                            ) :

        methode_name = "calculate_cordinate_after_rotation"

        # print ( "==================================" )
        # print ( "Enter into  , Class = " +  MatrixFor3DRotaion.CV_CLASS_NAME
        #               + " , methode = " + methode_name )
        # print ( "    arg_list_cordinate_before_rotation = " )
        # print ( arg_list_cordinate_before_rotation )
        # print ( "==================================" )

        cordinate_before_NUMPY =   \
              NUMPY.array (        \
                  [ \
                    [ arg_list_cordinate_before_rotation [ 0 ] ] \
                  , [ arg_list_cordinate_before_rotation [ 1 ] ] \
                  , [ arg_list_cordinate_before_rotation [ 2 ] ] \
                  ] \
                          )

        return_matrixUsingNUMPY = NUMPY.matmul (   \
                 self.iv_ins_MatrixUsingNUMPY_for_rotation
                ,cordinate_before_NUMPY
                 )

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

        # print ( "cordinate_before_NUMPY = " )
        # print ( cordinate_before_NUMPY )

        # print ( "self.iv_ins_MatrixUsingNUMPY_for_rotation = " )
        # print ( self.iv_ins_MatrixUsingNUMPY_for_rotation )

        # print ( "return_matrixUsingNUMPY = " )
        # print ( return_matrixUsingNUMPY )

        # print ( "************************" )
        # print ( "matmul done" )
        # print ( "************************" )

        #*********************************
        return_list = [  \
                         float ( return_matrixUsingNUMPY [ 0 ] [ 0 ] ) \
                      ,  float ( return_matrixUsingNUMPY [ 1 ] [ 0 ] ) \
                      ,  float ( return_matrixUsingNUMPY [ 2 ] [ 0 ] ) \
                      ]

     #   return  return_matrixUsingNUMPY
        return return_list
        #*********************************

    #------------------------------------------------------------
    def  make_matrix ( self   \
                    , arg_rotation_around_axis
                    , arg_rotation_angle_radian
                                            ) :

        methode_name = "make_matrix"

        rotation_matrix_value = [
                                  [ 1 , 0 , 0 ] ,
                                  [ 0 , 1 , 0 ] ,
                                  [ 0 , 0 , 1 ]
                                ]

        if ( arg_rotation_around_axis == "Z" ) :
                              #rotation  around Z axis
                              # direction X axis --> Y axis

                      #--------- row 1 ---------------
            rotation_matrix_value [ 1 - 1 ] [ 1 - 1 ]    \
                      = math.cos ( arg_rotation_angle_radian )
            rotation_matrix_value [ 1 - 1  ] [ 2 - 1 ]  \
                      = ( 0.0 - math.sin ( arg_rotation_angle_radian ) )
            rotation_matrix_value [ 1 - 1 ]  [ 3 - 1 ]  \
                      = 0.0
                      #---------- row 2 --------------
            rotation_matrix_value [ 2 - 1 ]  [ 1 - 1 ]   \
                         = math.sin ( arg_rotation_angle_radian )
            rotation_matrix_value [ 2 - 1 ]  [ 2 - 1 ]    \
                         = math.cos ( arg_rotation_angle_radian )
            rotation_matrix_value [ 2 - 1 ]  [ 3 - 1 ]     \
                         = 0.0
                     #---------- row 3 --------------
            rotation_matrix_value [ 3 - 1 ]  [ 1 - 1 ]    \
                         = 0.0
            rotation_matrix_value [ 3 - 1 ]  [ 2 - 1 ]  \
                         = 0.0
            rotation_matrix_value [ 3 - 1 ]  [ 3 - 1 ]   \
                         = 1.0

            return_matrixUsingNUMPY   \
                = NUMPY.array ( rotation_matrix_value )
            return  return_matrixUsingNUMPY

        #---------------------------------------------------
        if ( arg_rotation_around_axis == "Y" ) :
                              #rotation  around Y axis
                              # direction X axis --> Z axis

                      #--------- row 1 ---------------
            rotation_matrix_value [ 1 - 1 ]   [ 1 - 1 ]    \
                         = math.cos ( arg_rotation_angle_radian )
            rotation_matrix_value [ 1 - 1  ]    [ 2 - 1 ]     \
                         = 0.0
            rotation_matrix_value [ 1 - 1 ]  [ 3 - 1 ]      \
                         = math.sin ( arg_rotation_angle_radian )
                      #---------- row 2 --------------
            rotation_matrix_value [ 2 - 1 ]   [ 1 - 1 ]    \
                         = 0.0
            rotation_matrix_value [ 2 - 1 ]   [ 2 - 1 ]    \
                         = 1.0
            rotation_matrix_value [ 2 - 1 ]   [ 3 - 1 ]      \
                         = 0.0
                     #---------- row 3 --------------
            rotation_matrix_value [ 3 - 1 ]   [ 1 - 1 ]   \
                         = 0.0 - math.sin ( arg_rotation_angle_radian )
            rotation_matrix_value [ 3 - 1 ]   [ 2 - 1 ]    \
                         = 0.0
            rotation_matrix_value [ 3 - 1 ]   [ 3 - 1 ]    \
                         = math.cos ( arg_rotation_angle_radian )

            return_matrixUsingNUMPY   \
                = NUMPY.array ( rotation_matrix_value )
            return  return_matrixUsingNUMPY

        #---------------------------------------------------
        if ( arg_rotation_around_axis == "X" ) :
                              #rotation  around X axis
                              # direction Y axis --> Z axis

                      #--------- row 1 ---------------
            rotation_matrix_value [ 1 - 1 ]   [ 1 - 1 ]     \
                         = 1.0
            rotation_matrix_value [ 1 - 1  ]   [ 2 - 1 ]     \
                         = 0.0
            rotation_matrix_value [ 1 - 1 ]   [ 3 - 1 ]     \
                         = 0.0
                      #---------- row 2 --------------
            rotation_matrix_value [ 2 - 1 ]  [ 1 - 1 ]     \
                         = 0.0
            rotation_matrix_value [ 2 - 1 ]   [ 2 - 1 ]     \
                         = math.cos ( arg_rotation_angle_radian )
            rotation_matrix_value [ 2 - 1 ]   [ 3 - 1 ]     \
                         = 0.0 - math.sin ( arg_rotation_angle_radian )
                     #---------- row 3 --------------
            rotation_matrix_value [ 3 - 1 ]   [ 1 - 1 ]    \
                         = 0.0
            rotation_matrix_value [ 3 - 1 ]   [ 2 - 1 ]     \
                         = math.sin ( arg_rotation_angle_radian )
            rotation_matrix_value [ 3 - 1 ]    [ 3 - 1 ]     \
                         = math.cos ( arg_rotation_angle_radian )

            return_matrixUsingNUMPY   \
                = NUMPY.array ( rotation_matrix_value )
            return  return_matrixUsingNUMPY

        #-----------------------------------------
        return_matrixUsingNUMPY   \
                = NUMPY.array ( rotation_matrix_value )
        return  return_matrixUsingNUMPY

----------

----------

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


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