Forum

Author Topic: Align Images using Control points.  (Read 1979 times)

Aravinth

  • Newbie
  • *
  • Posts: 18
    • View Profile
Align Images using Control points.
« on: January 23, 2020, 04:20:47 PM »
Hello Everyone,
                      I have Aligned the images using 6 markers and projected the markers and I have got the errors for all the markers. In order to do a simulation process, I have align the images using the control points and get the errors for those.  Now I am trying to use 5 control points from the markers leaving each one at each iteration and trying to get the error for the 5 control points each time, but after running the first iteration I have got the marker error for 5 markers and I have got the error

<CoordinateSystem 'ETRS89 / UTM zone 32N (N-E) (EPSG::3044)'>

2020-01-24 14:53:56 point 3 0.005685494747012854 0.004827383905649185 0.0002521519620586332 0.00746271173922734

2020-01-24 14:53:56 point 4 -0.01812988135498017 -0.008003545925021172 0.0012451643736568485 0.019856983145407684

2020-01-24 14:53:56 point 6 -0.000979920499958098 0.0036176517605781555 -0.0008834924167473446 0.0038507411361324226

2020-01-24 14:53:56 point 2 0.028576436801813543 -0.009189705364406109 -0.001535830924112247 0.03005698257643467

2020-01-24 14:53:56 point 1 -0.015152139123529196 0.008748216554522514 0.0009220051803708884 0.017520522436737913

Traceback (most recent call last):

2020-01-24 14:53:56   File "E:/Softz/Align_Flir.py", line 225, in <module>

2020-01-24 14:53:56     error = estim - source

2020-01-24 14:53:56 AttributeError: invalid arguments

2020-01-24 14:53:56 Error: invalid arguments

The code is not proceeding after this, please kindly help What am I doing wrong in this. I have attached the code along with this mail Kindly help me, please.


Thank you, everyone.


Code: [Select]

import itertools
import glob
import time
import os

try:
    import Metashape as env
    import Metashape
    doc = Metashape.app.document
    env_type = 1
except:
    import PhotoScan as env
    doc = PhotoScan.app.document
    env_type = 2
   
# load_images = True
# load_thermal = True
# Thermal data
path_calib = os.path.abspath('.') #'D:\\my-jobs\\marker\\'
path_images = os.path.abspath('.') #'D:\\my-jobs\\marker\\'
path_subdir = 'flir'
fileext = '*.tiff'
chunk_name = 'FLIR'
calib_fname = 'xt2_flir_20190206.xml'
flength = 13
psize = 0.017

# ch = env.app.document.addChunk()
# ch.label = chunk_name

# initiate images list
imgList = []
imgListNames = []
# construct flight directory
dir_srs = os.path.join(path_images, path_subdir)


# reset file counter
# cnt = 0

# # go over all images
# for filename in glob.glob(dir_srs + fileext):
# # add image to list
#     imgList.append(filename)
#     fname = filename.split('\\')
#     # add file name
#     imgListNames.append(fname[-1])
#     #' increment counter
#     cnt += 1
   
# if load_images == 1:
#     # add images to chunk
#     ch.addPhotos(imgList)
# # create calibration instance
# calibData = env.Calibration()
# # load calibration data from file
# calibData.load(calib_fname, format=Metashape.CalibrationFormatXML)
# # load camera calibration params
# ch.sensors[0].user_calib = calibData
# # assign camera focal length
# ch.sensors[0].focal_length = flength
# # assign camera pixel size
# ch.sensors[0].pixel_size = env.Vector([psize,psize])
# # do not optimise camera intrinsics
# if env_type == 1:
#     ch.sensors[0].fixed_calibration = True
# else:
#     ch.sensors[0].fixed = True

# # ch = Metashape.app.document.chunk
# path = 'Marker_Try.txt'

# chunk = Metashape.app.document.chunk
# out_crs = Metashape.CoordinateSystem("EPSG::3044")
# for camera in chunk.cameras:
#     if camera.reference.location:
#         camera.reference.location = Metashape.CoordinateSystem.transform(camera.reference.location, chunk.crs, out_crs)
# chunk.crs = out_crs 
# ch.importReference(path, Metashape.ReferenceFormatCSV, delimiter=",", columns="nxyz", create_markers=True)

# ch.updateTransform()
         
# ch.matchPhotos(downscale=0.25, generic_preselection=False,reference_preselection=True)
# ch.alignCameras()


#Project markers
#ch = Metashape.app.document.chunk
# file = open('C:\\Program Files\\Agisoft\\Metashape Pro', "markers_projections_rgb_center.txt") #input file

print(os.getcwd())
r = open('Marker_Try.txt', 'r')
data_points = r.readlines()
r.close()

import os, tempfile

