0

I have a stars raster object called raster. I am trying to run edge detection on it like:

image(rot90c(edge.detect(raster[[1]], thresh1=1, thresh2=80, noise="gaussian", noise.s=3, method="Canny")))  

This works fine, but when I try to crop the raster against an sf object other_object using:

image(rot90c(edge.detect(st_crop(raster,other_object)[[1]], thresh1=1, thresh2=80, noise="gaussian", noise.s=3, method="Canny")))  

I get errors due to NAs. Is there any way to create a cropped raster such that the edge detection method doesn't throw an error due to missing regions from the image?

coder11
  • 15
  • 3
  • I guess I would ask, if you rasterize, crop, then edge.detect `other_object`, does your thresh2 value still make sense, or perhaps it should be smaller relative to the crop that happens in your errored call? This is really a function of what's going on in `wvtool` whose presence in the problem set is probably worth mentioning. – Chris Nov 02 '21 at 21:08
  • Thank you for your reply! I don't think it has so much to do with the threshold as an exception being thrown when running into NA values... cropping basically introduces NA values into the raster, where there used to be none, which triggers an exception with the edge.detect() function. I'm wondering if there's any known way to work around this case... – coder11 Nov 02 '21 at 22:32
  • 1
    Well, as you have the R objects of interest (i.e. at this point a non-MRE), test it out by `debugonce(edge.detect)` on your erroring st_crop, and step through, or do the same but replace other_object with variants of raster that are reduced in size.... Just some thoughts on debugging this. – Chris Nov 02 '21 at 22:47
  • I get the following error: Error in h(simpleError(msg, call)) : error in evaluating the argument 'x' in selecting a method for function 'image': missing value where TRUE/FALSE needed. I think the problem is that cropping takes a previously square image and introduces NA values (blank regions) into the image. I'm wondering if there's a way to fix this with edge.detect. E.g. is there some standard way to fill in blank regions before running edge detection? – coder11 Nov 03 '21 at 16:44
  • 1
    And difference if you crop first then pass that into edge.detect? – Chris Nov 03 '21 at 20:08
  • No difference, it's the same error – coder11 Nov 03 '21 at 20:21
  • Order and type of objects in `st_crop(` matter, and I think you're experiencing condition in the third approach on [man-st_crop](https://rdrr.io/cran/stars/man/st_crop.html), as well as how you specify intersection to be approached (as points or polygons). edge.detect doesn't specify abhorance of NAs, and the error msg approach is more in the style of sf-verse. – Chris Nov 03 '21 at 20:53

0 Answers0