opencv基础(二)查看图像属性,查看图像像素值
1.查看图像像素值
2. 灰度图像像素值的获取方法
2.1 根据at索引获取像素值
2.2 根据行指针获取像素值
2.3 根据基地址获取
3 彩色图像像素值的获取方法
3.1采用at获取彩色像素
3.2采用行指针ptr获取像素
3.3 采用基地址获取像素
1.图像操作
1.1查看图像信息
2 灰度图像获取像素值
输出结果:
3. 彩色图像获取像素值
输出结果:
4. 程序源码
#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>
//using namespace cv;int main(int argc, char** argv) {
cv::String imgPath = "F:/软件开发/qt开发/08opencv/opencv素材/Image/";
cv::String fileName = "Luffy.png";
//cv::String fileName = "opencvlog.jpg";
//cv::String fileName = "mybaby.png";cv::String pathFile = imgPath + fileName;
cv::Mat src, srcGray;src = cv::imread(pathFile); // 采用默认方式读取图像
if (src.data == NULL) {
printf("图像赌徒失败\n");
return -1;
}
cv::imshow("1. 默认读图", src);srcGray = cv::imread(pathFile, cv::IMREAD_GRAYSCALE);
cv::imshow("2. 转换成灰度读图", srcGray);// 1. 查看图像信息
std::cout << "灰度图像通道数: " << srcGray.channels() <<
" 深度: " << srcGray.depth() << " 类型:" <<srcGray.type() << "elemSize1: " <<srcGray.elemSize1()
<< "elemSize: " << srcGray.elemSize() <<" step1:" <<srcGray.step1() << " total():" << srcGray.total() <<
" size: " << srcGray.size << " flags: " << srcGray.flags << " dims: " << srcGray.dims << " rows,clos " <<
srcGray.rows << "," << srcGray.cols << std::endl;
std::cout << std::endl;
//2. 查看彩色图像信息
std::cout << "彩色图像通道数: " << src.channels() <<
" 深度: " << src.depth() << " 类型:" << src.type() << "elemSize1: " << src.elemSize1()
<< "elemSize: " << src.elemSize() << " step1:" << src.step1() << " total():" << src.total() <<
" size: " << src.size << " flags: " << src.flags << " dims: " << src.dims << " rows,clos " <<
src.rows << "," << src.cols << std::endl;// 3.灰度图像获取指定像素值的方法
// 3.1 根据at索引获取像素值
int row = 100;
int col = 100;
std::cout << " 采用at获取灰度图像的单个像素:" << std::endl;
for (int j = 0; j < srcGray.cols; j++) {
std::cout << (int)srcGray.at<uchar>(row,j) << " ";
}
std::cout << std::endl;
// 3.2 根据行指针获取像素值
uchar* ptr= srcGray.ptr<uchar>(row);
std::cout << " 采用ptr获取灰度图像的单个像素:" << std::endl;
for (int j = 0; j < srcGray.cols; j++) {
std::cout << (int)*(ptr + j) << " ";
}
std::cout << std::endl;
// 3.3 根据起始地址获取
uchar* pts = srcGray.data;
std::cout << " 采用data获取灰度图像的单个像素:" << std::endl;
int rows = srcGray.rows;
int cols = srcGray.cols;
for (int j = 0; j < srcGray.cols; j++) {
std::cout << (int)*(pts + rows*row + j) << " ";
}
std::cout << std::endl;// 4 彩色图像获取方法
// 4.1 采用at获取彩色像素
std::cout << " 采用at获取彩色图像的单个像素:" << std::endl;
cv::Vec3b bgr;
uchar bPix;
uchar gPix;
uchar rPix;
for (int j = 0; j < srcGray.cols/2; j++) {
bgr = (cv::Vec3b)src.at<cv::Vec3b>(row, j); // 根据位置获取每个像素值,
bPix = bgr[0];// 根据像素获取蓝色值
gPix = bgr[1];// 获取色通道像素
rPix = bgr[2]; // 获取红色通道像素
printf("[%d,%d,%d] ", int(bPix),int(gPix),int(rPix));
//std::cout << (cv::Vec3b)src.at<cv::Vec3b>(row, j) << " ";
}
std::cout << std::endl;
//4.2采用行指针ptr获取像素std::cout << " 采用ptr获取灰度图像的单个像素:" << std::endl;
ptr = src.ptr<uchar>(row);
for (int j = 0; j < src.rows/2; j++) {
printf("[%d,%d,%d] ", (int)*(ptr + j*3), (int)*(ptr + j * 3 + 1), (int)*(ptr + j * 3 + 2));
}
std::cout << std::endl;
//4.3 采用基地址获取像素
uchar* baseAddr = src.data;
std::cout << " 采用基地址获取灰度图像的单个像素:" << std::endl;
for (int j = 0; j < src.rows / 2; j++) {printf("[%d,%d,%d] ", (int)*(baseAddr + rows*row + j * 3), (int)*(baseAddr + rows*row + j * 3 + 1),
(int)*(baseAddr + rows*row + j * 3 + 2));
}
std::cout << std::endl;cv::waitKey(0);
system("pause");
return 0;
}