# Remove borders in a series of images

I have a series of mouse MRI images, here are two

How can I automatically remove the metal cage around the mouse to make it look like this:

The position of the cage is not always in exactly the same position. Making the image more horizontal would also be nice. Also each image should have the same dimensions.

image1 = Import["http://i.imgur.com/iGia7W6.jpg"]

-

You could use ImageLines to locate the straight edges:

image1 = Import["http://i.stack.imgur.com/xni6T.jpg"];
lines = ImageLines[EdgeDetect@image1, MaxFeatures -> 4];
Show[image1, Graphics[{Thick, Orange, Line /@ lines}]]


Then ImageRotate to straighten the image, and then re-detect the lines:

image1 = ImageRotate[image1, Pi - Mean[ArcTan @@@ Subtract @@@ lines], Full];
lines = ImageLines[EdgeDetect@image1, MaxFeatures -> 4];
Show[image1, Graphics[{Thick, Orange, Line /@ lines}]]


Then isolate the two inner lines and trim the image:

lines = SortBy[lines, #[[1, 2]] &][[{2, 3}]];
ImageTrim[image1, Flatten[lines, 1]]


-