cv2.LUT()(使用查找表中的值填充输出数组)

def LUT(src, lut, dst=None): # real signature unknown; restored from __doc__
    """
    LUT(src, lut[, dst]) -> dst
    .   @brief Performs a look-up table transform of an array.
    对数组执行查找表转换。
    .   
    .   The function LUT fills the output array with values from the look-up table. Indices of the entries
    .   are taken from the input array. That is, the function processes each element of src as follows:
    函数LUT使用查找表中的值填充输出数组。 条目的索引取自输入数组。 
    也就是说,该函数按以下方式处理src的每个元素:
    .   \f[\texttt{dst} (I)  \leftarrow \texttt{lut(src(I) + d)}\f]
    .   where
    .   \f[d =  \fork{0}{if \(\texttt{src}\) has depth \(\texttt{CV_8U}\)}{128}{if \(\texttt{src}\) has depth \(\texttt{CV_8S}\)}\f]
    .   @param src input array of 8-bit elements. 8位元素的输入数组。
    
    .   @param lut look-up table of 256 elements; in case of multi-channel input array, the table should
    .   either have a single channel (in this case the same table is used for all channels) or the same
    .   number of channels as in the input array.
    256个元素的查询表; 
    如果是多通道输入数组,则该表应具有单个通道
    (在这种情况下,所有通道都使用相同的表)或与输入阵列中的通道数相同。
    
    .   @param dst output array of the same size and number of channels as src, and the same depth as lut.
    输出数组,其大小和通道数与src相同,深度与lut相同。
    .   @sa  convertScaleAbs, Mat::convertTo
    """
    pass

示例:
使用gamma_table填充数组

import cv2
import numpy as np
# img=np.array(([[[1,2,3],[1,2,3],[1,2,3]],[[1,2,3],[1,2,3],[1,2,3]]])).astype(np.uint8)
# img=np.array(([[1,2,3,4,5,6]])).astype(np.uint8)
img=np.array([1,2,3,4,5,6]).astype(np.uint8)
print(img.shape)	# (6,)
print(img.dtype)	# uint8
gamma=0.8
# 映射表必须为0-255(改成其他会报错)
gamma_table = [np.power(x / 255.0, gamma) * 255.0 for x in range(256)]
# numpy数组默认数据类型为int32,需要将数据类型转换成opencv图像适合使用的无符号8位整型uint8,否则会报错
gamma_table = np.round(np.array(gamma_table)).astype(np.uint8)
print(gamma_table)
img_gamma = cv2.LUT(img, gamma_table)
print(img_gamma)

结果:
gamma_table:
[ 0 3 5 7 9 11 13 14 16 18 19 21 22 24 25 26 28 29
31 32 33 35 36 37 39 40 41 42 44 45 46 47 48 50 51 52
53 54 56 57 58 59 60 61 63 64 65 66 67 68 69 70 71 73
74 75 76 77 78 79 80 81 82 83 84 85 86 88 89 90 91 92
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
111 112 113 114 115 116 117 118 119 120 121 122 123 123 124 125 126 127
128 129 130 131 132 133 134 135 136 137 138 139 140 140 141 142 143 144
145 146 147 148 149 150 151 151 152 153 154 155 156 157 158 159 160 161
161 162 163 164 165 166 167 168 169 169 170 171 172 173 174 175 176 177
177 178 179 180 181 182 183 183 184 185 186 187 188 189 190 190 191 192
193 194 195 196 196 197 198 199 200 201 202 202 203 204 205 206 207 207
208 209 210 211 212 212 213 214 215 216 217 217 218 219 220 221 222 222
223 224 225 226 227 227 228 229 230 231 232 232 233 234 235 236 236 237
238 239 240 240 241 242 243 244 245 245 246 247 248 249 249 250 251 252
253 253 254 255]

img_gamma:
[[ 3]
[ 5]
[ 7]
[ 9]
[11]
[13]]

这里img为索引,取table中索引对应的值进行img填充

转载自:https://blog.csdn.net/Dontla/article/details/103963085

猜你喜欢

转载自blog.csdn.net/qq_35037684/article/details/121575356