for ind in range (len(data_points)):
    #w = open('Marker_Try.txt', 'w')
    #w.writelines([data_points[k] for k in range(len(data_points)) if k != ind])
    #w.close()
    data_ = [data_points[k] for k in range(len(data_points)) if k != ind]
    load_images = True
    load_thermal = True

    ch = env.app.document.addChunk()
    ch.label = chunk_name

    tmp = tempfile.NamedTemporaryFile(delete=False)
    with open(tmp.name, 'w') as f:
        f.writelines(data_)
        f.close()
    cnt = 0
    # go over all images
    for filename in glob.glob(os.path.join(dir_srs,fileext)):
    # add image to list
        imgList.append(filename)
        fname = filename.split('\\')
        print(filename, fname)
        # add file name
        imgListNames.append(fname[-1])
        #' increment counter
        cnt += 1
   
    if load_images == 1:
        # add images to chunk
        ch.addPhotos(imgList)
    # create calibration instance
    calibData = env.Calibration()
    # load calibration data from file
    calibData.load(calib_fname, format=Metashape.CalibrationFormatXML)
    # load camera calibration params
    ch.sensors[0].user_calib = calibData
    # assign camera focal length
    ch.sensors[0].focal_length = flength
    # assign camera pixel size
    ch.sensors[0].pixel_size = env.Vector([psize,psize])
    # do not optimise camera intrinsics
    if env_type == 1:
        ch.sensors[0].fixed_calibration = True
    else:
        ch.sensors[0].fixed = True

    # ch = Metashape.app.document.chunk
    # path = 'Marker_Try.txt'
    path = tmp.name

    chunk = Metashape.app.document.chunk
    out_crs = Metashape.CoordinateSystem("EPSG::3044")
    for camera in chunk.cameras:
        if camera.reference.location:
            camera.reference.location = Metashape.CoordinateSystem.transform(camera.reference.location, chunk.crs, out_crs)
    chunk.crs = out_crs 
    ch.importReference(path, Metashape.ReferenceFormatCSV, delimiter=",", columns="nxyz", create_markers=True)

    ch.updateTransform()
           
    ch.matchPhotos(downscale=0.25, generic_preselection=False,reference_preselection=True)
    ch.alignCameras()
    ###################
    ch = Metashape.app.document.chunk
    file = open("Marker_center_Flir_Final.txt") #input file
    eof = False
    line = file.readline()
    if not len(line):
            eof = True
    while not eof:
        sp_line = line.rsplit(",", 3)   #splitting read line by four parts
        y = float(sp_line[3]) #y- coordinate of the current projection in pixels
        x = float(sp_line[2]) #x- coordinate of the current projection in pixels
        path = sp_line[0] #camera label
        marker_name = sp_line[1] #marker label
        flag = 0
        for i in range (len(ch.cameras)):

            if ch.cameras[i].label == path: #searching for the camera
                for j in range (len(ch.markers)): #searching for the marker (comparing with all the marker labels in chunk)
                    if ch.markers[j].label == marker_name:
                        ch.markers[j].projections[ch.cameras[i]] = Metashape.Marker.Projection(Metashape.Vector([x,y]), True) #setting up marker projection of the correct photo)
                        flag = 1
                        break
                #if ind != j:
                #    ch.cameras[j].enable = False
                if not flag:
                    marker = ch.addMarker()
                    marker.label = marker_name
                    marker.projections[ch.cameras[i]] =  Metashape.Marker.Projection(Metashape.Vector([x,y]), True)
                break
               
        line = file.readline() #reading the line from input file
        if not len(line):
            eof = True
            break # EOF
    file.close()


    doc = env.app.document
    env_type = 1
    #chunk = env.app.document.chunk
    #chunk = env.app.document.addChunk()
    chunk = Metashape.app.document.chunk
    #path = "Marker_Try.txt"
    path = tmp.name
    chunk.importReference(path, format = env.ReferenceFormatCSV, delimiter=",", columns="nxyz", create_markers=True)
    for camera in chunk.cameras:
        camera.reference.enabled = False
    chunk.updateTransform()


       
    print ("--------------- Markers import finished. ----------------------\n")
    print(str(chunk.crs))

    chunk = Metashape.app.document.chunk

    for marker in chunk.markers:
        source = marker.reference.location
        estim = chunk.crs.project(chunk.transform.matrix.mulp(marker.position))
        error = estim - source
        total = error.norm()
        print(marker.label, error.x, error.y, error.z, total)
    print('<<------------------------------------->>')
    time.sleep(20)

« Last Edit: January 24, 2020, 05:28:58 PM by Aravinth »

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 14927
    • View Profile
Re: Align Images using Control points.
« Reply #1 on: January 25, 2020, 04:51:05 PM »
Hello Aravinth,

The problem is that you are loading the source coordinate data for all but one markers, but trying to print out the errors for all the markers. Therefore marker.reference.location value for one of the markers is None and cannot be used to calculate the errors.
Best regards,
Alexey Pasumansky,
Agisoft LLC