# How Can I superpose an image and a graphics?

I want to superpose an image with a graphics in order to evaluate their alignment.

I am writting this exemple of code.

s1 = Graphics[{RGBColor[{1,0,0}],Circle[{0, 1}]}];
model = ContourPlot[x^2 + y^2 - 1 == 0, {x, -2, 2}, {y, -2, 2}]
imref = Rasterize[s1, ImageSize -> {200, 150}](*suppose that is given by camera*)
Show[imref,model];
(*second method*)
immodel=Rasterize[model, ImageSize -> {200, 150}];
ImageCompose[imref,immodel]


I have an other idea, I can binarize the two images and Multiply them or do ImageDifference to get the result alignement but I need also to show graphically the results! Is there any idea to add the opacity or transparency parameters in order to see both images superposed images? thanks

• Take a look at the examples of Inset, including the one that displays a map and country information in a grid: reference.wolfram.com/mathematica/ref/Inset.html Commented Oct 22, 2013 at 19:17
• take a look at inset. Dont know if Cercle is a mispell or what.. you might want to fix that.. Commented Oct 22, 2013 at 19:21
• similar question Commented Oct 22, 2013 at 21:02

Similar to inset or show you can also use Overlay. Related questions might be this. or this.

s1 = Graphics[{RGBColor[{1, 0, 0}], Circle[{0, 1}]}]
model = ContourPlot[x^2 + y^2 - 1 == 0, {x, -2, 2}, {y, -2, 2}]
img = Import["https://i.sstatic.net/3ynJe.png"]

Overlay[{img, s1, model}]


The trick to Inset..ing a raster is to supply the required offset/scale arguments:

   s1 = Graphics[{RGBColor[{1, 0, 0}], Circle[{1 - .05, .05}, 1]},
PlotRange -> {{-2, 2}, {-2, 2}}];
model = ContourPlot[(x - 1)^2 + y^2 - 1 == 0, {x, -2, 2}, {y, -2, 2}];
imref = Rasterize[s1, ImageSize -> {400, 400}];
Show[{Graphics[Inset[imref, {-2, -2}, {0, 0}, {4, 4}] ], model},
PlotRange -> {{-3, 3}, {-3, 3}}]


This aligns the two circles (sorry I cant post graphics) by placing the "0,0" of the raster at the lower left of the range specified in the first graphics object, and scaling appropriately.

For some reason this looses the contour plot axes, but you can do within contourplot as a prolog:

ContourPlot[(x - 1)^2 + y^2 - 1 == 0, {x, -2, 2}, {y, -2, 2},
Prolog -> Inset[imref, {-2, -2}, {0, 0}, {4, 4}],AspectRatio->Automatic]


You can try something like this I guess,

s1 = Graphics[{RGBColor[{1, 0, 0}],
Circle[{0, 0}, 1], {PointSize[.02], Point[{0, 0}]}},
GridLines -> Automatic];
p1 = ContourPlot[x^2 + y^2 - 1 == 0, {x, -1, 1}, {y, -1, 1},
GridLines -> Automatic, FrameStyle -> Directive[Orange, 12]];
ImageCompose[s1, {p1, 1}]


If Inset is used

Graphics[{{RGBColor[{1, 0, 0}], Circle[{0, 1}]},
Inset[ContourPlot[x^2 + y^2 - 1 == 0, {x, -1, 1}, {y, -1, 1},
Axes -> True]]}, Frame -> True, GridLines -> Automatic]


• Thanks for your response, I know how Can I supperpose two graphics, we can use Show[],inset,... also. But in my case, I need to supperpose two images or an image and a graphics. I need to supperpose a reference image and a plotting of the model. Commented Oct 22, 2013 at 19:59
• with image you cannot control coordinates so how will you decide..? To measure relative size, you need frame of reference. Commented Oct 22, 2013 at 20:02
• OK!thanks, I will try again, surely, there is a solution! Commented Oct 22, 2013 at 20:10