在原始图像上绘制点模式

2024-04-27 19:00:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个组织病理学图像,我使用开发的算法提取感兴趣的细胞作为点模式。你知道吗

现在我想在原始图像上绘制点模式,我的期望应该是这样的: enter image description here

首先我尝试使用R加载原始图像,但是加载的图像与轴一起显示,没有如图所示的网格线。所以我想知道如何复制图中所示的格式。 谢谢! enter image description here 有关绘图的“fig”变量的信息: enter image description here


Tags: 图像算法信息绘图格式模式fig绘制
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()

结果: Image result

下面是一个使用R的建议解决方案,使用EBImage包。我喜欢这个,因为它很容易与图像交互。你知道吗

# 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()相互作用)。你知道吗

Grids added to image with EBImage

相关问题 更多 >