Title: | R Simple Image Mosaic Creation Library |
---|---|
Description: | Provides a way to transform an image into a mosaic composed from a set of smaller images (tiles). It also contains a simple function for creating the tiles from a folder of images directly through R, without the need of any external code. At this moment only the JPEG format is supported, either as input (image and tiles) or output (mosaic transformed image). |
Authors: | Alberto Krone-Martins |
Maintainer: | Alberto Krone-Martins <[email protected]> |
License: | GPL (>= 2) |
Version: | 1.0.3 |
Built: | 2024-10-25 02:44:12 UTC |
Source: | https://github.com/cran/RsimMosaic |
RsimMosaic is a package for transforming an image into a mosaic composed from a set of smaller images (tiles). This library also contains a simple function for creating the tiles from a folder of images directly through R, without the need of any external code. At this moment only the JPEG format is supported, either as input (image and tiles) or output (mosaic transformed image).
Package: | RsimMosaic |
Type: | Package |
Version: | 1.0.2 |
Date: | 2014-08-23 |
License: | GPL (>= 2) |
The RsimMosaic, or the R Simple IMage Mosaic creation library, is a package for the production of mosaics. From a set of images that are called tiles, the implemented methods are able to compose a mosaic that mimics a user selected image. One example is represented in the figure below:
Note that the larger and the more varied is the adopted tile library, the more faithful will be the resulting mosaic. This package comes with a very small library, containing only 100 stamps of astronomical sources (called Messier objects) from the 2MASS catalogue, for example and test purposes.
Author: Alberto Krone-Martins
Maintainer: Alberto Krone-Martins <[email protected]>
composeMosaicFromImageRandom
, composeMosaicFromImageRegular
, createTiles
# This example will transform an image of the Moon into a mosaic # composed by objects from the Messier catalogue. The tiles were # created from the images of the 2MASS catalogue. # # Set the filename of the original image #origImgFileN <- system.file("extdata", "verySmallMoon.jpg", package="RsimMosaic") origImgFileN <- system.file("extdata", "reallyVerySmallMoon.jpg", package="RsimMosaic") # Set the folder where the tiles library is located pathToTileLib <- system.file("extdata/2Massier", package="RsimMosaic") # Set the filename of the output image (the mosaic!) outImgFileN <- file.path(tempdir(), "verySmallMoon-2MASS-Mosaic.jpg") # Create the mosaic composeMosaicFromImageRandom(origImgFileN, outImgFileN, pathToTileLib, removeTiles=FALSE)
# This example will transform an image of the Moon into a mosaic # composed by objects from the Messier catalogue. The tiles were # created from the images of the 2MASS catalogue. # # Set the filename of the original image #origImgFileN <- system.file("extdata", "verySmallMoon.jpg", package="RsimMosaic") origImgFileN <- system.file("extdata", "reallyVerySmallMoon.jpg", package="RsimMosaic") # Set the folder where the tiles library is located pathToTileLib <- system.file("extdata/2Massier", package="RsimMosaic") # Set the filename of the output image (the mosaic!) outImgFileN <- file.path(tempdir(), "verySmallMoon-2MASS-Mosaic.jpg") # Create the mosaic composeMosaicFromImageRandom(origImgFileN, outImgFileN, pathToTileLib, removeTiles=FALSE)
This is a simple function to add a tile (with the filename tileFilename
) back to the tile library (passed as the argument libForMosaic
). The tile data in the parameter space is copied from the original tile library (passed as the argument libForMosaicFull
).
addBackTile(tileFilename, libForMosaic, libForMosaicFull)
addBackTile(tileFilename, libForMosaic, libForMosaicFull)
tileFilename |
The filename of the tile to add back in the tile library. |
libForMosaic |
The tile library where the tile should be added back. |
libForMosaicFull |
The original tile library containing the data of the all the tiles in the parameter space. |
It returns the tile library libForMosaic, with the requested tile added.
Alberto Krone-Martins
# Creates the tile library data frame from the example tiles my2MassTilesFull <- createLibraryIndexDataFrame(system.file("extdata/2Massier/", package="RsimMosaic")) my2MassTiles <- my2MassTilesFull # Get a random filename of one of the tiles idx <- round(runif(1, 1, length(my2MassTiles[,1]))) tileFilename <- as.character(my2MassTiles[idx,1]) # Remove it from the library my2MassTiles <- removeTile(tileFilename, my2MassTiles) # Now, put it back my2MassTiles <- addBackTile(tileFilename, my2MassTiles, my2MassTilesFull)
# Creates the tile library data frame from the example tiles my2MassTilesFull <- createLibraryIndexDataFrame(system.file("extdata/2Massier/", package="RsimMosaic")) my2MassTiles <- my2MassTilesFull # Get a random filename of one of the tiles idx <- round(runif(1, 1, length(my2MassTiles[,1]))) tileFilename <- as.character(my2MassTiles[idx,1]) # Remove it from the library my2MassTiles <- removeTile(tileFilename, my2MassTiles) # Now, put it back my2MassTiles <- addBackTile(tileFilename, my2MassTiles, my2MassTilesFull)
This function is just a wrapper to the interp.surface.grid
function to perform bilinar interpolation of a regular matrix.
bilinearInterpolator(oldMatrix, pointsInNewX, pointsInNewY)
bilinearInterpolator(oldMatrix, pointsInNewX, pointsInNewY)
oldMatrix |
The original matrix. |
pointsInNewX |
The number of points in the new matrix (the number of new rows). |
pointsInNewY |
The number of points in the new matrix (the number of new columns). |
It returns an interpolated matrix. The size of the new matrix is pointsInNewX rows and pointsInNewY columns.
Alberto Krone-Martins
library('jpeg') # Read the R logo logo <- readJPEG(system.file("img", "Rlogo.jpg", package="jpeg")) # Create a scaled down version of the R channel intrpArray <- array(dim=c(20, 20, 3)) intrpArray[,,1] <- bilinearInterpolator(logo[,,1], dim(intrpArray)[1], dim(intrpArray)[2]) # Display the results dev.new() image(logo[,,1], main="Original") dev.new() image(intrpArray[,,1], main="Scaled down")
library('jpeg') # Read the R logo logo <- readJPEG(system.file("img", "Rlogo.jpg", package="jpeg")) # Create a scaled down version of the R channel intrpArray <- array(dim=c(20, 20, 3)) intrpArray[,,1] <- bilinearInterpolator(logo[,,1], dim(intrpArray)[1], dim(intrpArray)[2]) # Display the results dev.new() image(logo[,,1], main="Original") dev.new() image(intrpArray[,,1], main="Scaled down")
A function to compose the mosaic of an image based on regular tiles. This function will compute the mosaic by randomly replacing the pixels of the original image with tiles from a tile library.
composeMosaicFromImageRandom(originalImageFileName, outputImageFileName, imagesToUseInMosaic, useGradients = FALSE, removeTiles = TRUE, fracLibSizeThreshold = 0.7, repFracSize = 0.25, verbose = TRUE)
composeMosaicFromImageRandom(originalImageFileName, outputImageFileName, imagesToUseInMosaic, useGradients = FALSE, removeTiles = TRUE, fracLibSizeThreshold = 0.7, repFracSize = 0.25, verbose = TRUE)
originalImageFileName |
The original image you want to use to create the mosaic from (note that for the sake of your computer's memory, this image should be small, about 128 or 256 pixels wide, or so...). |
outputImageFileName |
The filename (with the path) where you want the image to be stored. This image can be quite large, depending on the number of pixels in the original image and on the tiles. |
imagesToUseInMosaic |
A path with the folder where the tiles are contained (note that the tiles should be square and for the sake of your computer's memory, small, e.g. 40x40, 120x120 pixels or so...). You can use the function |
useGradients |
A flag to indicate if approximate gradients should be taken into account when selecting the tiles. |
removeTiles |
A flag to indicate if the user wants to temporarily remove tiles from the library after using them, so tile repetition would be discoraged by the software. If the tile library is small, the tiles will be added back to the library after its size reaches a certain threshold (defined by |
fracLibSizeThreshold |
The fraction of the size of the original tile library when the tiles must be (randomly) put back into the library. |
repFracSize |
The fraction of the size of the original tile library to replace when filling back the array (it should be smaller than, or equal to, 1-fracLibSizeThreshold). |
verbose |
A flag indicating if the user wants to have messages during the mosaic creation. |
Alberto Krone-Martins
# Set the filename of the original image #origImgFileN <- system.file("extdata", "verySmallMoon.jpg", package="RsimMosaic") origImgFileN <- system.file("extdata", "reallyVerySmallMoon.jpg", package="RsimMosaic") # Set the folder where the tiles library is located pathToTileLib <- system.file("extdata/2Massier", package="RsimMosaic") # Set the filename of the output image (the mosaic!) outImgFileN <- file.path(tempdir(), "verySmallMoon-2MASS-Mosaic.jpg") # Create the mosaic composeMosaicFromImageRandom(origImgFileN, outImgFileN, pathToTileLib, removeTiles=FALSE)
# Set the filename of the original image #origImgFileN <- system.file("extdata", "verySmallMoon.jpg", package="RsimMosaic") origImgFileN <- system.file("extdata", "reallyVerySmallMoon.jpg", package="RsimMosaic") # Set the folder where the tiles library is located pathToTileLib <- system.file("extdata/2Massier", package="RsimMosaic") # Set the filename of the output image (the mosaic!) outImgFileN <- file.path(tempdir(), "verySmallMoon-2MASS-Mosaic.jpg") # Create the mosaic composeMosaicFromImageRandom(origImgFileN, outImgFileN, pathToTileLib, removeTiles=FALSE)
A function to compose the mosaic of an image based on regular tiles. This function will compute the mosaic by randomly replacing the pixels of the original image with tiles from a tile library, and it will try to use as many tiles from the library as possible.
composeMosaicFromImageRandomOptim(originalImageFileName, outputImageFileName, imagesToUseInMosaic, useGradients = FALSE, removeTiles = TRUE, fracLibSizeThreshold = 0.7, repFracSize = 0.25, verbose = TRUE, neig=20)
composeMosaicFromImageRandomOptim(originalImageFileName, outputImageFileName, imagesToUseInMosaic, useGradients = FALSE, removeTiles = TRUE, fracLibSizeThreshold = 0.7, repFracSize = 0.25, verbose = TRUE, neig=20)
originalImageFileName |
The original image you want to use to create the mosaic from (note that for the sake of your computer's memory, this image should be small, about 128 or 256 pixels wide, or so...). |
outputImageFileName |
The filename (with the path) where you want the image to be stored. This image can be quite large, depending on the number of pixels in the original image and on the tiles. |
imagesToUseInMosaic |
A path with the folder where the tiles are contained (note that the tiles should be square and for the sake of your computer's memory, small, e.g. 40x40, 120x120 pixels or so...). You can use the function |
useGradients |
A flag to indicate if approximate gradients should be taken into account when selecting the tiles. |
removeTiles |
A flag to indicate if the user wants to temporarily remove tiles from the library after using them, so tile repetition would be discoraged by the software. If the tile library is small, the tiles will be added back to the library after its size reaches a certain threshold (defined by |
fracLibSizeThreshold |
The fraction of the size of the original tile library when the tiles must be (randomly) put back into the library. |
repFracSize |
The fraction of the size of the original tile library to replace when filling back the array (it should be smaller than, or equal to, 1-fracLibSizeThreshold). |
verbose |
A flag indicating if the user wants to have messages during the mosaic creation. |
neig |
An integer indicating the number of neighbors to retrieve from the tile library. |
Alberto Krone-Martins
# Set the filename of the original image #origImgFileN <- system.file("extdata", "verySmallMoon.jpg", package="RsimMosaic") origImgFileN <- system.file("extdata", "reallyVerySmallMoon.jpg", package="RsimMosaic") # Set the folder where the tiles library is located pathToTileLib <- system.file("extdata/2Massier", package="RsimMosaic") # Set the filename of the output image (the mosaic!) outImgFileN <- file.path(tempdir(), "verySmallMoon-2MASS-Mosaic.jpg") # Create the mosaic composeMosaicFromImageRandomOptim(origImgFileN, outImgFileN, pathToTileLib, removeTiles=FALSE)
# Set the filename of the original image #origImgFileN <- system.file("extdata", "verySmallMoon.jpg", package="RsimMosaic") origImgFileN <- system.file("extdata", "reallyVerySmallMoon.jpg", package="RsimMosaic") # Set the folder where the tiles library is located pathToTileLib <- system.file("extdata/2Massier", package="RsimMosaic") # Set the filename of the output image (the mosaic!) outImgFileN <- file.path(tempdir(), "verySmallMoon-2MASS-Mosaic.jpg") # Create the mosaic composeMosaicFromImageRandomOptim(origImgFileN, outImgFileN, pathToTileLib, removeTiles=FALSE)
A function to compose the mosaic of an image based on regular tiles. This function will compute the mosaic by regularly replacing the pixels of the original image with tiles from a tile library.
composeMosaicFromImageRegular(originalImageFileName, outputImageFileName, imagesToUseInMosaic, useGradients = FALSE, removeTiles = TRUE, fracLibSizeThreshold = 0.7, repFracSize = 0.25, verbose = TRUE)
composeMosaicFromImageRegular(originalImageFileName, outputImageFileName, imagesToUseInMosaic, useGradients = FALSE, removeTiles = TRUE, fracLibSizeThreshold = 0.7, repFracSize = 0.25, verbose = TRUE)
originalImageFileName |
The original image you want to use to create the mosaic from (note that for the sake of your computer's memory, this image should be small, about 128 or 256 pixels wide, or so...). |
outputImageFileName |
The filename (with the path) where you want the image to be stored. This image can be quite large, depending on the number of pixels in the original image and on the tiles. |
imagesToUseInMosaic |
A path with the folder where the tiles are contained (note that the tiles should be square and for the sake of your computer's memory, small, e.g. 40x40, 120x120 pixels or so...). You can use the function |
useGradients |
A flag to indicate if approximate gradients should be taken into account when selecting the tiles. |
removeTiles |
A flag to indicate if the user wants to temporarily remove tiles from the library after using them, so tile repetition would be discoraged by the software. If the tile library is small, the tiles will be added back to the library after its size reaches a certain threshold (defined by |
fracLibSizeThreshold |
The fraction of the size of the original tile library when the tiles must be (randomly) put back into the library. |
repFracSize |
The fraction of the size of the original tile library to replace when filling back the array (it should be smaller than, or equal to, 1-fracLibSizeThreshold). |
verbose |
A flag indicating if the user wants to have messages during the mosaic creation. |
Alberto Krone-Martins
# Set the filename of the original image #origImgFileN <- system.file("extdata", "verySmallMoon.jpg", package="RsimMosaic") origImgFileN <- system.file("extdata", "reallyVerySmallMoon.jpg", package="RsimMosaic") # Set the folder where the tiles library is located pathToTileLib <- system.file("extdata/2Massier", package="RsimMosaic") # Set the filename of the output image (the mosaic!) outImgFileN <- file.path(tempdir(), "verySmallMoon-2MASS-Mosaic.jpg") # Create the mosaic composeMosaicFromImageRegular(origImgFileN, outImgFileN, pathToTileLib, removeTiles=TRUE)
# Set the filename of the original image #origImgFileN <- system.file("extdata", "verySmallMoon.jpg", package="RsimMosaic") origImgFileN <- system.file("extdata", "reallyVerySmallMoon.jpg", package="RsimMosaic") # Set the folder where the tiles library is located pathToTileLib <- system.file("extdata/2Massier", package="RsimMosaic") # Set the filename of the output image (the mosaic!) outImgFileN <- file.path(tempdir(), "verySmallMoon-2MASS-Mosaic.jpg") # Create the mosaic composeMosaicFromImageRegular(origImgFileN, outImgFileN, pathToTileLib, removeTiles=TRUE)
A function to compute the relevant pixel quantity (the RGB color). Optionally it can also output the values of relevant, nearby pixels as RGB colors at the Left, UpperLeft, Upper, UpperRight, Right, LowerRight, Lower, and LowerLeft pixels.
computeStatisticalQuantitiesPixel(i, j, img, useGradients = FALSE)
computeStatisticalQuantitiesPixel(i, j, img, useGradients = FALSE)
i |
The row (or the X in the image). |
j |
The column (or the Y in the image). |
img |
An image array (as created by the readJPEG function from the jpeg library). |
useGradients |
A flag indicating if the values of the nearby pixels should be returned. |
An array with the relevant pixel quantites.
Alberto Krone-Martins
computeStatisticalQuantitiesTile
# Read the R logo and output the value of its pixel (50, 5) in the parameter space library('jpeg') logo <- readJPEG(system.file("img", "Rlogo.jpg", package="jpeg")) # Read the R logo computeStatisticalQuantitiesPixel(50, 5, logo) # Compute the quantities at the pixel (5,5)
# Read the R logo and output the value of its pixel (50, 5) in the parameter space library('jpeg') logo <- readJPEG(system.file("img", "Rlogo.jpg", package="jpeg")) # Read the R logo computeStatisticalQuantitiesPixel(50, 5, logo) # Compute the quantities at the pixel (5,5)
A function to compute the relevant statistical quantities (only the median value is implemented) of the RGB colors for the entire image. Optionally it can also compute the median values of the RGB colors at the Left, UpperLeft, Upper, UpperRight, Right, LowerRight, Lower, and LowerLeft, corners of the image.
computeStatisticalQuantitiesTile(img, useGradients = FALSE)
computeStatisticalQuantitiesTile(img, useGradients = FALSE)
img |
An image array (as created by the readJPEG function from the jpeg library). |
useGradients |
A flag indicating if the values of the relevant quantities should be calculated in the corners of the image. |
The data is defined as the median RGB colors of the tile images at this version. Optionally, the median RGB values of the tile image corners are also calculated.
An array with the relevant quantities calculated.
Alberto Krone-Martins
readJPEG
, computeStatisticalQuantitiesPixel
# Read the R logo and output its values in the parameter space library('jpeg') logo <- readJPEG(system.file("img", "Rlogo.jpg", package="jpeg")) # Read the R logo computeStatisticalQuantitiesTile(logo) # Compute the quantities
# Read the R logo and output its values in the parameter space library('jpeg') logo <- readJPEG(system.file("img", "Rlogo.jpg", package="jpeg")) # Read the R logo computeStatisticalQuantitiesTile(logo) # Compute the quantities
A function to create the tile library data frame containing the data of the tiles in the parameter space, as well as the filename of the tiles.
createLibraryIndexDataFrame(path, saveLibraryIndex = FALSE, libraryFilename, useGradients = FALSE)
createLibraryIndexDataFrame(path, saveLibraryIndex = FALSE, libraryFilename, useGradients = FALSE)
path |
A path indicating where the images that will compose the library are stored. |
saveLibraryIndex |
A flag to indicate if the library should be saved in a file. |
libraryFilename |
The filename to use if the user wants to store the library to a file. |
useGradients |
A flag indicating if the values of the data in the parameter space in the corners of the tile images should be calculated. |
The tile image data in the parameter space is calculated by computeStatisticalQuantitiesTile
.
It returns a data frame containing the filename of each tile found at the path
and the median RGB values of each tile. Optionally it also includes the median RGB values of the corners of the image.
Alberto Krone-Martins
computeStatisticalQuantitiesTile
# Creates the tile library data frame from the example tiles my2MassTiles <- createLibraryIndexDataFrame(system.file("extdata/2Massier/", package="RsimMosaic"))
# Creates the tile library data frame from the example tiles my2MassTiles <- createLibraryIndexDataFrame(system.file("extdata/2Massier/", package="RsimMosaic"))
A very simple function to create tiles from a folder containing JPEG images. It uses bilinear interpolation (via the bilinearInterpolator
function), thus note that the quality of the tiles will be sub-optimal. Thus, for high quality purposes please use another external tool providing better interpolation schemes to create your tiles (bicubic splines, sincz, ...).
createTiles(inPath, outPath, tileHeight = 40, verbose = TRUE)
createTiles(inPath, outPath, tileHeight = 40, verbose = TRUE)
inPath |
A path with the folder where the images are contained. |
outPath |
A path with the folder where the tiles will be created (if the folder does not exists, it will be created). |
tileHeight |
The tile height in pixels. |
verbose |
A boolean flag to indicate if the user wants to have screen output or not. |
Alberto Krone-Martins
# Set the folder where the original images are located pathToOriginalImages <- system.file("extdata/2Massier/", package="RsimMosaic") # Set the folder where the tiles will be stored. It will be created if it does not exists. pathToTileImages <- paste(tempdir(), "/myTiles/", sep="") # Create the tiles (10 pix are used just to make this example run fast) createTiles(pathToOriginalImages, pathToTileImages, tileHeight=10)
# Set the folder where the original images are located pathToOriginalImages <- system.file("extdata/2Massier/", package="RsimMosaic") # Set the folder where the tiles will be stored. It will be created if it does not exists. pathToTileImages <- paste(tempdir(), "/myTiles/", sep="") # Create the tiles (10 pix are used just to make this example run fast) createTiles(pathToOriginalImages, pathToTileImages, tileHeight=10)
This function will return the filename of a tile that is a close match to a pixel in the parameter space. The nneig
matches are selected using a nearest neighbour search (nn2
) in the tile library (libraryDataFrame
). After the candidates selected, one of the is randomly choosen and its filename is returned by the function.
getCloseMatch(pixelArray, libraryDataFrame, nneig = 20)
getCloseMatch(pixelArray, libraryDataFrame, nneig = 20)
pixelArray |
The parameters of the pixel to get a similar image from the library in the parameter space. |
libraryDataFrame |
The tile library containing the data of the tiles in the parameter space. |
nneig |
Number of neighbours to retrieve in the intermediate test. Only one of the neighbours will be returned to the user. |
The filename of a tile that is a close match to a pixel in the parameter space.
Alberto Krone-Martins
# Creates the tile library data frame from the example tiles my2MassTiles <- createLibraryIndexDataFrame(system.file("extdata/2Massier", package="RsimMosaic")) # Get a close match for the point with parameters (0.2, 0.3, 0.2) getCloseMatch(c(0.2, 0.3, 0.2), my2MassTiles) # Get another close match for the point with parameters (0.2, 0.3, 0.2) getCloseMatch(c(0.2, 0.3, 0.2), my2MassTiles)
# Creates the tile library data frame from the example tiles my2MassTiles <- createLibraryIndexDataFrame(system.file("extdata/2Massier", package="RsimMosaic")) # Get a close match for the point with parameters (0.2, 0.3, 0.2) getCloseMatch(c(0.2, 0.3, 0.2), my2MassTiles) # Get another close match for the point with parameters (0.2, 0.3, 0.2) getCloseMatch(c(0.2, 0.3, 0.2), my2MassTiles)
This is a simple function to remove a tile (with the filename tileFilename
) from the tile library (passed as the argument libForMosaic
).
removeTile(tileFilename, libForMosaic)
removeTile(tileFilename, libForMosaic)
tileFilename |
The filename of the tile to remove from the tile library. |
libForMosaic |
The library containing the data of the tiles in the parameter space from which the tile should be removed. |
It returns the tile library libForMosaic, with the requested tile removed.
Alberto Krone-Martins
# Creates the tile library data frame from the example tiles my2MassTiles <- createLibraryIndexDataFrame(system.file("extdata/2Massier", package="RsimMosaic")) # Get a random filename of one of the tiles idx <- round(runif(1, 1, length(my2MassTiles[,1]))) tileToRemove <- as.character(my2MassTiles[idx,1]) # Remove it from the library my2MassTiles <- removeTile(tileToRemove, my2MassTiles)
# Creates the tile library data frame from the example tiles my2MassTiles <- createLibraryIndexDataFrame(system.file("extdata/2Massier", package="RsimMosaic")) # Get a random filename of one of the tiles idx <- round(runif(1, 1, length(my2MassTiles[,1]))) tileToRemove <- as.character(my2MassTiles[idx,1]) # Remove it from the library my2MassTiles <- removeTile(tileToRemove, my2MassTiles)