1.5【OpenCV圖像處理】讀寫像素

讀取修改像素image.at<Vec3b>(row, col)[0]

1.5.1 讀寫圖像

imread 可以指定載入為灰度或者RGB圖像

Imwrite 保存圖像文件,類型由擴展名決定

見1.2

1.5.1 讀像素

(1)讀一個GRAY像素點的像素值(CV_8UC1):

Scalar intensity = img.at<uchar>(y, x); 或

Scalar intensity = img.at<uchar>(Point(x, y));

int gray = gray_src.at<uchar>(row, col); //(讀像素)讀GRAY像素點的像素值

(2)讀一個RGB像素點的像素值:

Vec3f intensity = img.at<Vec3f>(y, x)[ ]; Vec3f三通道,是個數組,要加[ ]

float blue = intensity.val[0]; 第一通道的像素

float green = intensity.val[1];

float red = intensity.val[2];

int b = src.at<Vec3b>(row, col)[0]; //(讀像素)讀第一通道像素,Vec3b放了RGB的像素

int g = src.at<Vec3b>(row, col)[1]; //讀第二通道像素

int r = src.at<Vec3b>(row, col)[2]; //讀第三通道像素

1.5.2 修改像素值

(1)灰度圖像修改像素:

img.at<uchar>(y, x) = 128;

gray_src.at<uchar>(row, col) = 255 - gray;//(寫像素)反差像素值n

(2)RGB三通道圖像修改像素:

img.at<Vec3b>(y,x)[0]=128; // blue

img.at<Vec3b>(y,x)[1]=128; // green

img.at<Vec3b>(y,x)[2]=128; // red

dst.at<Vec3b>(row, col)[0] = 255 - b; //(寫像素)反差像素值ndst.at<Vec3b>(row, col)[1] = g;ndst.at<Vec3b>(row, col)[2] = r;n

空白圖像賦值:

img = Scalar(0);

ROI選擇:

Rect r(10, 10, 100, 100);

Mat smallImg = img(r);

最大值最小值取灰色圖像:

gray_src.at<uchar>(row, col) = max(r, max(b, g));//每個像素都取最大值,就得到灰度圖像(較亮)ngray_src.at<uchar>(row, col) = min(r, min(b, g));//每個像素都取最小值,就得到灰度圖像(較暗)n

Vec3b與Vec3F:

Vec3b對應三通道的順序是blue、green、red的uchar類型數據。

Vec3f對應三通道的float類型數據

把CV_8UC1轉換到CV_32F1實現如下:

src.convertTo(dst, CV_32F);


完整程序:

/*1.5 圖像操作-修改像素*/n#include <opencv2/opencv.hpp> //包含頭文件n#include <iostream> nusing namespace cv; //使用cv命名空間nint main(int argc, char** argv) { //argc 表示命令行輸入參數的個數(以空白符分隔),argv中存儲了所有的命令行參數nMat src = imread("E:/OpenCV/testimage/test5.jpg"); //載入圖像nif (src.empty()) { //是不是為空n printf("could not load image...n");nreturn -1;n }n namedWindow("test opencv setup", CV_WINDOW_AUTOSIZE); //創建窗口,自動大小n imshow("test opencv setup", src); //顯示圖像,窗口標題nn/*單通道讀寫像素*/nMat gray_src;n cvtColor(src, gray_src, CV_BGR2GRAY); //轉換為灰色n// namedWindow("gray_src", CV_WINDOW_AUTOSIZE);n// imshow("gray_src", gray_src);nint height = gray_src.rows;nint width = gray_src.cols;nfor (int row = 0; row < height; row++) {nfor (int col = 0; col < width; col++) {nint gray = gray_src.at<uchar>(row, col); //(讀像素)讀GRAY像素點的像素值n gray_src.at<uchar>(row, col) = 255 - gray; //(寫像素)反差像素值n }n }n imshow("grary_invert", gray_src); //在3.x的版本里可以省略namedWindow,它會自動創建n/*三通道讀寫像素*/nMat dst;n dst.create(src.size(), src.type());n height = src.rows;n width = src.cols;nint nc = src.channels();nfor (int row = 0; row < height; row++) {nfor (int col = 0; col < width; col++) {nif (nc == 1) {nint gray = gray_src.at<uchar>(row, col); //(讀像素)讀GRAY像素點的像素值n gray_src.at<uchar>(row, col) = 255 - gray; //(寫像素)反差像素值n }nelse if (nc == 3) {nint b = src.at<Vec3b>(row, col)[0]; //(讀像素)讀第一通道像素,Vec3b放了RGB的像素nint g = src.at<Vec3b>(row, col)[1]; //讀第二通道像素nint r = src.at<Vec3b>(row, col)[2]; //讀第三通道像素n dst.at<Vec3b>(row, col)[0] = 255 - b; //(寫像素)反差像素值n dst.at<Vec3b>(row, col)[1] = g;n dst.at<Vec3b>(row, col)[2] = r;nn gray_src.at<uchar>(row, col) = max(r, max(b, g));//每個像素都取最大值,就得到灰度圖像(較亮)n// gray_src.at<uchar>(row, col) = min(r, min(b, g)); //每個像素都取最小值,就得到灰度圖像(較暗)n }n }n }n// bitwise_not(src, dst); //位操作反差,所有通道像素全反差n imshow("grary_src_max", gray_src); //在3.x的版本里可以省略namedWindow,它會自動創建n imshow("dst_invert", dst); //在3.x的版本里可以省略namedWindow,它會自動創建n waitKey(0); //等待任意按鍵按下nreturn 0;n}n

運行結果:


推薦閱讀:

TAG:OpenCV | 学习OpenCV书籍 | OpenCV3计算机视觉书籍 |