How to crop image and mask in patches?

I am using the classic CamVid dataset (http://mi.eng.cam.ac.uk/research/projects/VideoRec/CamSeq01/) for object segmentation. It is basically a dataset with videoframes (think of it just as images) and their corresponding segmentation mask.

I would like now to try something else: I would like to crop each frame and its segmentation mask in e.g. 100 patches centered at random positions from the image with a given size (e.g. 224*224) and save the results in the corresponding image / label folders. Note that both the image and the label have to be cropped in the same way for it to work. Does anybody know how to achieve this with few lines of code?

You could use the functional API of torchvision.transforms as described in this post.

@ptrblck RandomResizeCrop works but RandomCrop returns me an error…

Your code snippet won’t crop the images, as

  • you have an assert statement, which checks both image shapes are equal
  • you calling get_params with the same input and output size, thus it will be static and return the parameters to use the complete image (line of code)

but @ptrblck both images (mask and image) do have the same size (for this reason the assert does not fail). Moreover, get_params is getting the new size passed (224, 320)… isn’t it?

You should pass inputSize to the transformation to samples from [224, 320].