PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, July 27, 2022

[FIXED] Why cropped images color go deeper?

 July 27, 2022     crop, python     No comments   

Issue

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.


Solution

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.



Answered By - sgarizvi
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing