【问题标题】:Why cropped images color go deeper?为什么裁剪后的图像颜色会变深?
【发布时间】:2019-11-29 04:13:39
【问题描述】:

我正在尝试针对一些病理图像测试我的模型。我需要将它们裁剪成小块。
这是我的裁剪代码。

def crop_to_four_with_cropSize(image, crop_sz=None):
    if crop_sz == None:
        crop_sz = image.shape[0] // 2
    img_sz = image.shape[0]
    y = 0
    x = 0
    h = crop_sz
    w = crop_sz
    image_crop_1 = image[y:y + h, x:x + w, :]
    image_crop_2 = image[-h:, x:x + w, :]
    image_crop_3 = image[y:y + h, -w:, :]
    image_crop_4 = image[-h:, -w:, :]
    return (image_crop_1, image_crop_2, image_crop_3, image_crop_4)

以下是我用来保存的方法。

def save_to_file(image, name, path='./'):
    if not os.path.exists(path):
        os.makedirs(path)
    full_name = os.path.join(path, name)
    scipy.misc.toimage(image).save(full_name)

left is orignal image,right is cropped image.

我的模型对颜色敏感,但我不知道为什么一个数字矩阵具有不同程度的亮度。

我会很感激你的指示。

【问题讨论】:

    标签: python crop


    【解决方案1】:

    这里的罪魁祸首是scipy.misc.toimage 函数。根据documentation of toimage 中的警告,此函数使用bytescale 缩放数组值以使用一个字节的整个范围,即从0 到255。这就是裁剪图像中的颜色具有更好对比度的原因。

    如果image 变量是类似数组的对象(numpy 数组等),那么您可以使用其他方法将图像保存到磁盘,而不是使用scipy.misc.toimage 后跟save作为 SciPy 的 scipy.misc.imsave 或 OpenCV 的 imwrite 函数。

    【讨论】:

    • 谢谢,我的朋友。 scipy.misc.imsave 不起作用(问题中的相同条件)但在 OpenCV 中的 imwrite 效果很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-24
    • 2018-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    相关资源
    最近更新 更多