I guess that you can improve this function using crop. I try to use your function firstly in an image about 100 MB I waited for 5 minutes and the matlab continued processing, there should be a way to improve it. Also I have tried using an image of only 10 MB and I got the same result. Any advises?