试图从极坐标图片中裁剪出中心圆

Trying to crop out center circle from polar coordinate picture

我一直在努力解决方案的几次迭代,试图获得一张已转换为极坐标的照片(最初是矩形半球照片)以裁剪出圆圈之外的区域。最初遵循此 [Whosebug post] with no luck. Here's the image I'm working with (in reality there are thousands I must process):

这是我做的函数:

 hemi_rect_to_polar <- function(hemi_rect_image){
  
  hemi_polar <- image_distort(hemi_rect_image,
                                   "Polar",
                                   c(0),
                                   bestfit = TRUE)
  
  min_dim <- min(image_info(hemi_rect_image)[2])
  
  figure <- image_draw(image_blank(min_dim,min_dim))
  symbols(min_dim/2, 
          min_dim/2, 
          circles = (min_dim/2), 
          fg = 'black', 
          add = TRUE)
  
  dev.off()
  
  return(hemi_polar)
}

以及我从该函数获得的输出,该函数的颜色继续超过圆形边界,我需要摆脱该边界以供以后分析:

我不确定为什么它一直将极坐标拉伸到边界圆之外。

我不确定这是否非常有效,但它确实有效。由于这使用了缩放,因此您只需要制作一个遮罩,然后将其缩放到图像,而不管图像大小。如果图片大小都一样,那你只需要构图就可以了。

library(magick)

path = "https://i.stack.imgur.com/XBDRI.jpg"
img <- image_read(path)

hemi_rect_to_polar <- function(hemi_rect_image){
  hemi_polar <- image_distort(hemi_rect_image,
                              "Polar",
                              c(0),
                              bestfit = TRUE)
  return(hemi_polar)
}

he = hemi_rect_to_polar(img)

# make the mask
png(tf <- tempfile(fileext = "png"), 736, 736)
par(mar = rep(0,4), yaxs="i", xaxs="i")
plot(0, type = "n", ylim = c(0,1), xlim=c(0,1), axes=F, xlab=NA, ylab=NA)
plotrix::draw.circle(.5,0.5,.5, col="black")

# dump the temp file
dev.off()

# read in the plot as an image to use as a mask
mask <- image_read(tf)
# scale the mask for the image size
mask <- image_scale(mask, as.character(image_info(he)$width))

# crop the image
image_composite(mask, he, "plus")