我有一个组织病理学图像,我使用发达的算法提取了感兴趣的细胞作为点模式。
首先,我尝试使用R加载原始图像,但是,加载的图像与轴一起显示,并且如图所示没有网格线。所以我想知道如何复制如图所示的格式。 谢谢! 有关图的“ fig”变量的信息:
答案 0 :(得分:2)
您可以使用OpenCV和Python执行此操作:
(编辑:添加了垂直线)
import numpy as np
import cv2
# Load image
img = cv2.imread('your_image.jpg', cv2.IMREAD_COLOR)
# draw grid
spacing = 200
color = (255,0,255) # magenta
linewidth = 5
# horizontal lines
ystart = 20
for i in range(3):
x1, x2 = 0, img.shape[1]
y = ystart + spacing * i
cv2.line(img, (x1,y), (x2,y), color, linewidth)
# vertical lines
xstart = 60
for i in range(3):
y1, y2 = 0, img.shape[0]
x = xstart + spacing * i
cv2.line(img, (x,y1), (x,y2), color, linewidth)
# create a bunch of locations for dots
num = 50
xs = np.random.randint(0,img.shape[1],num)
ys = np.random.randint(0,img.shape[0],num)
# draw the dots on the image (use red so they stand out)
radius = 10
color = (0,0,255)
for i in range(num):
cv2.circle(img,(xs[i],ys[i]), radius, color, -1)
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
答案 1 :(得分:1)
以下是使用EBImage
包的R的建议解决方案。我之所以喜欢它,是因为它易于与图像进行交互。
# EBImage is required
if (!require(EBImage)) {
source("https://bioconductor.org/biocLite.R")
biocLite("EBImage")
library(EBImage)
}
# Read the image and plot it with no borders, 300 ppi
fn <- file.choose() # select saved image
img <- readImage(fn)
img <- channel(img, "gray") # gray scale for simplicity
dev.new(width = dim(img)[1]/300, height = dim(img)[2]/300)
plot(img)
此步骤利用了与R中的图形元素进行交互的便利性。这里,locator
用于在网格线的交点处放置标记并记录x,y坐标。然后在假定图像的方向垂直和水平延伸的情况下,将网格线添加到图像中。
# Use locator() to interactively identify convenient intersections
pp <- locator(type = "p", pch = 3, col = 2) # end with ctrl-click
如果仅选择沿对角线的交点,则以下代码不是必需的。此额外的代码可容纳任意数量的选择以确定唯一的网格线(只要选择包括每条网格线之一)。平均值将从多个选择中确定。
# Little more coding to extract and plot unique grid lines
breaks <- lapply(pp, function(v) hist(v, plot = FALSE)$breaks)
groups <- Map(cut, pp, breaks)
pp <- Map(function(v, g) tapply(v, g, mean), pp, groups)
pp <- lapply(pp, function(x) x[!is.na(x)]) # to re-use if needed
# Place grid lines on new image
plot(img)
abline(v = pp$x, h = pp$y, col = 2)
使用最简单的基本功能添加了网格线。如果需要,可以添加更复杂的行。为了说明其他可能性,此处放置了每条网格线的坐标(以像素为单位)。
text(min(pp$x), pp$y, round(pp$y), col = 2, adj = c(1, -0.2))
text(pp$x, max(pp$y), round(pp$x), col = 2, adj = c(0, 1.2))
结果(通过locator()
进行了交互。