uint8_t图像数据类型介绍
0. C++基礎(chǔ)類型介紹
| 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++的主要數(shù)據(jù)類型,主要分為三類,布爾型、整型(char型從本質(zhì)上說,也是種整型類型,它是長度為1的整數(shù),通常用來存放字符的ASCII碼)、浮點(diǎn)型。而*_t是typedef定義的表示標(biāo)志,是結(jié)構(gòu)的一種標(biāo)注。即我們所看到的uint8_t、uint16_t、uint32_t都不是新的數(shù)據(jù)類型,而是通過typedef給類型起的別名。很明顯的可以看出:uint8_t是用一個(gè)字節(jié)表示的;uint16_t是用兩個(gè)字節(jié)表示的;uint32_t是用4個(gè)字節(jié)表示的。比如:
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.圖像數(shù)據(jù)類型uint8_t
從上面可以得知,uint8_t的定義是unsigned char,數(shù)據(jù)范圍在0~255之間,非常適合于存放圖像數(shù)據(jù)。比如我們通過opencv讀取一幅灰度影像,可以用一個(gè)uint8數(shù)組來保存灰度影像每個(gè)像素的灰度值。
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);} }當(dāng)我們想輸出uint8_t整型值來看時(shí),總是會(huì)得不到我們想看到的整形值。這是因?yàn)?lt;<操作符有一個(gè)重載版本是ostream & operator <<(ostream&, unsigned char),它會(huì)將unsigned char類型的參數(shù)經(jīng)過ASCII碼值轉(zhuǎn)換輸出對(duì)應(yīng)的字符,要是想輸出整形值而非字符,其實(shí)也很簡單,在輸出的時(shí)候?qū)int8_t轉(zhuǎn)換成unsigned int類型就可以了,可以用下面的輸出語句:
std::cout << unsigned(a) << std::endl; //或者 std::cout << +a << std::endl; std::cout << a+0 << std::endl;至于類型的轉(zhuǎn)換,等遇到實(shí)際問題時(shí)再來作補(bǔ)充
總結(jié)
以上是生活随笔為你收集整理的uint8_t图像数据类型介绍的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 云脉H5文档管理为你轻松管理文档档案
- 下一篇: 码绘VS手绘