...Mask in an image overlay? - MATLAB Answers - MATLAB Central
One further Question though. Is it possible to drag the lines? With your solution it is possible to change the rectangle by clicking a certain coordinate. It would be more convenient for the user to just click the existing line and drag it.