已解决:Python:裁剪图像被一个像素关闭

发布于 2025-02-07 17:31:52 字数 1416 浏览 3 评论 0原文

我正在编写一个程序,以分析Mikroskopy图像的细胞。在某一时刻,我需要裁剪二进制面膜(“膜”),因此它的形状与我想在(“部分”)上使用蒙版的图像部分相同。两者都是numpy数组,因此我用[y_min:y_max,x_min:x_max]将蒙版用y_max和y_max和x_max基于section.shape将其切成薄片。 对于某些口罩而言,这可以正常工作,对于某些掩码,形状被一个像素熄灭,后来会导致indexError。

那么,为什么有些口罩没有我以前刚刚定义过一行的形状呢? 这是一些奇怪的内部舍入错误还是我做错了什么?

一个代码剪切看起来像这样:

for counter, event in enumerate(image_event_list):
    [...calculating x/y coordinates and stuff...]
    section = image[ymin:ymax, xmin:xmax]
    membranal_cropped = membranal[0:section.shape[0], 0:section.shape[1]]
    print("Event:", counter, "Section Shape:", section.shape, "Membranal_cropped Shape:", membranal_cropped.shape)
    try:
        section_intensity = np.mean(section[membranal_cropped>0])
        print("No error, working fine.")
    except IndexError:
        print("IndexError")

输出将是这样的:

  • 事件:4部分形状:(111,93,3)membranal_cropped形状:(111,92) indexError

  • 事件:6部分形状:(85,134,3)Membranal_cropped形状:(85,134) 没有错误,正常工作。

  • 事件:8部分形状:(158,169,3)membranal_cropped形状:( 158,169) 没有错误,正常工作。

  • 事件:10部分形状:(74,97,3)membranal_cropped形状:(74,97) 没有错误,正常工作。

  • 事件:13部分形状:(109,106,3)membranal_cropped形状:(109,105) indexError

  • 事件:17部分形状:(94,83,3)膜_Cropped形状:(93,82) indexError

  • 事件:23截面形状:(82,110,3)Membranal_cropped形状:(82,110) 没有错误,正常工作。

抱歉,我无法提供完整的代码。

I'm writing a programm to analyse cells from mikroskopy images. At one point I need to crop a binary mask ("membranal") so it has the same shape as the image section I would like to use the mask on ("section"). Both are numpy arrays, so I slice the mask with [y_min:y_max, x_min:x_max] with y_max and x_max based on section.shape.
For some masks this works just fine and for some the shape is off by one pixel, which causes an IndexError later.

So why do some masks don't have the shape I just defined one line before?
Is this some weird internal rounding error or what do I do wrong?

A code snipped looks like this:

for counter, event in enumerate(image_event_list):
    [...calculating x/y coordinates and stuff...]
    section = image[ymin:ymax, xmin:xmax]
    membranal_cropped = membranal[0:section.shape[0], 0:section.shape[1]]
    print("Event:", counter, "Section Shape:", section.shape, "Membranal_cropped Shape:", membranal_cropped.shape)
    try:
        section_intensity = np.mean(section[membranal_cropped>0])
        print("No error, working fine.")
    except IndexError:
        print("IndexError")

Output would be like this:

  • Event: 4 Section Shape: (111, 93, 3) Membranal_cropped Shape: (111, 92)
    IndexError

  • Event: 6 Section Shape: (85, 134, 3) Membranal_cropped Shape: (85, 134)
    No error, working fine.

  • Event: 8 Section Shape: (158, 169, 3) Membranal_cropped Shape: (158, 169)
    No error, working fine.

  • Event: 10 Section Shape: (74, 97, 3) Membranal_cropped Shape: (74, 97)
    No error, working fine.

  • Event: 13 Section Shape: (109, 106, 3) Membranal_cropped Shape: (109, 105)
    IndexError

  • Event: 17 Section Shape: (94, 83, 3) Membranal_cropped Shape: (93, 82)
    IndexError

  • Event: 23 Section Shape: (82, 110, 3) Membranal_cropped Shape: (82, 110)
    No error, working fine.

Sorry that I can't provide the full code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文