-1

I have a 800MB high resolution image in .png format. I want to create a training set of smaller images, say 15-20 images. How can I do that in python/matlab using some simple method?

Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143
  • Did you search SO? f.e. https://stackoverflow.com/questions/5953373/how-to-split-image-into-multiple-pieces-in-python – Patrick Artner Jun 07 '20 at 18:53
  • To be fair, the answers in the link are pretty horrible. There are more elegant solutions than using 4 nested for loops. – Nicolas Gervais Jun 07 '20 at 19:57
  • Do you mean you want to slice a big image into 5 images by 4 images? Or you want to resize the entire 800MB image down into 20 different images all containing the full picture at different sizes? – Mark Setchell Jun 08 '20 at 20:26
  • @MarkSetchell I want to perform Hough transform and certain Deep learning algorithms. I have a single large high-rez image, and I want to break it into smaller images/parts so that I have a set of images to perform my analysis. So, yes, I want to slice a big image into smaller pieces. – Divyang Soni Jun 12 '20 at 14:20

1 Answers1

1

I think the best way is currently skimage.util.view_as_blocks.

import numpy as np
from skimage.util.shape import view_as_blocks

A = np.arange(4*4).reshape(4,4)
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])
B = view_as_blocks(A, block_shape=(2, 2))
B[0, 0]

The previous line will select the square at row 0, column 0:

array([[0, 1],
       [4, 5]])
Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143
  • Thank you Nicolas, will surely try this solution and let you know... – Divyang Soni Jun 08 '20 at 19:27
  • Please don't feel I'm telling you or anybody else, what to do, but I have been making an effort to try and use non-square images lately, so that it is clearer to beginners/learners which dimension is which. I keep seeing 3x3 images with 3 RGB channels and can't help feeling it's far easier to see what's going on with a 5x4 image with 3 channels. – Mark Setchell Jun 08 '20 at 20:23
  • Great idea! I think the maintainers of `skimage` would love to hear your input on how their documentation can be improved! Here's how: https://scikit-image.org/docs/stable/contribute.html – Nicolas Gervais Jun 08 '20 at 21:33
  • I tried, but I seem to be making minor mistakes. I want to try it a few more times before I come back to you with an answer xD – Divyang Soni Jun 12 '20 at 14:22
  • @NicolasGervais i'm having problems loading the image into python. The image is a very high-rez satellite image, and there seem to be pixel limitations in PIL Image module. – Divyang Soni Jun 12 '20 at 14:40
  • oh. well i'm afraid i can only solve one problem at a time. you should maybe turn it into an array directly with cv2. i don't know – Nicolas Gervais Jun 12 '20 at 14:46