[英]Pixel coordinates from point A to B
I have a gray scale 50 x 50 pixels picture as a numpy 2D array.我有一个灰度50 x 50 像素的图片作为 numpy 2D 数组。
Each pixel is a coordinate starting top left [ 0, 0 ] to bottom right [ 50, 50 ] .每个像素都是从左上角[0, 0]到右下角[50, 50]的坐标。
How do i get coordinates of each pixel that is on the line from point A to B where those points can be any given pixel pairs ie A[ 19, 3 ] to B[ 4, 4 ] or A[ 3, 12 ] to B[ 0, 33 ]?我如何获得从点 A 到 B 的线上的每个像素的坐标,这些点可以是任何给定的像素对,即 A[19, 3] 到 B[4, 4] 或 A[3, 12] 到 B [0, 33]?
Example: line from A [ 4, 9 ] to B[ 12, 30 ] crosses which pixels?示例:从 A [ 4, 9 ] 到 B[ 12, 30 ] 的线穿过哪些像素?
Thanks in advance提前致谢
Evo埃沃
You can interpolate your image to extract a line profile if that is what you wish to do, this way the coordinates need not be integers:如果您希望这样做,您可以插入图像以提取线条轮廓,这样坐标不必是整数:
from scipy.ndimage import map_coordinates
from skimage.data import coins
from matplotlib import pyplot as plt
import numpy as np
npts = 128
rr = np.linspace(30, 243, npts) # coordinates of points defined here
cc = np.linspace(73, 270, npts)
image = coins()
# this line extracts the line profile from the image
profile = map_coordinates(image, np.stack((rr, cc)))
# can visualise as
fig, ax = plt.subplots(ncols=2)
ax[0].matshow(image)
ax[0].plot(cc, rr, 'w--')
ax[1].plot(profile) # profile is the value in the image along the line
What I figure out helps is to calculate pixel value at a curtain point on the line (vector).我发现有助于计算线(矢量)上窗帘点的像素值。 Code below:
下面的代码:
coordA = [0,0]
coordB = [3,4]
def intermid_pix(coorA, coorB, nb_points=8):
x_axis = (coorB[0] - coorA[0]) / (nb_points + 1)
y_axis = (coorB[1] - coorA[1]) / (nb_points + 1)
rounded = [[round(coorA[0] + i * x_axis), round(coorA[1] + i * y_axis)]
for i in range(1, nb_points + 1)]
rounded_trim = []
[rounded_trim.append(x) for x in rounded if x not in rounded_trim]
return rounded_trim
coordinates = intermed_pix(coordA, coordB, nb_points=8)
print(coordinates)
output: output:
[[0, 0], [1, 1], [1, 2], [2, 2], [2, 3], [3, 4]]
With scikit-image line() like this:使用scikit-image line()像这样:
import numpy as np
from skimage.draw import line
from skimage import io
# Create image
img = np.zeros((50, 50), dtype=np.uint8)
# Get row and col of pixels making line from (4,9) to (12,30)
rr, cc = line(4, 9, 12, 30)
print(rr)
array([ 4, 4, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 9, 9, 9, 10, 10,
10, 11, 11, 12, 12])
print(cc)
array([ 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
26, 27, 28, 29, 30])
# Visualise as image instead of list
img[rr, cc] = 255
io.imsave('result.jpg',img)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.