Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image alignment for multiple flight_captures is throwing an error "RuntimeError: context has already been set" #18

Closed
sadsel-AUT opened this issue Sep 2, 2018 · 1 comment

Comments

@sadsel-AUT
Copy link

sadsel-AUT commented Sep 2, 2018

I have done all the panel calibration etc and got the dls_correction from panel and DLS. I've worked through the Alignment.ipynb and wanted to replicate it for multiple flight_captures and it's not working. I pieced together all the code from Alignment.ipynb and added some minor modifications. I'd really appreciate if someone could tell me where I'm wrong. It's going through the captures and plotting the reflectance images but fails when it comes to alignment. Code below.

for cap1 in flight_captures:

cap1.plot_undistorted_reflectance(cap1.dls_irradiance()*dls_correction[0])
print("Aligning images. Depending on settings this can take from a few seconds to many minutes")

# Increase max_iterations to 1000+ for better results, but much longer runtimes
warp_matrices, alignment_pairs = imageutils.align_capture(cap1, max_iterations=200)
print("Finished Aligning, warp matrices:")
for i,mat in enumerate(warp_matrices):
    print("Band {}:\n{}".format(i,mat))
    
dist_coeffs = []
cam_mats = []
# create lists of the distortion coefficients and camera matricies
for i,img in enumerate(cap1.images):
    dist_coeffs.append(img.cv2_distortion_coeff())
    cam_mats.append(img.cv2_camera_matrix())
# cropped_dimensions is of the form:
# (first column with overlapping pixels present in all images, 
#  first row with overlapping pixels present in all images, 
#  number of columns with overlapping pixels in all images, 
#  number of rows with overlapping pixels in all images   )
cropped_dimensions = imageutils.find_crop_bounds(cap1.images[0].size(), 
                                             warp_matrices, 
                                             dist_coeffs, 
                                             cam_mats)
im_aligned = imageutils.aligned_capture(warp_matrices, alignment_pairs, cropped_dimensions)

# Create a normalized stack for viewing
im_display = np.zeros((im_aligned.shape[0],im_aligned.shape[1],5), dtype=np.float32 )

rows, cols, bands = im_display.shape
driver = gdal.GetDriverByName('GTiff')
fileno = 'test'+ '_' +'aligned' + '_' +j+'.tif'
j+1
outputImagePath = os.path.join('.','data', 'test_output')
outputImageName = os.path.join(outputImagePath, fileno)
print(outputImageName)
outRaster = driver.Create(**outputImageName**, cols, rows, bands, gdal.GDT_Float32)

for i in range(0,bands):
    outband = outRaster.GetRasterBand(i+1)
    outband.WriteArray(im_aligned[:,:,i])
    outband.FlushCache()

outRaster = None   

Error below

RuntimeError Traceback (most recent call last)
in ()
4 print("Aligning images. Depending on settings this can take from a few seconds to many minutes")
5 # Increase max_iterations to 1000+ for better results, but much longer runtimes
----> 6 warp_matrices, alignment_pairs = imageutils.align_capture(cap1, max_iterations=200)
7 print("Finished Aligning, warp matrices:")
8 for i,mat in enumerate(warp_matrices):

C:\imageprocessing-master\micasense\imageutils.py in align_capture(capture, ref_index, warp_mode, max_iterations, epsilon_threshold)
112
113 #required to work across linux/mac/windows, see https://stackoverflow.com/questions/47852237
--> 114 multiprocessing.set_start_method('spawn')
115 pool = multiprocessing.Pool(processes=multiprocessing.cpu_count())
116 for i,mat in enumerate(pool.imap_unordered(align, alignment_pairs)):

~\AppData\Local\Continuum\anaconda3\envs\micasense\lib\multiprocessing\context.py in set_start_method(self, method, force)
240 def set_start_method(self, method, force=False):
241 if self._actual_context is not None and not force:
--> 242 raise RuntimeError('context has already been set')
243 if method is None and force:
244 self._actual_context = None

RuntimeError: context has already been set

@poynting
Copy link
Contributor

poynting commented Sep 3, 2018

multiprocessing.set_start_method('spawn')

Can only be called once in a program. Remove it from the alignment function and set it before you run other code so that it's not in a loop. See also the previous issue which also encountered this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants