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()
# 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)
您可以使用OpenCV和Python执行此操作:
(编辑:添加垂直线)
结果:
下面是一个使用R的建议解决方案,使用
EBImage
包。我喜欢这个,因为它很容易与图像交互。你知道吗这一步利用了与R中图形元素交互的便利性。这里,
locator
用于在网格线的交点处放置一个标记并记录x,y坐标。然后,在假设图像的方向是垂直和水平的网格线的情况下,将网格线添加到图像中。你知道吗如果仅选择了沿对角线的交点,则不必使用以下代码。这个额外的代码容纳了任意数量的选择来确定唯一的网格线(只要选择包括每个网格线中的一个)。平均值将从多项选择中确定。你知道吗
网格线是用最简单的基函数添加的。如果需要,可以添加更复杂的线条。为了说明其他可能性,每个网格线的坐标(以像素为单位)放在这里。你知道吗
结果(通过
locator()
相互作用)。你知道吗相关问题 更多 >
编程相关推荐