I'm trying to test my model for some pathology images. And I need to crop them into small patches.
Here is my cropping code.
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)
And the following is the method I used for save.
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.
my model is color sensitive, but I have no idea why one number matrix has different degrees of brightness.
I'll appreciate your directions.
The culprit here is the scipy.misc.toimage
function. According to the warning in the documentation of toimage
, this function uses bytescale
for scaling the array values to use the full range of a byte i.e. from 0 to 255. That is why the colors in the cropped image have better contrast.
If the image
variable is an array-like object (numpy
array etc.), then instead of using scipy.misc.toimage
followed by save
, you may use some other method to save the image to disk such as scipy.misc.imsave
of SciPy or imwrite
function of OpenCV.