You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python将电机采集的多张衍射图案拼接为马赛克图像?

衍射图案拼接为马赛克图像的问题

我通过移动电机采集了衍射图案数据,需要将这些图像拼接成一张马赛克图像,但目前实现效果不符合预期。尝试过识别衍射弧、取最大值等方法,均未得到理想结果,希望生成类似某PPT第13页的完整衍射环图像。

当前使用的代码如下:

import numpy as np
import cv2
import silx.io.h5py_utils
from PIL import Image

class BlissPath(object):
    def __init__(self, path_to_session=None, sample_dir_name=None, dataset_dir_name=None, detector_name=None):
        self.path_to_session = path_to_session
        self.sample_dir_name = sample_dir_name
        self.dataset_dir_name = dataset_dir_name
        self.detector_name = detector_name

class Scan(object):
    def __init__(self):
        self._motors = {}
        self._counters = {}
        self._det_images = None
        self.scanning_motors = []
        self._metadata = None
        self._default_scan_path = None
        self.scan = None

    def set_default_scan_path(self, value):
        self._default_scan_path = value

    def _load_from_bliss(self, scan_number, scan_path):
        file_name = f"{scan_path.path_to_session}/{scan_path.sample_dir_name}/{scan_path.sample_dir_name}_{scan_path.dataset_dir_name}/{scan_path.sample_dir_name}_{scan_path.dataset_dir_name}.h5"
        print(file_name)
        with silx.io.h5py_utils.File(file_name) as fp:
            scan_key = str(scan_number) + '.1'
            scan = fp[scan_key]
            print(scan_key)
            motors = list(fp[scan_key + '/instrument/positioners'].keys())
            for motor in motors:
                self._motors[motor] = fp[scan_key + '/instrument/positioners/' + motor][()]
            counters = list(fp[scan_key + '/measurement'].keys())
            for counter in counters:
                self._counters[counter] = fp[scan_key + '/measurement/' + counter][()]
            self._det_images = fp[scan_key + '/measurement/' + scan_path.detector_name][()]

    def load(self, scan_number, scan_path=None):
        if isinstance(scan_path, BlissPath):
            print(scan_path)
            self._load_from_bliss(scan_number, scan_path)

bliss_path = BlissPath(
    path_to_session="/home/nosu/summer23",
    sample_dir_name="CeO2_nist_2",
    dataset_dir_name="0001",
    detector_name="minipix1"
)

scan = Scan()
scan.load(3, scan_path=bliss_path)

# Stitching the images together as a mosaic
image_shape = scan._det_images.shape[1:]  # Assuming all images have the same shape
num_images = scan._det_images.shape[0]
num_columns = int(np.sqrt(num_images))
num_rows = int(np.ceil(num_images / num_columns))
reference_image = np.zeros((num_rows * image_shape[0], num_columns * image_shape[1]))
overlap_count = np.zeros_like(reference_image)

for i, image in enumerate(scan._det_images):
    row = i // num_columns
    col = i % num_columns
    start_row = row * image_shape[0]
    start_col = col * image_shape[1]
    reference_image[start_row: start_row + image_shape[0], start_col: start_col + image_shape[1]] += image
    overlap_count[start_row: start_row + image_shape[0], start_col: start_col + image_shape[1]] += (image > 0).astype(np.float32)

reference_image /= overlap_count  # Divide by the overlap count instead of the number of images

# Convert the reference image to PIL Image
reference_image = Image.fromarray(reference_image)

# Display the reference image
reference_image.show()

当前代码生成的图像存在拼接错位、衍射环断裂的问题,无法形成连续完整的衍射图案。


内容的提问来源于stack exchange,提问作者tyrannosaurus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 13:27:54