Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

overimpose graphics on an imported image

281 views
Skip to first unread message

alexxx...@gmail.com

unread,
Jan 1, 2010, 5:37:03 AM1/1/10
to
hi people,
I wonder if it is just me being dumb, but I'm unable to perform what I
thought was a very simple operation:

having imported an image of (Lx,Ly) pixels, draw above it an empty
rectangle at coordinates (x0,y0) (x1,y1) (say, lower left and upper
right corners).

Say for example that I want to overimpose on image i1 a rectangle
covering a 10% portion, if I use this:

ImageCompose[i1, Graphics[Rectangle[{0, 0}, {0.1*Lx, 0.1*Ly}]]]

I have (apart from a filled rectangle instead of empty, but I can deal
with it later)
a huge rectangle covering all of it!
How these coordinates interact?

Thanks for any help...


Alessandro Magni

Patrick Scheibe

unread,
Jan 2, 2010, 5:04:04 AM1/2/10
to
Hi,

tell Mathematica about your plotting-range:

img = Import["http://sipi.usc.edu/database/misc/5.1.12.tiff"];
{nx, ny} = ImageDimensions[img];
ImageCompose[img, {Graphics[{Red, Rectangle[{0, 0}, {nx/2, ny/2}]},
PlotRange -> {{0, nx}, {0, ny}}], 0.5}]


Cheers
Patrick

Sjoerd C. de Vries

unread,
Jan 2, 2010, 5:06:27 AM1/2/10
to
Hi Allessandro,

The overlay being unrasterized is scaled to the dimensions of the
first graphic and then rasterized. That explains what you see.

I find the behaviour of ImageCompose a bit unexpected. From the
documentation ("ImageCompose[image,obj] overlays obj pixel-by-pixel
onto image, without changing the size of obj. ") I understand that if
the overlay is rasterized its size isn't changed, but it seems it is.
The positioning commands don't seem to work as expected as well:

{Lx, Ly} = {100, 100};
i1 = Rasterize[
Graphics[{Yellow, Rectangle[{0, 0}, {1, 1}]}, ImagePadding -> None,
PlotRangePadding -> None], RasterSize -> {Lx, Ly},
ImageSize -> Round[{Lx, Ly}]]
i2 = Rasterize[
Graphics[Rectangle[{0, 0}, {1, 0.5}], ImagePadding -> None,
PlotRangePadding -> None], RasterSize -> Round[1/2 Lx],
ImageSize -> Round[1/2 Lx]]
i1 // ImageDimensions
i2 // ImageDimensions
ImageCompose[i1, i2, {Left, Bottom}, {Left, Bottom}]

Cheers -- Sjoerd

On Jan 1, 12:37 pm, "alexxx.ma...@gmail.com" <alexxx.ma...@gmail.com>
wrote:

alexxx...@gmail.com

unread,
Jan 5, 2010, 1:49:06 AM1/5/10
to
thank you for your help!
It works, although it needs this further refinement:

ImageCompose[i1, {Graphics[{Red,
Rectangle[{xmin, ny - ymin}, {xmax, ny - ymax}]},


PlotRange -> {{0, nx}, {0, ny}}], 0.5}]

if I want to be consistent with coordinates given in other Mathematica
functions
(e.g. ImageTake[i1, {ymin, ymax}, {xmin, xmax}] which I use on the
same image...)

I just would like the coordinates were less confusing in M !

alessandro

On 2 Gen, 11:04, Patrick Scheibe <psche...@trm.uni-leipzig.de> wrote:
> Hi,
>
> tell Mathematica about your plotting-range:
>
> img = Import["http://sipi.usc.edu/database/misc/5.1.12.tiff"];
> {nx, ny} = ImageDimensions[img];
> ImageCompose[img, {Graphics[{Red, Rectangle[{0, 0}, {nx/2, ny/2}]},
> PlotRange -> {{0, nx}, {0, ny}}], 0.5}]
>
> Cheers
> Patrick
>

van der Burgt, Maarten

unread,
Jan 7, 2010, 2:27:42 AM1/7/10
to
Alessandro,

A bit more straightforward is the following:

{nx, ny} = ImageDimensions[img];

Show[{Rasterize[img], Graphics@{Red, Opacity[0.5], Disk[{nx/3, 2 ny/3},
35]}}]

Using the Rasterize command the image is converted to Graphics first.
(Thank to Mariusz Jankowski)

regards, Maarten


-----Original Message-----
From: alexxx...@gmail.com [mailto:alexxx...@gmail.com]
Sent: Tuesday, 05 January, 2010 07:50
Subject: Re: overimpose graphics on an imported image

thank you for your help!
It works, although it needs this further refinement:

ImageCompose[i1, {Graphics[{Red,
Rectangle[{xmin, ny - ymin}, {xmax, ny - ymax}]},


PlotRange -> {{0, nx}, {0, ny}}], 0.5}]

if I want to be consistent with coordinates given in other Mathematica


functions
(e.g. ImageTake[i1, {ymin, ymax}, {xmin, xmax}] which I use on the
same image...)

I just would like the coordinates were less confusing in M !

alessandro

On 2 Gen, 11:04, Patrick Scheibe <psche...@trm.uni-leipzig.de> wrote:

> Hi,
>
> tell Mathematica about your plotting-range:
>
> img = Import["http://sipi.usc.edu/database/misc/5.1.12.tiff"];
> {nx, ny} = ImageDimensions[img];
> ImageCompose[img, {Graphics[{Red, Rectangle[{0, 0}, {nx/2, ny/2}]},
> PlotRange -> {{0, nx}, {0, ny}}], 0.5}]
>
> Cheers
> Patrick
>

Fred Klingener

unread,
Jan 8, 2010, 4:14:17 AM1/8/10
to
On Jan 5, 1:49 am, "alexxx.ma...@gmail.com" <alexxx.ma...@gmail.com>
wrote:
>...

> I just would like the coordinates were less confusing in M !
> ...

> -----Original Message-----
> From: alexxx.ma...@gmail.com [mailto:alexxx.ma...@gmail.com]
> Sent: Tuesday, 05 January, 2010 07:50
> Subject: Re: overimpose graphics on an imported image
>
> ...

> I just would like the coordinates were less confusing in M !
> ...

Me too.

One of the common tasks I seem to set for myself is drawing on top of
imported images, and I like Inset[] for that.

Manipulate[
Graphics[{
Rotate[
Inset[
img
, {0., 0.}
, center
, scale
] (* Inset *)
, angle
, {0., 0.}
] (* Rotate *)
, {Blue, Opacity[0.5], Disk[{0., 0.}, 0.5]}
}
, PlotRange -> {{-0.5, 1.5}, {-0.5, 1.2}}
, Axes -> True
, AxesOrigin -> 0
, ImageSize -> {200, 170}
] (* Graphics *)
, {{scale, 1}, 0.5, 3}
, {{angle, 0.}, -Pi/12, Pi/12}
, {{center, {189, 124}}, {150, 100}, {200, 150}}
]

This form lets me scale and align the image, then use GetCoordinates
from the 2D drawing palette to pick key matching points on the image
in my drawing coordinate system.

My final construction uses the same Inset[] structure but with frozen
scale and alignment parameters.

Hth,
Fred Klingener

0 new messages