Are you asking for an algorithm to detect a rectangle or how to set and unset Region of Interest(ROI)?
use
cvSetImageROI(img,cvRect(x,y,width,height));
to set ROI where img = image, x,y are anchor points for the rectangle
and
cvResetImageROI(img);
to unset ROI
Then all processing will take place within the specified region of interest (ROI).
Hope this helps