# Split a large image into many small images to get training data for a CNN

**URL:** <https://discourse.julialang.org/t/split-a-large-image-into-many-small-images-to-get-training-data-for-a-cnn/97853>\
**Category:** Machine Learning\
**Created:** [April 24, 2023, 11:33am UTC](https://discourse.julialang.org/t/split-a-large-image-into-many-small-images-to-get-training-data-for-a-cnn/97853 "2023-04-24T11:33:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kemper](https://avatars.discourse-cdn.com/v4/letter/k/b5ac83/32.png) [@Kemper](https://discourse.julialang.org/u/Kemper)\
**Post date:** [April 24, 2023, 11:33am UTC](https://discourse.julialang.org/t/split-a-large-image-into-many-small-images-to-get-training-data-for-a-cnn/97853/1 "2023-04-24T11:33:34Z")

</div>

Hey,

I Googled, but I think I miss the right terms to find what I am looking for:

I want to train a CNN to dedect landforms.  
I have a .shp file with the landforms for training and want to:

a: rasterize it  
b: split up the large image into many small, overlapping images .

Can someone give me a hint which package I can use to do something like that?

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [April 24, 2023, 5:43pm UTC](https://discourse.julialang.org/t/split-a-large-image-into-many-small-images-to-get-training-data-for-a-cnn/97853/2 "2023-04-24T17:43:39Z")

</div>

For the `shp` file you can use [GitHub - JuliaGeo/Shapefile.jl: Parsing .shp files in Julia](https://github.com/JuliaGeo/Shapefile.jl).

To chop images, you should be able to use Images.jl.

```julia
using Images
using TestImages

img = testimage("mandrill")

w, h = size(img)

ims = [@view img[iw:iw + 100, ih:ih + 100] 
    for iw in 1:100:w - 100, 
        ih in 1:100:h - 100]

for (i, im) in enumerate(ims)
     save("/tmp/im-$(lpad(i, 3, "0")).png", im)
end

```

 ![Screenshot 2023-04-24 at 18.40.18](https://global.discourse-cdn.com/julialang/original/3X/f/3/f315b721aaa58902f34182e3d6baea3bab78cc25.jpeg)

Obviously you’ll need to add code to specify overlaps, work out how many tiles you can get, allow for remainders, etc.

---

<div class="post-metadata">

**Author:** ![Kemper](https://avatars.discourse-cdn.com/v4/letter/k/b5ac83/32.png) [@Kemper](https://discourse.julialang.org/u/Kemper)\
**Post date:** [April 25, 2023, 9:12am UTC](https://discourse.julialang.org/t/split-a-large-image-into-many-small-images-to-get-training-data-for-a-cnn/97853/3 "2023-04-25T09:12:15Z")

</div>

Thank you so much!!

this helped a lot!!
