C++中OpenCV应用
生活随笔
收集整理的這篇文章主要介紹了
C++中OpenCV应用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
C++中OpenCV應用
- 圖片處理
- 獲取圖片中的像素值
圖片處理
獲取圖片中的像素值
#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; }程序本身的執行邏輯比較簡單,也比較容易理解,程序中主要的知識點就是數據結構Vec3b
在OpenCV中,使用imread讀取的Mat數據類型統一使用uchar類型的數據存儲,對于RGB三通道的圖像,每個像素點的數據都是用一個Vec3b類型來存放,數據存放順序為B、G、R
Vec3b是OpenCV中的一種數據結構,相當于動態數組Vector,于其類似的還有Vec3f(float)、Vec3d(double)
Vec3b中的數據長度為8Bit,存放數據為RGB彩色圖像,數據范圍為0~255
Vec3b不僅可以用于獲取像素值,也可以通過更改相應像素點的Vec3b信息的方式,修改其數據值
例:
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
總結
以上是生活随笔為你收集整理的C++中OpenCV应用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 亲测可用:Anaconda Windo
- 下一篇: MFC提示 未在此计算机上注册Activ