python - 处理像素坐标来自行列表的图像的像素值
问题描述
我正在从事图像处理(python3)的任务,希望能找到算法的改进。我的任务有两个主要规格:
- 3张灰度图像(大小相同,命名为A、B、C)
- 直线坐标列表(numpy数组)((x,y)是一个点的坐标)
[[x1 y1 x2 y2] ... [x1n y1n x2n y2n]]
处理步骤:
- 将一条线的所有像素坐标放入列表 (x_i, y_i)
- 获取坐标(x_i,y_i)对应的每3幅图像(A,B,C)的像素值(p_x,p_y)。将所有值(p_x,p_y)(分别为A,B,C)求和,然后我有(scoreA,scoreB,scoreC)
- 在某些条件下测试(scoreA、scoreB、scoreC),如果不满足则忽略该行。
- 用其他行重复这些步骤。
目前,我的想法是创建一个名为“line_obj”的类来存储线路信息。
class line_obj:
def __init__(self, x1, y1, x2, y2):
self.x1 = x1
self.y1 = y1
self.x2 = x2
self.y2 = y2
self.scoreA = 0.0
self.scoreB = 0.0
self.scoreC = 0.0
然后我创建一个 line_obj(s) 列表,其中包含每一行的信息。之后,我对上述所有任务使用 for 循环。
for line_obj in line_obj_list:
#line() method from scikit-image library to retrieve pixel coordinates of a line
rr, cc = line(line_obj.y1, line_obj.x1, line_obj.y2, line_obj.x2)
line_obj.scoreA = A[rr,cc].sum()
line_obj.scoreB = B[rr,cc].sum()
line_obj.scoreC = C[rr,cc].sum()
对于这个任务,我需要让它尽可能快,不确定它是否被优化。你能给我一个更好的主意吗?谢谢吨。
解决方案
我实际上不了解您的坐标数据的结构。你说这是一个 numpy 数组,但你写的是一个平面坐标列表。不管是什么,最好将其转换为numpy。如果想象你有以下数据:
import numpy as np
A = np.random.randint(0, 255, (1024, 1024)) #1024 is just example pic size
B = np.random.randint(0, 255, (1024, 1024)) #1024 is just example pic size
C = np.random.randint(0, 255, (1024, 1024)) #1024 is just example pic size
coords = np.random.randint(0, 1023, (200, 4)) #assuming you have 200*2 coords or whatever size the scikit outputs
#Here I'm assuming x is the rows and y is columns
A_pix_values_c1 = A[coords[:, 0], coords[: , 1]] #c1 is coords 1
A_pix_values_c2 = A[coords[:, 2], coords[: , 3]] #c2 is coords 2
B_pix_values_c1 = B[coords[:, 0], coords[: , 1]]
B_pix_values_c2 = B[coords[:, 2], coords[: , 3]]
C_pix_values_c1 = C[coords[:, 0], coords[: , 1]]
C_pix_values_c2 = C[coords[:, 2], coords[: , 3]]
现在我在猜测,因为我不太了解您要做什么。我假设您想将每个图像的两个坐标的像素值相加。
scoreA = A_pix_values_c1 + A_pix_values_c2
scoreB = B_pix_values_c1 + B_pix_values_c2
scoreC = C_pix_values_c1 + C_pix_values_c2
最好尽可能避免循环。我对 scikit 图像不太熟悉,但是如果您可以一次将所有行放在一个数组中,那么最好这样做,然后应用上述方法而不是循环遍历。对于测试,您可以使用多种方式的 numpy 数组,例如
final_scoreA = scoreA[scoreA_condition]
推荐阅读
- uipath - 如何在 UIPath 中向下(递归)
- reactjs - 用于测试 json 结构上的组件映射的笑话问题
- python - 用于分类的 Pytorch 神经网络
- javascript - 获取失败
- sublimetext3 - Sublime 侧边栏创建自定义菜单
- spring - Spring kafka消费者停止接收消息
- tensorflow - Keras:model.evaluate() 的结果保持高位,所有权重和偏差均为 0
- ios - 在具有多个 android 应用程序和一个 iOS 应用程序的 firebase 中注册新捆绑包 ID 时出错
- ansible - 如何在ansible中显示消息
- android - 防止黑带跑到屏幕上?