![[bug] numpy.astype(uint8)和opencv imwrite函数的自动数据转换 [bug] numpy.astype(uint8)和opencv imwrite函数的自动数据转换](https://image.shishitao.com:8440/aHR0cHM6Ly93d3cuaXRkYWFuLmNvbS9nby9hSFIwY0RvdkwybHRaeTVpYkc5bkxtTnpaRzR1Ym1WMEx6SXdNVFl3TkRBM01qRXlNakk1TmpVM1AzZGhkR1Z5YldGeWF5OHlMM1JsZUhRdllVaFNNR05FYjNaTU1rcHpZakpqZFZrelRtdGlhVFYxV2xoUmRpOW1iMjUwTHpWaE5rdzFUREpVTDJadmJuUnphWHBsTHpRd01DOW1hV3hzTDBrd1NrSlJhMFpEVFVFOVBTOWthWE56YjJ4MlpTODNNQzluY21GMmFYUjVMME5sYm5SbGNnPT0%3D.jpg?w=700)
![[bug] numpy.astype(uint8)和opencv imwrite函数的自动数据转换 [bug] numpy.astype(uint8)和opencv imwrite函数的自动数据转换](https://image.shishitao.com:8440/aHR0cHM6Ly93d3cuaXRkYWFuLmNvbS9nby9hSFIwY0RvdkwybHRaeTVpYkc5bkxtTnpaRzR1Ym1WMEx6SXdNVFl3TkRBM01qRXlNek13TWpNMVAzZGhkR1Z5YldGeWF5OHlMM1JsZUhRdllVaFNNR05FYjNaTU1rcHpZakpqZFZrelRtdGlhVFYxV2xoUmRpOW1iMjUwTHpWaE5rdzFUREpVTDJadmJuUnphWHBsTHpRd01DOW1hV3hzTDBrd1NrSlJhMFpEVFVFOVBTOWthWE56YjJ4MlpTODNNQzluY21GMmFYUjVMME5sYm5SbGNnPT0%3D.jpg?w=700)
![[bug] numpy.astype(uint8)和opencv imwrite函数的自动数据转换 [bug] numpy.astype(uint8)和opencv imwrite函数的自动数据转换](https://image.shishitao.com:8440/aHR0cHM6Ly93d3cuaXRkYWFuLmNvbS9nby9abWxzWlRvdkx5OURPaTlWYzJWeWN5OXFaWEpsYlhrdlFYQndSR0YwWVM5TWIyTmhiQzlVWlcxd0wyVnVhSFJ0YkdOc2FYQXZaR1Z0YjE5bmIyOWtYK1dKcithY3JDNXFjR2M9.jpg?w=700)
![[bug] numpy.astype(uint8)和opencv imwrite函数的自动数据转换 [bug] numpy.astype(uint8)和opencv imwrite函数的自动数据转换](https://image.shishitao.com:8440/aHR0cHM6Ly93d3cuaXRkYWFuLmNvbS9nby9abWxzWlRvdkx5OURPaTlWYzJWeWN5OXFaWEpsYlhrdlFYQndSR0YwWVM5TWIyTmhiQzlVWlcxd0wyVnVhSFJ0YkdOc2FYQXY1cnFpNVllNjZaU1o2Syt2TGtwUVJ3PT0%3D.jpg?w=700)
![[bug] numpy.astype(uint8)和opencv imwrite函数的自动数据转换 [bug] numpy.astype(uint8)和opencv imwrite函数的自动数据转换](https://image.shishitao.com:8440/aHR0cHM6Ly93d3cuaXRkYWFuLmNvbS9nby9hSFIwY0RvdkwybHRaeTVpYkc5bkxtTnpaRzR1Ym1WMEx6SXdNVFl3TkRBM01qRXlORE00TmpReVAzZGhkR1Z5YldGeWF5OHlMM1JsZUhRdllVaFNNR05FYjNaTU1rcHpZakpqZFZrelRtdGlhVFYxV2xoUmRpOW1iMjUwTHpWaE5rdzFUREpVTDJadmJuUnphWHBsTHpRd01DOW1hV3hzTDBrd1NrSlJhMFpEVFVFOVBTOWthWE56YjJ4MlpTODNNQzluY21GMmFYUjVMME5sYm5SbGNnPT0%3D.jpg?w=700)
图2
OpenCV 中imwrite函数对数据类型进行自动转换时,是根据如下的方法进行的:
Saturation Arithmetics
As a computer vision library, OpenCV deals a lot with image pixels that are often encoded in a compact, 8- or 16-bit per channel, form and thus have a limited value range. Furthermore, certain operations on images, like color space conversions, brightness/contrast adjustments, sharpening, complex interpolation (bi-cubic, Lanczos) can produce values out of the available range. If you just store the lowest 8 (16) bits of the result, this results in visual artifacts and may affect a further image analysis. To solve this problem, the so-calledsaturation arithmetics is used. For example, to store r, the result of an operation, to an 8-bit image, you find the nearest value within the 0..255 range:
Similar rules are applied to 8-bit signed, 16-bit signed and unsigned types. This semantics is used everywhere in the library. In C++ code, it is done using the saturate_cast<> functions that resemble standard C++ cast operations. See below the implementation of the formula provided above:
wherecv::uchar is an OpenCV 8-bit unsigned integer type. In the optimized SIMD code, such SSE2 instructions aspaddusb,packuswb, and so on are used. They help achieve exactly the same behavior as in C++ code.
Note:
Saturation is not applied when the result is 32-bit integer.