You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am encountering an issue where the warped GeoJSON annotation is misaligned and tilted after performing registration. The annotations should align directly over the target image, but they appear slightly offset and rotated. This misplacement is causing inaccuracies when overlaying annotations onto the registered images.
image_path = os.path.join(os.getcwd(), "2_image")
slide_src_dir = image_path
fixed_img = os.path.join(image_path, f"{sectionNo_fixed_img}.jpg") # Image associated with the annotations
moving_img = os.path.join(image_path, f"{sectionNo_moving_img}.jpg")
moving_img = load_image(moving_img)
fixed_img = load_image(fixed_img)
print('images loaded')
# Resize moving image to match fixed image dimensions
fixed_img = resize_image_to_match(moving_img, fixed_img)
registrar = registration.Valis(
slide_src_dir,
results_dst_dir,
non_rigid_registrar_cls=non_rigid_registrars.OpticalFlowWarper,
reference_img_f = f"{sectionNo_fixed_img}.jpg",
align_to_reference=True
)
try:
rigid_registrar, non_rigid_registrar, error_df = registrar.register()
print("Registration complete")
# Warp and save slides
registered_slide_dst_dir = os.path.join(os.getcwd(), "registered_slides")
registrar.warp_and_save_slides(registered_slide_dst_dir)
warp_annotations(biosample,sectionNo_moving_img, sectionNo_fixed_img, registrar)
finally:
registration.kill_jvm() # Ensure the JVM is killed`
`def warp_annotations(biosample,sectionNo_moving_img, sectionNo_fixed_img, registrar):
# Get GeoJSON data for annotations
image_path = os.path.join(os.getcwd(), "2_image")
annotation_img_f = os.path.join(image_path, f"{sectionNo_fixed_img}.jpg") # Image associated with the annotations
target_img_f = os.path.join(image_path, f"{sectionNo_moving_img}.jpg") # Target image for registration
res = getAtlasgeoJson(biosample, sectionNo_fixed_img)
gjson = res['msg'][0]['geoJson']
jp2_wid = res['msg'][0]['width']
jp2_hei = res['msg'][0]['height']
# Paths for GeoJSON files
GeoJson_path = os.path.join(os.getcwd(), "geoJson")
os.makedirs(GeoJson_path, exist_ok=True)
geojson_data = os.path.join(GeoJson_path, "annotation_ref.geojson")
geojson_validator = ValidateGeoJSON(geojson_data= gjson, jp2_wid=jp2_wid, jp2_hei=jp2_hei, convert_to_int=False, rotate_coords=False)
gjson_=geojson_validator.valid_geojson()
annotation_geojson_f = os.path.join(GeoJson_path, "annotation_ref_validated.geojson")
warped_geojson_annotation_f = os.path.join(GeoJson_path, "annotation_moving.geojson")
try:
# Save GeoJSON file
with open(annotation_geojson_f, 'w') as f:
json.dump(gjson_, f)
except Exception as e:
print(f"Error during geojson dumping: {e}")
return
try:
# Extract coordinates from the reference GeoJSON
annotation_pt_xy = extract_coordinates_from_geojson(annotation_geojson_f)
# Get slides for warping
annotation_source_slide = registrar.get_slide(annotation_img_f)
target_slide = registrar.get_slide(target_img_f)
# Warp the annotations to the target slide
annotations_on_target_slide_xy = annotation_source_slide.warp_xy_from_to(annotation_pt_xy, target_slide)
# Save the warped coordinates to a new GeoJSON file
save_coordinates_to_geojson(annotations_on_target_slide_xy, annotation_geojson_f, warped_geojson_annotation_f)
print("Warping complete, new GeoJSON saved.")
except Exception as e:
print(f"Error during warping: {e}")
def extract_coordinates_from_geojson(geojson_path):
# Upload the GeoJSON file
with open(geojson_path, 'r') as f:
geojson_data = json.load(f)
# Initialize a list to store all coordinates
all_coordinates = []
# Iterate over all features
for feature in geojson_data['features']:
geometry = feature['geometry']
if geometry['type'] == 'Polygon':
# Extract polygon coordinates
for polygon in geometry['coordinates']:
# Flatten the list of coordinates and add them to the main list
all_coordinates.extend(polygon)
elif geometry['type'] == 'MultiPolygon':
# Extract the coordinates of each polygon in the multipolygon
for multipolygon in geometry['coordinates']:
for polygon in multipolygon:
all_coordinates.extend(polygon)
# Convert list of coordinates to a numpy array
xy = np.array(all_coordinates)
return xy
def save_coordinates_to_geojson(coordinates, original_geojson_path, output_geojson_path):
# Load the original GeoJSON file to maintain its structure
with open(original_geojson_path, 'r') as f:
geojson_data = json.load(f)
# Iterate over features and update coordinates
coord_index = 0
for feature in geojson_data['features']:
geometry = feature['geometry']
if geometry['type'] == 'Polygon':
for polygon in geometry['coordinates']:
num_coords = len(polygon)
polygon[:] = coordinates[coord_index:coord_index + num_coords].tolist()
coord_index += num_coords
elif geometry['type'] == 'MultiPolygon':
for multipolygon in geometry['coordinates']:
for polygon in multipolygon:
num_coords = len(polygon)
polygon[:] = coordinates[coord_index:coord_index + num_coords].tolist()
coord_index += num_coords
# Save the new GeoJSON to a file
with open(output_geojson_path, 'w') as f:
json.dump(geojson_data, f, indent=2)`
The text was updated successfully, but these errors were encountered:
Hi @shubhankar-git,
Sorry you're running into this issue. It looks like the code you are using is based off Github issue 126. Is that correct? If so, could you let me know if you are getting a similar error when you use valis' annotation warping function (see example here).
I am encountering an issue where the warped GeoJSON annotation is misaligned and tilted after performing registration. The annotations should align directly over the target image, but they appear slightly offset and rotated. This misplacement is causing inaccuracies when overlaying annotations onto the registered images.
`def OpticalFlowWarper_(biosample, sectionNo_moving_img, sectionNo_fixed_img):
def save_coordinates_to_geojson(coordinates, original_geojson_path, output_geojson_path):
# Load the original GeoJSON file to maintain its structure
with open(original_geojson_path, 'r') as f:
geojson_data = json.load(f)
The text was updated successfully, but these errors were encountered: