How to save results of SuperpixelImageSegmentation as a raster - r

I am using library(SuperpixelImageSegmentation) to segmenting my image with associated coordinate NZTM. I would like to save get the segment results as a raster/vector file with associated coordinate NZTM.
When I run my script as below, it has error " Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘writeRaster’ for signature ‘"array", "character"". How to fix it?
library(SuperpixelImageSegmentation)
library(OpenImageR)
library(sp)
library(raster)
library(rgdal)
memory.limit(size=800000)
path = 'C:/Data/SLICO/image.tif'
im = OpenImageR::readImage(path)
init = Image_Segmentation$new()
spx = init$spixel_segmentation(input_image = im,
superpixel =400,
AP_data = TRUE,
use_median = TRUE,
sim_wL = 3,
sim_wA = 10,
sim_wB = 10,
sim_color_radius = 10,
verbose = TRUE)
str(spx)
OpenImageR::imageShow(spx$AP_image_data)
b=writeRaster(spx$AP_image_data, filename='C:/Data/test.tif', format="GTiff", overwrite=TRUE)

Related

error in t(dat1) and pairwise not found errors in R

Can someone tell me why the Error in t(dat1) : object 'dat1' not found is appearing, and why Error in is.element("pairwise", names(x)) : object 'pwStats' not found is too?
I'm new to R, and I am trying to visualize the Djost graph shown here:
pwStats <-fastDivPart(infile = microsatellitecoyreadyforR,
outfile = "Coyote_resultspwstats",
gp = 2, bs_locus = TRUE,
bs_pairwise = TRUE, boots = 3)
Error in t(dat1) : object 'dat1' not found
#visualize pwStats
diffPlot(x=pwStats,outfile = "Coyote_resultspwstats", interactive=TRUE)
Error in is.element("pairwise", names(x)) : object 'pwStats' not found

Create empty spatial lines object

I am trying to create an empty SpatialLines object. With polygons it is easy:
SpatialPolygons(list())
For spatial lines this does not work:
SpatialLines(LinesList = list())
Error in bb[1, ] : incorrect number of dimensions
SpatialLines(LinesList = Lines(list(),ID = "a"))
Error in as.list.default(X) :
no method for coercing this S4 class to a vector
SpatialLines(LinesList = Lines(slinelist = Line(coords = cbind(x = c(), y = c())), ID = c()))
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘coordinates’ for signature ‘"NULL"’
Does someone know how it I could create an empty SpatialLines object?
Workaround
I found a workaround which is maybe not the best way of doing it. I generates a spatial line with no length:
SpatialLines(list(Lines(Line(coords = cbind(x = c(0,0), y = c(0,0))), ID = "A")))
Interesting Q!
The only way I was able to work around was by creating a dummy line and removing it like this:
sl <- SpatialLines(LinesList = list(Lines(Line(matrix(0, ncol = 2)), ID = NA)))
sl <- sl[0]
length(sl)
# [1] 0
When adding your dummy line the length is returned as 1 as expected:
length(rbind.SpatialLines(sl, SpatialLines(list(Lines(Line(coords = cbind(x = c(0,0),
y = c(0,0))),
ID = "A")))))
# [1] 1

How can I create an Item Frequency Plot for association rule data? By converting to data frame or numeric?

I have given the code that the rules are generated.I would like to create an item frequency histogram plot but ıt didn't work.
library(arules)
library(arulesViz)
library(rattle)
x <- read.table("C:/Users/toshıba pc/Desktop/Kitap2.csv", header = TRUE, sep = ";")
y <- as.matrix(x)
rules <- apriori(y, parameter = list(supp = 0.1, conf = 0.8))
itemFrequencyPlot(rules, support = 0.1, cex.names = 0.8)
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘itemFrequencyPlot’ for signature ‘"rules"’
itemFrequencyPlot is not defined for rules. You can use
itemFrequencyPlot(items(rules))
to get the frequency of the items in the rules, but I am not sure that this will give you the results you want.

Using XLConnect to read in xls

I'm using the following code to try and read in an old xls file
library("XLConnect")
path <- "C:/Users/foo/Desktop/WEEK 17.xls"
df <- readWorksheet(path, sheet = 1)
i get the following error
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘readWorksheet’ for signature ‘"character", "numeric"’
Does anyone know why?
I can open the file in excel
It s because readWorksheet use workbook as object.
You have to
# Load workbook
wb <- loadWorkbook("C:/Users/foo/Desktop/WEEK 17.xls")
and then
df <- readWorksheet(wb, sheet = 1)
or use
readWorksheetFromFile("C:/Users/foo/Desktop/WEEK 17.xls", sheet = 1)
Which realy do the same
> XLConnect::readWorksheetFromFile
function (file, ...)
{
args <- list(...)
args$object <- loadWorkbook(file, create = FALSE)
do.call("readWorksheet", args)
}
<environment: namespace:XLConnect>

KDE to Raster conversion fails when eval.points are specified

I want to match a kernel density layer's {ks} raster dimensions to kriged surface {spatial} and have run into an error. The code demonstrates:
require(ks)
require(raster)
require(spatial)
data(topo, package="MASS")
topo.kr = surf.gls(2, expcov, topo, d=0.7)
krig_ras = raster(prmat(topo.kr, 0, 6.5, 0, 6.5, 50))
kd = kde(topo[,1:2])
kde_ras = raster(kd) # works ok
par(mfrow = c(1, 2))
plot(krig_ras); title('krig')
plot(kde_ras); title('kde')
As you can see the dimensions don't match as kde has added a border (see also setMinMax(krig_ras); setMinMax(kde_ras)). But we can specify eval.points:
pts = as.data.frame(krig_ras, xy=TRUE)
kd2 = kde(topo[,1:2], eval.points = pts[,1:2])
kde_ras2 = raster(kd2)
The last returns the error:
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘raster’ for signature ‘"numeric"’
This R-sig-geo page seems to cover the same error, but I can't figure out if this is the same cause - numeric layerNames. Looking at the names/str for kd2 doesn't suggest anything. Grateful for assistance.
If I understand, you would like something like that:
kde_ras2 <- krig_ras
kde_ras2 <- setValues(kde_ras2, kd2$estimate)

Resources