骨龄预测代码学习(二)

调试问题

scikit-image安装问题:

可能版本不兼容pycharm一开始安装scikit-image 0.18 1 报错

 DLL load failed while importing _remap: 找不到指定的模块

降低版本后解决


文件路径错误:

两个代码就差了个/
查半天查不出问题,简直了。。。。,我说怎么文件夹里面一直没有图片。路径最后要带一个/,代表该文件夹

target_folder = "/home/rsna_bone_age/DatasetTransform2/"
target_folder = "/home/rsna_bone_age/DatasetTransform2"

代码:

小tips:

img.shape[:2] 取彩色图片的长、宽。

img.shape[:3] 则取彩色图片的长、宽、通道。

img.shape[0]:图像的垂直尺寸(高度)

img.shape[1]:图像的水平尺寸(宽度)

img.shape[2]:图像的通道数

在矩阵中,[0]就表示行数,[1]则表示列数。

代码中引入的库函数:Python篇-图像读取-skimage.io.imsave&imread
skimage.transform模块实现图片缩放与形变.
Python中 [:,n] [n,:] [a:b,:] [:,a:b].


运行中警告⚠问题

float转为unit8,有可能会造成数据的损失,因此会有警告提醒
具体参考: 图像数据类型及颜色空间转换.

Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning.

代码详解:

import os
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
from operator import itemgetter

from skimage.transform import rescale, resize, downscale_local_mean#图像的形变与缩放
from skimage.io import imread, imsave#读/存图片



target_width = 384
target_heigth = 384
target_ratio = target_width/target_heigth

source_folder = "/home/rsna_bone_age/boneage-training-dataset/"
target_folder = "/home/rsna_bone_age/DatasetTransform2/"

sizes = []
sum_ratio = 0
nbriter = 0



for i in range(1378, 1380): #15610
    try:
        img = Image.open(source_folder + str(i) + ".png")
        img = np.asarray(img)#将输入转为矩阵格式。当输入是列表的时候,更改列表的值并不会影响转化为矩阵的值。
        
        heigth, width = img.shape
        ratio = width/heigth
        
        if heigth>width:
            #------------------------------------------------------------
            # Resize keeping width
            #------------------------------------------------------------
            new_heigth = int(img.shape[0]/(img.shape[1]/target_heigth))
            img_resized = resize(img, (new_heigth, target_width))
            heigth_resized, width_resized = img_resized.shape
            y1 = int((heigth_resized-target_heigth)/2)
            y2 = y1+target_heigth
            img_cropped = img_resized[y1:y2,0:target_width] # height, width 在切片,相当于在原有的像素矩阵中选区(384384)的子矩阵
            imsave(target_folder + str(i) + '.png', img_cropped)
        else:
            #------------------------------------------------------------
            # Resize keeping heigth
            #------------------------------------------------------------
            new_width = int(img.shape[1]/(img.shape[0]/target_width))
            img_resized = resize(img, (target_heigth, new_width))
            heigth_resized, width_resized = img_resized.shape
            x1 = int((width_resized-target_width)/2)
            x2 = x1+target_width
            img_cropped = img_resized[0:target_heigth,x1:x2]
            imsave(target_folder + str(i) + '.png', img_cropped)
        
        
    except:
        print("file doesn't exist")

#print("ratio average: " + str(sum_ratio/nbriter))
        }
a = np.array([[1,2,3,4], [3,4,5,6], [5,6,7,8], [7,8,9,10]])
b = a[1:3,1:3]
print(b)

[[4 5]
 [6 7]]

资源:

数据集及代码。。。上一篇文章给出了。over


猜你喜欢

转载自blog.csdn.net/qq_45392109/article/details/113704014