C++中OpenCV应用
C++中OpenCV應(yīng)用
- 圖片處理
- 獲取圖片中的像素值
圖片處理
獲取圖片中的像素值
#include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace std;int main() {Mat img = imread("/home/wang/opencv/project/2/1.jpg");Mat img_gray;cvtColor(img,img_gray,COLOR_BGR2GRAY);int x,y;int row,col;row = img.rows;col = img.cols;Vec3b intensity;uchar blue,green,red;for(int i = 0; i < col; i++){for(int j = 0; j < row; j++){x = i;y = j;intensity = img.at<cv::Vec3b>(y,x);blue = intensity[0];green = intensity[1];red = intensity[2];cout << "The point(" << x << "," << y <<") rgb_val: "<< (unsigned int)blue << "," << (unsigned int)green << "," << (unsigned int)red << endl;cout << "Gray pixel there is:" << (unsigned int)img_gray.at<uchar>(y,x) << endl;}}return 0; }程序本身的執(zhí)行邏輯比較簡單,也比較容易理解,程序中主要的知識(shí)點(diǎn)就是數(shù)據(jù)結(jié)構(gòu)Vec3b
在OpenCV中,使用imread讀取的Mat數(shù)據(jù)類型統(tǒng)一使用uchar類型的數(shù)據(jù)存儲(chǔ),對(duì)于RGB三通道的圖像,每個(gè)像素點(diǎn)的數(shù)據(jù)都是用一個(gè)Vec3b類型來存放,數(shù)據(jù)存放順序?yàn)锽、G、R
Vec3b是OpenCV中的一種數(shù)據(jù)結(jié)構(gòu),相當(dāng)于動(dòng)態(tài)數(shù)組Vector,于其類似的還有Vec3f(float)、Vec3d(double)
Vec3b中的數(shù)據(jù)長度為8Bit,存放數(shù)據(jù)為RGB彩色圖像,數(shù)據(jù)范圍為0~255
Vec3b不僅可以用于獲取像素值,也可以通過更改相應(yīng)像素點(diǎn)的Vec3b信息的方式,修改其數(shù)據(jù)值
例:
img.at< cv:: Vec3b >(x,y) [0]= 255;//B
img.at< cv:: Vec3b >(x,y) [1]= 255;//G
img.at< cv:: Vec3b >(x,y) [2]= 255;//R
總結(jié)
以上是生活随笔為你收集整理的C++中OpenCV应用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 亲测可用:Anaconda Windo
- 下一篇: C++:常用数据类型及常见操作