在Python中从图像中提取每个像素的x、y坐标
假设我有一个彩色图像,我已经加载到一个尺寸为200x300x3的numpy数组中。图像中总共有60000个像素。我试图从代表像素1的左上角开始提取每个像素的宽度、高度x、y坐标,这样:在Python中从图像中提取每个像素的x、y坐标,python,pandas,opencv,image-processing,pca,Python,Pandas,Opencv,Image Processing,Pca,假设我有一个彩色图像,我已经加载到一个尺寸为200x300x3的numpy数组中。图像中总共有60000个像素。我试图从代表像素1的左上角开始提取每个像素的宽度、高度x、y坐标,这样: pixel# x y 1 0 0 2 1 0 . . 301 0 1 302 1 1 . . 60,000 299 199 我很想使用for循环以更手动的方式来实现这一点,但是是否有库或更有效的方法来获取每个像素的坐标值
pixel# x y
1 0 0
2 1 0
.
.
301 0 1
302 1 1
.
.
60,000 299 199
我很想使用for循环以更手动的方式来实现这一点,但是是否有库或更有效的方法来获取每个像素的坐标值?在您的示例中,像素索引顺序表示行主顺序。因此,可以使用以下函数获取任意像素的x和y值:
def idx_to_xy(idx):
'''
Assumes idx starts at one, as in the provided example
'''
x = (idx - 1) % 300
y = (idx - 1) // 300
return x, y
for px in [1, 2, 301, 302, 60000]:
x, y = idx_to_xy(px)
print("%5d %3d %3d" %(px, x, y))
# pixels x y
# 1 0 0
# 2 1 0
# 301 0 1
# 302 1 1
# 60000 299 199
在您的示例中,像素索引顺序表示行主顺序。因此,可以使用以下函数获取任意像素的x和y值:
def idx_to_xy(idx):
'''
Assumes idx starts at one, as in the provided example
'''
x = (idx - 1) % 300
y = (idx - 1) // 300
return x, y
for px in [1, 2, 301, 302, 60000]:
x, y = idx_to_xy(px)
print("%5d %3d %3d" %(px, x, y))
# pixels x y
# 1 0 0
# 2 1 0
# 301 0 1
# 302 1 1
# 60000 299 199
由于您显示的格式似乎是pandas,因此我将使用pandas显示输出,但您可以仅使用打印: 我甚至把你的问题的n维解决方案作为评论 将numpy作为np导入 来自itertools进口产品 arr=np.array[ 列表范围300 对于200范围内的uu ] 打印阵列形状 200, 300 像素=arr.reforme-1 n维解 坐标=地图范围,arr.shape 索引=np.arraylist乘积*坐标 xs=rangearr.shape[0] ys=范围arr.形状[1] 索引=np.arraylistproductxs,ys 作为pd进口熊猫 pd.options.display.max_行数=20 索引=pd.Seriespixels,名称=像素 df=pd.DataFrame{ x:指数[:,0], y:指数[:,1] },index=index printdf xy 像素 0 0 0 1 0 1 2 0 2 3 0 3 4 0 4 5 0 5 6 0 6 7 0 7 8 0 8 9 0 9 ... ... ... 290 199 290 291 199 291 292 199 292 293 199 293 294 199 294 295 199 295 296 199 296 297 199 297 298 199 298 299 199 299 [60000行x 2列]
由于您显示的格式似乎是pandas,因此我将使用pandas显示输出,但您可以仅使用打印: 我甚至把你的问题的n维解决方案作为评论 将numpy作为np导入 来自itertools进口产品 arr=np.array[ 列表范围300 对于200范围内的uu ] 打印阵列形状 200, 300 像素=arr.reforme-1 n维解 坐标=地图范围,arr.shape 索引=np.arraylist乘积*坐标 xs=rangearr.shape[0] ys=范围arr.形状[1] 索引=np.arraylistproductxs,ys 作为pd进口熊猫 pd.options.display.max_行数=20 索引=pd.Seriespixels,名称=像素 df=pd.DataFrame{ x:指数[:,0], y:指数[:,1] },index=index printdf xy 像素 0 0 0 1 0 1 2 0 2 3 0 3 4 0 4 5 0 5 6 0 6 7 0 7 8 0 8 9 0 9 ... ... ... 290 199 290 291 199 291 292 199 292 293 199 293 294 199 294 295 199 295 296 199 296 297 199 297 298 199 298 299 199 299 [60000行x 2列]
假设我理解您的问题,这里有一个使用Python/OpenCV的非常简单的方法。将图像转换为灰度,然后使用np.where
import cv2
import numpy as np
# create red image
img = np.full((10,10,3), (0,0,255), dtype=np.uint8)
# convert to grayscale
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# get coordinates (y,x) --- alternately see below for (x,y)
yx_coords = np.column_stack(np.where(gray >= 0))
print (yx_coords)
print ('')
# get coordinates (x,y)
xy_coords = np.flip(np.column_stack(np.where(gray >= 0)), axis=1)
print (xy_coords)
x,y的返回值:
[[0 0]
[1 0]
[2 0]
[3 0]
[4 0]
[5 0]
[6 0]
[7 0]
[8 0]
[9 0]
[0 1]
[1 1]
[2 1]
[3 1]
[4 1]
[5 1]
[6 1]
[7 1]
[8 1]
[9 1]
[0 2]
[1 2]
[2 2]
[3 2]
[4 2]
[5 2]
[6 2]
[7 2]
[8 2]
[9 2]
[0 3]
[1 3]
[2 3]
[3 3]
[4 3]
[5 3]
[6 3]
[7 3]
[8 3]
[9 3]
[0 4]
[1 4]
[2 4]
[3 4]
[4 4]
[5 4]
[6 4]
[7 4]
[8 4]
[9 4]
[0 5]
[1 5]
[2 5]
[3 5]
[4 5]
[5 5]
[6 5]
[7 5]
[8 5]
[9 5]
[0 6]
[1 6]
[2 6]
[3 6]
[4 6]
[5 6]
[6 6]
[7 6]
[8 6]
[9 6]
[0 7]
[1 7]
[2 7]
[3 7]
[4 7]
[5 7]
[6 7]
[7 7]
[8 7]
[9 7]
[0 8]
[1 8]
[2 8]
[3 8]
[4 8]
[5 8]
[6 8]
[7 8]
[8 8]
[9 8]
[0 9]
[1 9]
[2 9]
[3 9]
[4 9]
[5 9]
[6 9]
[7 9]
[8 9]
[9 9]]
假设我理解您的问题,这里有一个使用Python/OpenCV的非常简单的方法。将图像转换为灰度,然后使用np.where
import cv2
import numpy as np
# create red image
img = np.full((10,10,3), (0,0,255), dtype=np.uint8)
# convert to grayscale
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# get coordinates (y,x) --- alternately see below for (x,y)
yx_coords = np.column_stack(np.where(gray >= 0))
print (yx_coords)
print ('')
# get coordinates (x,y)
xy_coords = np.flip(np.column_stack(np.where(gray >= 0)), axis=1)
print (xy_coords)
x,y的返回值:
[[0 0]
[1 0]
[2 0]
[3 0]
[4 0]
[5 0]
[6 0]
[7 0]
[8 0]
[9 0]
[0 1]
[1 1]
[2 1]
[3 1]
[4 1]
[5 1]
[6 1]
[7 1]
[8 1]
[9 1]
[0 2]
[1 2]
[2 2]
[3 2]
[4 2]
[5 2]
[6 2]
[7 2]
[8 2]
[9 2]
[0 3]
[1 3]
[2 3]
[3 3]
[4 3]
[5 3]
[6 3]
[7 3]
[8 3]
[9 3]
[0 4]
[1 4]
[2 4]
[3 4]
[4 4]
[5 4]
[6 4]
[7 4]
[8 4]
[9 4]
[0 5]
[1 5]
[2 5]
[3 5]
[4 5]
[5 5]
[6 5]
[7 5]
[8 5]
[9 5]
[0 6]
[1 6]
[2 6]
[3 6]
[4 6]
[5 6]
[6 6]
[7 6]
[8 6]
[9 6]
[0 7]
[1 7]
[2 7]
[3 7]
[4 7]
[5 7]
[6 7]
[7 7]
[8 7]
[9 7]
[0 8]
[1 8]
[2 8]
[3 8]
[4 8]
[5 8]
[6 8]
[7 8]
[8 8]
[9 8]
[0 9]
[1 9]
[2 9]
[3 9]
[4 9]
[5 9]
[6 9]
[7 9]
[8 9]
[9 9]]
我不清楚这是从哪里开始的。你到底在提取什么?似乎上面的数据帧可以用2个简单的数字X=300和Y=200创建,基本上没有其他信息。是这样吗?我不清楚这是从哪里开始的。你到底在提取什么?似乎上面的数据帧可以用2个简单的数字X=300和Y=200创建,基本上没有其他信息。是这样吗?