已解决:Python:裁剪图像被一个像素关闭
我正在编写一个程序,以分析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)
IndexErrorEvent: 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)
IndexErrorEvent: 17 Section Shape: (94, 83, 3) Membranal_cropped Shape: (93, 82)
IndexErrorEvent: 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论