Let's go back to the image with the two cats we previewed recently. Here, we will create a new picture, which will only contain the cat on the right:
Our first step will be to identify an area of interest. We will do this by loading the image to Julia and checking its width and height:
using Images, ImageView
source_image = load("sample-images/cats-3061372_640.jpg");
size(source_image)
The size function will output (360, 640), which stands for 360px in height (y-axis) and 640px in width (x-axis). Both coordinates start from the top-left corner.
I have run a number of experiments and identified an area we are interested in—the height from 100 to 290 and the width from 280 to 540. You can try playing around with the following code to see how changing the region will affect the output:
cropped_image = img[100:290, 280:540];
imshow(cropped_image)
This will result in the following image being created and stored in the cropped_image variable. This will also allocate memory so that you can store the newly created image:
Images are vertical-major, which means that this first index corresponds to the vertical axis and the second to the horizontal axis. This might be different from other programming languages.
There is also another way to create a cropped image, which is by creating a view to the original image. Views don't create a new object or allocate memory, they just point to the original image (or array). They are great when you want to analyze or change a specific part of the picture without interfering with the rest of it:
cropped_image_view = view(img, 100:290, 280:540);
imshow(cropped_image_view)
If you run the preceding code, you will see that it returns an identical result. You can also save the image to disk without any problems.