uint8_t图像数据类型介绍
生活随笔
收集整理的這篇文章主要介紹了
uint8_t图像数据类型介绍
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
0. C++基礎類型介紹
| bool | 1 | false, true |
| char | 1 | -128 ~ 127 |
| signed char | 1 | -128 ~ 127 |
| unsigned char | 1 | 0 ~ 255 |
| short(signed short) | 2 | -215 ~ 215- 1 |
| unsigned short | 2 | 0 ~ 216- 1 |
| int(signed int) | 4 | -231 ~ 231- 1 |
| unsigned int | 4 | 0 ~ 232 - 1 |
| long(signed long) | 4 | -231 ~ 231 - 1 |
| long long | 8 | -263 ~ 263 - 1 |
| unsigned long | 4 | 0 ~ 232 - 1 |
| float | 4 | -3.4 * 10-38 ~ 3.4 * 1038 |
| double | 8 | -1.79 * 10-308 ~ 1.7 * 10308 |
C++的主要數據類型,主要分為三類,布爾型、整型(char型從本質上說,也是種整型類型,它是長度為1的整數,通常用來存放字符的ASCII碼)、浮點型。而*_t是typedef定義的表示標志,是結構的一種標注。即我們所看到的uint8_t、uint16_t、uint32_t都不是新的數據類型,而是通過typedef給類型起的別名。很明顯的可以看出:uint8_t是用一個字節表示的;uint16_t是用兩個字節表示的;uint32_t是用4個字節表示的。比如:
typedef signed char int8_t; typedef short int int16_t; typedef int int32_t;typedef unsigned char uint8_t; typedef unsigned short int uint16_t; typedef unsigned int uint32_t;1.圖像數據類型uint8_t
從上面可以得知,uint8_t的定義是unsigned char,數據范圍在0~255之間,非常適合于存放圖像數據。比如我們通過opencv讀取一幅灰度影像,可以用一個uint8數組來保存灰度影像每個像素的灰度值。
cv::Mat img = cv::imread(path, cv::IMREAD_GRAYSCALE); const int32_t width = static_cast<uint32_t>(img.cols); const int32_t height = static_cast<uint32_t>(img.rows);uint8_t bytes = new uint8_t[width * height];for(int i = 0; i < height; i++){for(int j = 0; j < width; j++){bytes[i * width + j] = img.at<uint8_t>(i,j);} }當我們想輸出uint8_t整型值來看時,總是會得不到我們想看到的整形值。這是因為<<操作符有一個重載版本是ostream & operator <<(ostream&, unsigned char),它會將unsigned char類型的參數經過ASCII碼值轉換輸出對應的字符,要是想輸出整形值而非字符,其實也很簡單,在輸出的時候將uint8_t轉換成unsigned int類型就可以了,可以用下面的輸出語句:
std::cout << unsigned(a) << std::endl; //或者 std::cout << +a << std::endl; std::cout << a+0 << std::endl;至于類型的轉換,等遇到實際問題時再來作補充
總結
以上是生活随笔為你收集整理的uint8_t图像数据类型介绍的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 云脉H5文档管理为你轻松管理文档档案
- 下一篇: 码绘VS手绘