Title: | A Multiscale Test of Spatial Stationarity for LS2W Processes |
---|---|
Description: | Wavelet-based methods for testing stationarity and quadtree segmenting of images, see Taylor et al (2014) <doi:10.1080/00401706.2013.823890>. |
Authors: | Sarah Taylor [aut], Matt Nunes [aut, cre], Idris Eckley [ctb, ths] |
Maintainer: | Matt Nunes <[email protected]> |
License: | GPL-2 |
Version: | 2.1-5 |
Built: | 2024-11-12 06:41:35 UTC |
Source: | CRAN |
This package contains functions for testing for stationarity within images, specifically locally stationary wavelet (LS2W) fields. In addition the package contains functions for implementing quadtree image decompositions, as well as code for simulating LS2W processes for a given spectral structure.
Sarah L. Taylor and Matt Nunes
Maintainer: Matthew Nunes <[email protected]>
For details on testing LS2W fields for stationarity, see
Taylor, S.L., Eckley, I.A., and Nunes, M.A. (2014) A Test of Stationarity for Textured Images. Technometrics, 56 (3), 291-301.
For further information on LS2W processes, see
Eckley, I.A., Nason, G.P., and Treloar, R.L. (2010) Locally stationary wavelet fields with application to the modelling and analysis of image texture Journal of the Royal Statistical Society Series C, 59, 595-616.
Calculates a test statistic for a test of stationarity based on the local wavelet spectrum.
avespecvar(spectrum)
avespecvar(spectrum)
spectrum |
A local wavelet spectrum estimate, i.e. a |
The test statistic given by Taylor et al. (2014) for a test for stationarity is computed for use in the boostrap testing procedure (TOS2D
).
statistic |
The value of the test statistic for the given spectrum. |
Sarah L. Taylor
Taylor, S.L., Eckley, I.A., and Nunes, M.A. (2014) A Test of Stationarity for Textured Images. Technometrics, 56 (3), 291-301.
#Generate a cddews object # X <- Haar2MA.diag(64) testspec<-cddews(X,smooth=FALSE) #Find the value of the test statistic # avespecvar(testspec) #
#Generate a cddews object # X <- Haar2MA.diag(64) testspec<-cddews(X,smooth=FALSE) #Find the value of the test statistic # avespecvar(testspec) #
The function combines two images together, and then tests the montage for stationarity.
compareImages(Im1, Im2, testsize = min(nrow(Im1), nrow(Im2)), alpha=0.05,...)
compareImages(Im1, Im2, testsize = min(nrow(Im1), nrow(Im2)), alpha=0.05,...)
Im1 |
The first image to be compared. |
Im2 |
The second image to be compared. |
testsize |
The size of the combined image montage to be tested for stationarity. |
alpha |
The significance of the stationarity test. |
... |
Any other optional arguments to |
An image montage of two images is created, and the homogeneity measure TOS2D
is used in combination with getpval
to assess stationarity of the montage. If the image is assessed as stationary, the two images are considered as the same texture.
montageres |
A boolean value indicating whether the montage of |
Sarah L. Taylor and Matt Nunes
Taylor, S.L., Eckley, I.A., and Nunes, M.A. (2014) A Test of Stationarity for Textured Images. Technometrics, 56 (3), 291-301.
# create two images to be compared: X1<-simTexture(32,K=1,imtype="S1")[[1]] X2<-simTexture(32,K=1,imtype="S1", sd=1.6)[[1]] # use the test to compare them: test<-compareImages(X1,X2,nsamples=100, smooth=FALSE)
# create two images to be compared: X1<-simTexture(32,K=1,imtype="S1")[[1]] X2<-simTexture(32,K=1,imtype="S1", sd=1.6)[[1]] # use the test to compare them: test<-compareImages(X1,X2,nsamples=100, smooth=FALSE)
Groups a list of (stationary) images into texture classes.
countTextures(Imgs, medpol = TRUE, ...)
countTextures(Imgs, medpol = TRUE, ...)
Imgs |
A list of images to classify into textures. |
medpol |
A boolean value indicating whether to zero mean the images (with Tukey's median polish) prior to classification. |
... |
Any other optional arguments to the classification function |
The procedure recursively uses the function compareImages
to decide whether two images are of the same texture or not. More specifically, the first image is sequentially tested with all others in the list, assigning the images the label "1" if assessed as the same texture as the first image. All other (unclassified) images are then similarly compared with candidates from different texture classes, until all images have been assigned a group label. Testing recursively in this way, there are at most choose(length(Imgs),2)
comparisons performed, but in reality the number could be a lot fewer.
Iclass |
A vector (of length |
Sarah L. Taylor and Matt Nunes
Taylor, S.L., Eckley, I.A., and Nunes, M.A. (2014) A Test of Stationarity for Textured Images. Technometrics, 56 (3), 291-301.
## Not run: X1<-simTexture(128,K=1,imtype="S1")[[1]] X2<-simTexture(128,K=1,imtype="S1")[[1]] X3<-simTexture(128,K=1,imtype="S1",sd=1.6)[[1]] Xlist<-list(X1,X2,X3) Xlist.class<-countTextures(Xlist, bs=100) ## End(Not run)
## Not run: X1<-simTexture(128,K=1,imtype="S1")[[1]] X2<-simTexture(128,K=1,imtype="S1")[[1]] X3<-simTexture(128,K=1,imtype="S1",sd=1.6)[[1]] Xlist<-list(X1,X2,X3) Xlist.class<-countTextures(Xlist, bs=100) ## End(Not run)
If the input image is not of dimension , for some n, then the image is cropped to an optionally specified size.
cropimage(image, newsize = NULL, pos = "e")
cropimage(image, newsize = NULL, pos = "e")
image |
The image you wish to crop. |
newsize |
An optional dimension (smaller than the original image dimension), to which the image should be cropped. |
pos |
The position of the subimage to take when cropping an image. See the documentation for |
As we often wish to work with images whose dimensions are some power of 2, this function will determine whether the image is of an appropriate size and if not it will crop the image so that it is. The optional pos
argument specifies the position of the cropped subimage to be returned; for example pos="e"
specifies the central region. See mix2images
for more details on the positioning argument.
subim |
A square image with dimension |
Matt Nunes
# #Create an image with dimensions not a power of two # testimage <- matrix(rnorm(300^2),nrow=300,ncol=300) # #Crop the image # Newimage <- cropimage(testimage) # # Check new dimension size. # dim(Newimage) #
# #Create an image with dimensions not a power of two # testimage <- matrix(rnorm(300^2),nrow=300,ncol=300) # #Crop the image # Newimage <- cropimage(testimage) # # Check new dimension size. # dim(Newimage) #
Computes and returns a p-value from the output of the bootstrap test for stationarity.
getpval(statvec, verbose = TRUE)
getpval(statvec, verbose = TRUE)
statvec |
A vector of test statistics, such as that given by |
verbose |
If TRUE then the p-value is printed and a sentance declaring "stationary" or "not stationary" is printed. |
p |
The p-value of the test. |
Sarah L. Taylor
Taylor, S.L., Eckley, I.A., and Nunes, M.A. (2014) A Test of Stationarity for Textured Images. Technometrics, 56 (3), 291-301.
#Generate a stationary image testimage <- matrix(rnorm(64*64), nrow=64, ncol=64) # Run test of stationarity ## Not run: TestofStat<-TOS2D(testimage) # Obtain p-value getpval(TestofStat$samples) ## End(Not run)
#Generate a stationary image testimage <- matrix(rnorm(64*64), nrow=64, ncol=64) # Run test of stationarity ## Not run: TestofStat<-TOS2D(testimage) # Obtain p-value getpval(TestofStat$samples) ## End(Not run)
The quadtree decomposition is achieved by recursively splitting subimages into regions of stationarity.
imageQT(image, test = TOS2D, minsize = 64,alpha=0.05, ...)
imageQT(image, test = TOS2D, minsize = 64,alpha=0.05, ...)
image |
An image to be decomposed. |
test |
A function for assessing regions of spatial homogeneity, for example |
minsize |
The smallest region to test for homogeneity. |
alpha |
The significance level for the homogeneity test |
... |
Any other (optional) arguments to |
This function works by assessing an image for homogeneity. If it is not homogeneous, the image is split into its four subquadrants. Each of these is then tested for homogeneity. The heterogeneous subimages are then again subdivided and tested again. This procedure is repeated until either all subimages are deemed stationary or the minimum testing size minsize
is reached.
An object of class imageQT
with the following components:
data.name |
The image analysed. |
indl |
The index representation of the nonstationary images in the quadtree decomposition. |
resl |
The results of the stationarity testing (from |
imsize |
The original image dimension. |
imS |
The stationary subimages in the quadtree decomposition. |
indS |
The index representation of the stationary images in the quadtree decomposition. |
minsize |
The minimum testing region used during the quadtree decomposition. |
Sarah L. Taylor and Matt Nunes
Sonka, M., Boyle, R., and Hlavic, V. (1999) Image processing, analysis and machine vision. 2nd Edition, PWS Publishing.
Taylor, S.L., Eckley, I.A., and Nunes, M.A. (2014) A Test of Stationarity for Textured Images. Technometrics, 56 (3), 291-301.
# generate an image: X<-simTexture(128,K=1,imtype="NS1", sd = 3)[[1]] ## Not run: XQT<-imageQT(X,binfun=TOS2D.bin)
# generate an image: X<-simTexture(128,K=1,imtype="NS1", sd = 3)[[1]] ## Not run: XQT<-imageQT(X,binfun=TOS2D.bin)
A function with which to generate nonstationary covariance structure.
lincurve(x, start = 1, end = 2, a = 0.25)
lincurve(x, start = 1, end = 2, a = 0.25)
x |
a sequence of x-values. |
start |
a starting value for the linear function. |
end |
an ending value for the linear function. |
a |
a proportion of the x-values for the linear part of the function. |
y |
the y-values associated to the linear function. |
Matt Nunes
x<-seq(0,1,length=128) y<-lincurve(x,start=1,end=2,a=.25) plot(x,y,type="l")
x<-seq(0,1,length=128) y<-lincurve(x,start=1,end=2,a=.25) plot(x,y,type="l")
Image A is re-sized to a specified proportion of Image B, then inserted into Image B at a given position.
mix2images(imageA, imageB, prop = 0.25, pos = "e")
mix2images(imageA, imageB, prop = 0.25, pos = "e")
imageA |
The first image which is resized and placed inside the second image. |
imageB |
The second image, into which the first is placed. |
prop |
The proportion of Image B to be taken up by Image A. |
pos |
The exact position of image A in image B. Possible options are "a", "b", "c", "d", "e" which corresponds to (a) top-right, (b) bottom-right, (c) top-left, (d) bottom-left and (e) centred. A more exact location may be specified by inputting |
This function first of all crops Image A to be a given proportion of Image B and then inserts it into image B at the specified location. If image B is too small for the size of image A required then the whole of image A is placed in image B. Both must be dyadic in length and square images.
ImageB |
A matrix with the specified values selected exchanged to those of Image A. |
Sarah L. Taylor
Taylor, S.L., Eckley, I.A., and Nunes, M.A. (2014) A Test of Stationarity for Textured Images. Technometrics, 56 (3), 291-301.
# Generate 2 images. # ImageA <- matrix(rnorm(256^2), nrow=256, ncol=256) ImageB <- matrix(rnorm(256^2, sd=2.8), nrow=256, ncol=256) # # Insert Image A into Image B at a proportion of 0.25 # MixImaImb <- mix2images(ImageA, ImageB, prop=0.25, pos="e") #
# Generate 2 images. # ImageA <- matrix(rnorm(256^2), nrow=256, ncol=256) ImageB <- matrix(rnorm(256^2, sd=2.8), nrow=256, ncol=256) # # Insert Image A into Image B at a proportion of 0.25 # MixImaImb <- mix2images(ImageA, ImageB, prop=0.25, pos="e") #
A plot function for quadtree decompositions.
## S3 method for class 'imageQT' plot(x, cires, unclassval = 0, class = FALSE, QT = TRUE, return = FALSE, qtl = 1, ...)
## S3 method for class 'imageQT' plot(x, cires, unclassval = 0, class = FALSE, QT = TRUE, return = FALSE, qtl = 1, ...)
x |
A quadtree decomposition object, such as output from |
cires |
Results of |
unclassval |
A value for unclassified values in a quadtree decomposition. |
class |
A boolean value indicating whether to plot the results from |
QT |
A boolean value indicating whether to plot the quadtree decomposition. |
return |
A boolean value indicating whether to return the matrix associated to the plotted image. |
qtl |
Colour specification for the lines drawn in the image segmentation (for |
... |
Any other optional arguments to |
The function plots the chosen quadtree decomposition, and optionally the textured region classification output from countTextures
. If the classification output is plotted (class=TRUE
), each textured region is uniquely coloured according to its texture group.
immat |
the matrix associated to the plotted image. |
Sarah L. Taylor and Matt Nunes
## Not run: X<-simTexture(256,K=1,imtype="NS2")[[1]] XQT<-imageQT(X, bs=100, smooth=FALSE) XCI <- Tex(XQT$imS, bs=100, smooth=FALSE) plot(XQT, XCI, QT=T, class=T) ## End(Not run)
## Not run: X<-simTexture(256,K=1,imtype="NS2")[[1]] XQT<-imageQT(X, bs=100, smooth=FALSE) XCI <- Tex(XQT$imS, bs=100, smooth=FALSE) plot(XQT, XCI, QT=T, class=T) ## End(Not run)
A function which rearranges image content for nice plotting.
plotmtx(m)
plotmtx(m)
m |
An image (matrix) for converting so that it can be plotted. |
Due to the input and plotting output of the R base function image
, this function reorders the pixels within an image such that, when used, the image
function produces a plot of a image (matrix) "as is".
m.out |
The manipulated image corresponding to the input image. |
Matt Nunes
Im<-simTexture(n=256,type="NS4",K=1)[[1]] image(plotmtx(Im))
Im<-simTexture(n=256,type="NS4",K=1)[[1]] image(plotmtx(Im))
This function prints out information about a imageQT object in a nice human-readable form.
## S3 method for class 'imageQT' print(x, ...)
## S3 method for class 'imageQT' print(x, ...)
x |
An object of class 'imageQT' about which you wish to print information. |
... |
This argument actually does nothing in this function! |
Matt Nunes
## Not run: # # Generate a imageQT object for a HaarMontage realisation # X<-simTexture(n=256,K=1,imtype="S1")[[1]] Xres <- imageQT(X) print(Xres) ## End(Not run)
## Not run: # # Generate a imageQT object for a HaarMontage realisation # X<-simTexture(n=256,K=1,imtype="S1")[[1]] Xres <- imageQT(X) print(Xres) ## End(Not run)
This function prints out information about a TOS2D object in a nice human-readable form.
## S3 method for class 'TOS2D' print(x, ...) ## S3 method for class 'TOS2D' summary(object, ...)
## S3 method for class 'TOS2D' print(x, ...) ## S3 method for class 'TOS2D' summary(object, ...)
x , object
|
An object of class 'TOS2D' about which you wish to print information. |
... |
This argument actually does nothing in this function! |
Matt Nunes
## Not run: # # Generate a TOS2D object for a HaarMontage realisation # X<-simTexture(n=256,K=1,imtype="S1")[[1]] Xres <- TOS2D(X) summary(Xres) ## End(Not run)
## Not run: # # Generate a TOS2D object for a HaarMontage realisation # X<-simTexture(n=256,K=1,imtype="S1")[[1]] Xres <- TOS2D(X) summary(Xres) ## End(Not run)
A function with which to generate nonstationary covariance structure.
scurve(x, a = 1, start = 1, end = 2)
scurve(x, a = 1, start = 1, end = 2)
x |
a sequence of x-values. |
a |
The coefficient of slope of the curve. |
start |
a starting value for the curve |
end |
an ending value for the curve |
y |
the function values associated to |
Matt Nunes
x<-seq(0,1,length=100) y<-scurve(x,.4,1,2) plot(x,y,type="l")
x<-seq(0,1,length=100) y<-scurve(x,.4,1,2) plot(x,y,type="l")
This function will generate images of a specified type
simTexture(n = 256, sd = 1, K = 150, imtype = "S1", ...)
simTexture(n = 256, sd = 1, K = 150, imtype = "S1", ...)
n |
The dimension of the image to be generated. |
sd |
The standard deviation of the increments of the LS2W process to be generated. |
K |
The number of images to generate. |
imtype |
The type of image(s) to create. Must be one of "S1","S2","S3","S4", "NS1","NS2", "NS3", "NS4","NS5","NS6", "NS7". See details for descriptions of the processes. |
... |
Any other optional arguments needed for the image generation (see details). |
Several different processes can be generated with the simTexture
function. The stationary processes are: a random normal process of specified standard deviation, sd
(S1
); a spatial moving average process with parameter rho
(S2
); an isotropic random field with a Matern covariance with shape parameter nu
(S3
) and a diagonal Haar moving average process of a specified order order
and standard deviation sd
(S4
) (see the Haar2MA.diag
function in the LS2W
package for more details).
The nonstationary processes are: a random field with unit standard deviation on the first half-plane, concatenated with a random normal half-plane of standard deviation sd
(NS1
); a white noise half-plane concatenated with a Matern stationary process (NS2
); a Haar Montage of specified standard deviation sd
(NS3
) (see the LS2W
HaarMontage
function for more details); a process with a slowly-varying covariance structure (NS4
); a white noise process with a central subregion of random Normal deviates with non-unit standard deviation sd
(NS5
); a white noise process with a subregion of random Normal deviates with non-unit standard deviation in the middle section of the top left quadrant sd
(NS6
); the final process is similar to NS5
, except that there is an additional texture in a subregion of the image. In other words, the image is a montage of three two-dimensional Normal processes with differing standard deviations. The base texture is again of unit variance, whereas the other two textures have standard deviations sd
and
sd2
(NS7
).
The other optional arguments for simTexture
are as follows:type
- the type of neighbourhood dependence for the random field, either "queen"
or "rook"
(see the cell2nb
function documentation in the spdep
package for more details).rho
- moving average parameter for the process S2
.nu
- shape parameter for the Matern covariance for process S3
.order
- Haar moving average order for S4
.fn
- scurve
or lincurve
for NS4
.start
- start value for NS4
(passed into scurve
or lincurve
).end
- end value for NS4
(passed into scurve
or lincurve
).a
- "gradient" for NS4
(passed into scurve
or lincurve
).prop
- proportion of inserted subimage for NS5
, NS6
and the first subimage (NS7
).sd2
- standard deviation of second inserted subimage for NS7
.prop2
- proportion of second inserted subimage for NS7
.pos1
- position of first inserted subimage for NS7
.pos2
- position of second inserted subimage for NS7
.
images |
A list of length |
Generating lots of images of high dimension may take a long time!
Note that as of version 2.1-4 (2022-05-09), textures S3 and NS2 have been temporarily removed (commented out) from the functionality of this function, due to a broken package (RandomFields).
Sarah L. Taylor and Matt Nunes
Matern, B. (1960) Spatial variation. Stochastic models and their application to some problems in forest surveys and other sampling investigations Meddleanden fran statens Skogsforskningsinstitut 49 (5).
Eckley, I.A., Nason, G.P., and Treloar, R.L. (2010) Locally stationary wavelet fields with application to the modelling and analysis of image texture Journal of the Royal Statistical Society Series C, 59, 595-616.
Taylor, S.L., Eckley, I.A., and Nunes, M.A. (2014) A Test of Stationarity for Textured Images. Technometrics, 56 (3), 291-301.
X1 <- simTexture(128,K=1,imtype="S4",order=3) X2 <- simTexture(128,K=1,imtype="NS4",fn=lincurve,a=.25,start=1,end=2) X3 <- simTexture(128,K=1,imtype="NS5",sd=1.6,prop=.25) X4 <- simTexture(128,K=1,imtype="NS7",sd=1.6,prop=.25,sd2=2.8, prop2=0.25, pos1=c(10,10),pos2="e") # try plotting the images: ## Not run: image(plotmtx(X1[[1]]))
X1 <- simTexture(128,K=1,imtype="S4",order=3) X2 <- simTexture(128,K=1,imtype="NS4",fn=lincurve,a=.25,start=1,end=2) X3 <- simTexture(128,K=1,imtype="NS5",sd=1.6,prop=.25) X4 <- simTexture(128,K=1,imtype="NS7",sd=1.6,prop=.25,sd2=2.8, prop2=0.25, pos1=c(10,10),pos2="e") # try plotting the images: ## Not run: image(plotmtx(X1[[1]]))
For a given image this function performs bootstrapping to test the hypothesis that the image is stationary.
TOS2D(image, detrend = FALSE, nsamples = 100, theTS = avespecvar, verbose = TRUE,...)
TOS2D(image, detrend = FALSE, nsamples = 100, theTS = avespecvar, verbose = TRUE,...)
image |
The image you want to analyse. |
detrend |
This specifies whether to use Tukey's median polish to remove the image trend. |
nsamples |
Number of bootstrap simulations to carry out. |
theTS |
Specifies the particular test statistic to be used. This function should measure the departure from constancy of the wavelet spectrum. |
verbose |
If TRUE informative messages are printed. |
... |
Any other arguments supplied to the LS2W function |
This function first of all crops the image (if necessary) to have dyadic dimensions. The test statistic (theTS
), which should be based upon the local wavelet spectrum, is calculated for this original image and the local wavelet spectrum under the null hypothesis is calculated, so as to be able to simulate realisations under the null hypothesis. nsamples
images are simulated and test statistic is found for each. The function returns all the test statistic values which may be passed to getpval
in order to find a p-value for the test. For full details on this testing procedure see Taylor et al. (2014).
A list with the following components:
data.name |
The name of the image analysed. |
samples |
A vector of length |
statistic |
The name of the test statistic used. |
p.value |
The bootstrap p-value for the test. |
Sarah L. Taylor
Taylor, S.L., Eckley, I.A., and Nunes, M.A. (2014) A Test of Stationarity for Textured Images. Technometrics, 56 (3), 291-301.
# Generate a stationary image # testimage <- matrix(rnorm(64*64), nrow=64, ncol=64) # #Run test of stationarity ## Not run: TestofStat<-TOS2D(testimage)
# Generate a stationary image # testimage <- matrix(rnorm(64*64), nrow=64, ncol=64) # #Run test of stationarity ## Not run: TestofStat<-TOS2D(testimage)