Python 使用带有openCV和NumPy的查找表
我尝试使用NumPy和CV2,对像素数组进行像素数学运算,然后使用查找表并用查找表的值替换该像素 这是可行的,但速度太慢了Python 使用带有openCV和NumPy的查找表,python,opencv,numpy,pixel,Python,Opencv,Numpy,Pixel,我尝试使用NumPy和CV2,对像素数组进行像素数学运算,然后使用查找表并用查找表的值替换该像素 这是可行的,但速度太慢了 image = cv2.imread('C:\\Users\\Event38\\Desktop\\IMG_2231.JPG') height, width, depth = image.shape image = image.astype('float') B = image[:, :, 0] G = image[:, :, 1] R = image[:, :, 2]
image = cv2.imread('C:\\Users\\Event38\\Desktop\\IMG_2231.JPG')
height, width, depth = image.shape
image = image.astype('float')
B = image[:, :, 0]
G = image[:, :, 1]
R = image[:, :, 2]
num = ((R+G)-(2*B))
den = ((R+G)+(2*B))
NDVI = ((num/den)*127.5) + 127.5
print NDVI[0,0] # print array NDVI spot 0,0 value
print r[NDVI[0,0]][0] # looks up NDVI's spot 0,0 in LUT
for i in range(0, height):
for j in range (0, width):
image.itemset((i,j,0),r[NDVI[i,j]][3])
image.itemset((i,j,1),r[NDVI[i,j]][2])
image.itemset((i,j,2),r[NDVI[i,j]][1])
#image = np.where(den == 0,1, NDVI).clip(0.0, 255.0)
任何关于如何使for循环部分的代码更快工作的建议都是非常好的,我知道有一些方法可以消除它。谢谢 您可以尝试使用OpenCV的查找表一次性应用LUT。这是你的电话号码 C++
void LUT(InputArray src, InputArray lut, OutputArray dst, int interpolation=0 )
cv2.LUT(src, lut[, dst[, interpolation]]) → dst
Python
void LUT(InputArray src, InputArray lut, OutputArray dst, int interpolation=0 )
cv2.LUT(src, lut[, dst[, interpolation]]) → dst
这完全取决于
r
是什么。