I want generate a random image using matlab which contains the object to be detected and the clutter images at random locations. I then want to detect this randomly generated ob
You may want to take a look at "Marching Squares" Algorithm. A well documented example is located here: http://devblog.phillipspiess.com/2010/02/23/better-know-an-algorithm-1-marching-squares/
The coordinates you obtain from the algorithm can then again be used as coordinates on where to place the contour pixels in another image.
Unfortunately, I cannot provide you with any Matlab-Code (just ported it to Fortran), but given the discussion of the algorithm, it should be straightforward to implement it.
Some advice from my experience in using the "Marching Squares":
edit: Have you seen isocontour in MatlabCentral? http://www.mathworks.com/matlabcentral/fileexcahange/30525-isocontour