有效地将坐标设置为numpy(位图)数组(不包括屏幕外坐标) [英] Efficiently plot coordinate set to numpy (bitmap) array excluding offscreen coordinates

查看:75
本文介绍了有效地将坐标设置为numpy(位图)数组(不包括屏幕外坐标)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题来自

我进行了一项测试,其中必须分配3145728(是您在其他问题中提供的位图大小的四倍),其中大约一半位于图像之外,平均大约需要140毫秒,而重新映射异常值和然后将同一行的第一行/列设置为零需要200毫秒

This question follows from Efficiently plot set of {coordinate+value}s to (numpy array) bitmap

A solution for plotting from x, y, color lists to a bitmap is given:

bitmap = np.zeros((10, 10, 3))

s_x = (0,1,2) ## tuple
s_y = (1,2,3) ## tuple
pixal_val = np.array([[0,0,1],[1,0,0],[0,1,0]]) ## np

bitmap[s_x, s_y] = pixal_val

plt.imshow(bitmap)

But how to handle the case where some (x,y) pairs lie outside the bitmap?

Efficiency is paramount.

If I could map offscreen coords to the first row/col of the bitmap (-42, 7) -> (0, 7), (15, -6) -> (15, 0), I could simply black out the first row&col with bitmap[:,0,:] = 0; bitmap[0,:,:] = 0.

Is this doable?

Is there a smarter way?

解决方案

Are you expecting offscreen coords? if so don't worry otherwise I was just wondering if it was using a non-traditional coordinate system - where the zero may be in the center of the image for whatever reason

Anyway, after my revelation that you can use numpy arrays to store the coordinates, mapping outliers to the first row/col is pretty straightforward, simply using: s_x[s_x < 0] = 0, however, i believe the most efficient way to use logic to find the index of the pixels you want to use so only they are allocated - see below:

bitmap = np.zeros((15, 16, 3))

## generate data 
s_x = np.array([a for a in range(-3,22)], dtype=int)
s_y = np.array([a for a in range(-4,21)], dtype=int)

np.random.shuffle(s_x)
np.random.shuffle(s_y)

print(s_x)
print(s_y)

pixel_val = np.random.rand(25,3)
## generate is done 

use = np.logical_and(np.logical_and(s_x >= 0, s_x < bitmap.shape[1]), np.logical_and(s_y >= 0, s_y < bitmap.shape[0]))

bitmap[s_y[use], s_x[use]] = pixel_val[use]
    
plt.imshow(bitmap)

output:

coordinates:

[ 8  3 21  9 -2 -3  5 14 -1 18 13 16  0 11  7  1  2 12 15  6 19 10  4 17 20]
[ 8 14  1  9  2  4  7 15  3 -3 19 16  6 -1  0 17  5 13 -2 20 -4 11 10 12 18]

image:

I ran a test where it had to allocate 3145728 (four times the size of the bitmap you gave in your other question), around half of which were outside the image and on average it took around 140ms, whereas remapping the outliers and then setting the first row/col to zero took 200ms for the same task

这篇关于有效地将坐标设置为numpy(位图)数组(不包括屏幕外坐标)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