首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >TypeError:片索引必须是整数或无或有__index__方法(Albumentations/NumPy)

TypeError:片索引必须是整数或无或有__index__方法(Albumentations/NumPy)
EN

Stack Overflow用户
提问于 2021-06-02 20:57:23
回答 1查看 723关注 0票数 0

大家好,你们能帮帮我吗?我得到了这个带有随机作物增强的虫子。TypeError:片索引必须是整数或无整数,或者有索引方法

密码在下面。

代码语言:javascript
运行
复制
!conda install -c conda-forge gdcm -y

import os

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

from PIL import Image
import cv2 as cv
import albumentations as A

import pydicom
from pydicom.pixel_data_handlers.util import apply_voi_lut

from tqdm.auto import tqdm

def read_img(path, voi_lut=True, fix_monochrome=True):
    dcm = pydicom.read_file(path)
    
    if voi_lut:
        img = apply_voi_lut(dcm.pixel_array, dcm)
    else:
        img = dcm.pixel_array
        
    if fix_monochrome and dcm.PhotometricInterpretation == "MONOCHROME1":
        img = np.amax(img) - img
        
    img = img - np.min(img)
    img = img / np.max(img)
    img = (img * 255).astype(np.uint8)
    
    return img

def resize_img(img, size, pad=True, resample=Image.LANCZOS):
    img = np.array(img)
    
    if pad:
        max_width = 4891
        max_height = 4891
        
        img = np.pad(img, ((0, max_height - img.shape[0]), (0, max_width - img.shape[1]), (0, 0)))
        
    img = img.resize((size, size), resample)
    
    return img

def augment_img(img, clahe=True, albumentations=True):
    if clahe:
        clahe = cv.createCLAHE(clipLimit=15.0, tileGridSize=(8,8))
        img = clahe.apply(img)
    else:
        img = cv.equalizeHist(img)
        
    if albumentations:
        img = np.stack((img, ) * 3, axis=-1)
        
        transform = A.Compose([
            A.RandomSunFlare(p=0.2), 
            A.RandomFog(p=0.2), 
            A.RandomBrightness(p=0.2),
            A.RandomCrop(p=1.0, width=img.shape[0] / 2, height=img.shape[1] / 2), 
            A.Rotate(p=0.2, limit=90),
            A.RGBShift(p=0.2), 
            A.RandomSnow(p=0.2),
            A.HorizontalFlip(p=0.2), 
            A.VerticalFlip(p=0.2), 
            A.RandomContrast(p=0.2, limit=0.2),
            A.HueSaturationValue(p=0.2, hue_shift_limit=20, sat_shift_limit=30, val_shift_limit=50)
        ])
        
        img = transform(image=img)["image"]
        
    return img

img = read_img('../input/siim-covid19-detection/test/00188a671292/3eb5a506ccf3/3dcdfc352a06.dcm') #You can replace this with any .dcm filepath on your system
img = augment_img(img)
img = resize_img(img, 1024)
plt.imshow(img, cmap='gray')

这是为了西姆·卡格尔的比赛。我不知道如何解决这个问题,问题是只有随机作物。我试着在网上搜索,但没能找到。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-03 05:20:47

我认为错误就在这一行:

代码语言:javascript
运行
复制
A.RandomCrop(p=1.0, width=img.shape[0] / 2, height=img.shape[1] / 2)

这里的问题是,您的宽度和高度可能不是整数,但它们必须是整数。

检查RandomCrop文档

,这里是解决方案.

  1. 显式地将宽度和高度参数转换为整数:
代码语言:javascript
运行
复制
A.RandomCrop(p=1.0, width=int(img.shape[0] / 2), height=int(img.shape[1] / 2))
  1. 使用整数除法:
代码语言:javascript
运行
复制
A.RandomCrop(p=1.0, width=img.shape[0] // 2, height=img.shape[1] // 2)

如果有帮助请告诉我!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67812484

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档