[R] How can I divide an image and randomly plot the segements

Rolf Turner rolf.turner at xtra.co.nz
Mon Aug 22 04:19:34 CEST 2011


On 21/08/11 23:06, Benson Kenduiywo wrote:
> Hi! I would like to divide a 401 by 401 image into 20 equal blocks and then re-plot the image with the segments randomly distributed in it.
> How can I do this?

For a 401 x 401 pixellation --- I don't know.  If you use a 400 x 400 
pixellation,
then the class "im" in the spatstat package can be used via the attached
function "shuffleIm()".  The number of pixels in each dimension must be 
divisible
by the number of blocks into which that dimension is subdivided.   E.g. 
if you
create your 20 blocks by making 4 divisions in the x-direction and 5 
divisions in
the y-direction then the number of pixels in the x-direction must be 
divisible by 4
and the number in the y-direction must be divisible by 5.  Since 401 is 
prime
it's kind of hard to do any kind of subdividing with a 401 x 401 image.

Note that if X is an object of class "im" with a 401 x 401 pixellation, 
you can
convert it to one with a 400 x 400 pixellation via

     X <- as.im(X,dimyx=400)

This makes the pixel values of the new image equal to the values of the old
image at the closest pixel centre.  This shouldn't (???) cause too much 
distortion.
Said he, optimistically.  (In the one experiment that I did, the images were
visually indistinguishable.)

If you have a 400 x 400 pixel image, say "X", of class "im" then

     Y <- shuffleIm(X,nx=4,ny=5)

gives you a new image in which the matrix of pixel values has been 
subdivided into
a 5 x 4 array of 80 x 100 submatrices and these submatrices randomly 
shuffled.
(Note that the rows of the matrix of pixel values in an object of class 
"im" correspond
to the ***y*** direction and the columns correspond to the ***x*** 
direction.
See fortune("conventions") for a comment. :-) )

Hope this does what you want.

     cheers,

         Rolf Turner
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: shuffleIm.R
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110822/816cb0d7/attachment.pl>


More information about the R-help mailing list