user1609160
user1609160

Reputation: 69

skimage.transform.rotate: getting a completely black image on saving

I am working with python 3.6 and performing following functions on an image (read image, pad image, crop image, rotate image). I am either using skimage or basic python function. If I don't rotate the image then there is no warning. But if I rotate the image, I am getting following warning

/anaconda3/lib/python3.6/site-packages/skimage/util/dtype.py:122: UserWarning: Possible precision loss when converting from int64 to float64 .format(dtypeobj_in, dtypeobj_out))

When I try to view the image using imshow, image values seemed to be in the range of (1e-17) (https://ibb.co/cSfzb7). I guess image values got normalized somewhere but I am not able to find the location.

If I try to save the rotated image, I get a completely black image.

im = img_as_uint(image)
imsave(image_path, im, plugin='imageio')

I tried removing img_as_uint, using default imsave, using (freeimage, imagio) as plugin option

/anaconda3/lib/python3.6/site-packages/skimage/util/dtype.py:122: UserWarning: Possible precision loss when converting from int64 to float64 .format(dtypeobj_in, dtypeobj_out))

/anaconda3/lib/python3.6/site-packages/skimage/util/dtype.py:122: UserWarning: Possible precision loss when converting from float64 to uint16 .format(dtypeobj_in, dtypeobj_out))

/anaconda3/lib/python3.6/site-packages/skimage/io/_io.py:132: UserWarning: /Users/ashisharora/google_Drive/madcat_arabic/lines/AAW_ARB_20061105.0017-S1_1_LDC0372_00z1.png is a low contrast image warn('%s is a low contrast image' % fname)

import sys
import argparse
import os
import numpy as np
import matplotlib
matplotlib.use('TkAgg')
import skimage
from skimage.io import imread, imsave, imshow
from skimage.transform import rotate
from skimage import img_as_uint

im_wo_pad = imread(image_file_name) 
im = pad_image(im_wo_pad) 
region_initial = im[281:480, 2509:4766] 
rotation_angle_in_rad = -0.00708 
img2 = rotate(region_initial, degrees(rotation_angle_in_rad)) 
region_final = img2[15:298, 7:2263] 
imshow(region_final)


def pad_image(image):
    offset = 200
    max_val = 255
    height = image.shape[0]
    im_pad = np.concatenate((max_val * np.ones((height, offset),
                                        dtype=int), image), axis=1)
    im_pad1 = np.concatenate((im_pad, max_val * np.ones((height, offset),
                                                 dtype=int)), axis=1)

    width = im_pad1.shape[1]

    im_pad2 = np.concatenate((max_val * np.ones((offset, width),
                                         dtype=int), im_pad1), axis=0)
    im_pad3 = np.concatenate((im_pad2, max_val * np.ones((offset, width),
                                                  dtype=int)), axis=0)
    return im_pad3

I tried solutions posted on stack overflow but it is not working for my case. I would appreciate if someone can help.

Upvotes: 4

Views: 2677

Answers (1)

blindmouse
blindmouse

Reputation: 91

By default, the skimage rotate function converts to float and scales the image values between 0 and 1. (See http://scikit-image.org/docs/dev/api/skimage.transform.html#skimage.transform.rotate)

Try using the preserve_range parameter and some type casting, e.g.,

img_rot = skimage.transform.rotate(img, rot_angle, preserve_range=True).astype(np.uint8)

Upvotes: 7

Related Questions