opencv依据图像类型读取图像像素点

Mat数据类型和通道对应的type():

库类型 C1 C2 C3 C4
CV_8U 0 8 16 24
CV_8S 1 9 17 25
CV_16U 2 10 18 26
CV_16S 3 11 19 27
CV_32S 4 12 20 28
CV_32F 5 13 21 29
CV_64F 6 14 22 30

通过c++程序查看类型并读取图像像素点:

cpp 复制代码
switch (im->type())
		{
		case 0:
			std::cout << "at (" << x << ", " << y << " ) value is: " << static_cast<int>(im->at<uchar>(cv::Point(x, y))) << std::endl; break;
		case 1:
			std::cout << "at (" << x << ", " << y << " ) value is: " << static_cast<int>(im->at<char>(cv::Point(x, y))) << std::endl; break;
		case 2:
			std::cout << "at (" << x << ", " << y << " ) value is: " << static_cast<int>(im->at<ushort>(cv::Point(x, y))) << std::endl; break;
		case 3:
			std::cout << "at (" << x << ", " << y << " ) value is: " << static_cast<int>(im->at<short>(cv::Point(x, y))) << std::endl; break;
		case 4:
			std::cout << "at (" << x << ", " << y << " ) value is: " << static_cast<int>(im->at<int>(cv::Point(x, y))) << std::endl; break;
		case 5:
			std::cout << "at (" << x << ", " << y << " ) value is: " << static_cast<int>(im->at<float>(cv::Point(x, y))) << std::endl; break;
		case 6:
			std::cout << "at (" << x << ", " << y << " ) value is: " << static_cast<int>(im->at<double>(cv::Point(x, y))) << std::endl; break;
		case 16:
			std::cout << "at (" << x << ", " << y << " ) value is: " << static_cast<int>(im->ptr<uchar>(y)[x * 3]) << "," << static_cast<int>(im->ptr<uchar>(y)[x * 3 + 1]) << "," << static_cast<int>(im->ptr<uchar>(y)[x * 3 + 2]) << std::endl; break;
		case 17:
			std::cout << "at (" << x << ", " << y << " ) value is: " << static_cast<int>(im->ptr<char>(y)[x * 3]) << "," << static_cast<int>(im->ptr<char>(y)[x * 3 + 1]) << "," << static_cast<int>(im->ptr<char>(y)[x * 3 + 2]) << std::endl; break;
		case 18:
			std::cout << "at (" << x << ", " << y << " ) value is: " << static_cast<int>(im->ptr<ushort>(y)[x * 3]) << "," << static_cast<int>(im->ptr<ushort>(y)[x * 3 + 1]) << "," << static_cast<int>(im->ptr<ushort>(y)[x * 3 + 2]) << std::endl; break;
		case 19:
			std::cout << "at (" << x << ", " << y << " ) value is: " << static_cast<int>(im->ptr<short>(y)[x * 3]) << "," << static_cast<int>(im->ptr<short>(y)[x * 3 + 1]) << "," << static_cast<int>(im->ptr<short>(y)[x * 3 + 2]) << std::endl; break;
		case 20:
			std::cout << "at (" << x << ", " << y << " ) value is: " << static_cast<int>(im->ptr<int>(y)[x * 3]) << "," << static_cast<int>(im->ptr<int>(y)[x * 3 + 1]) << "," << static_cast<int>(im->ptr<int>(y)[x * 3 + 2]) << std::endl; break;
		case 21:
			std::cout << "at (" << x << ", " << y << " ) value is: " << static_cast<int>(im->ptr<float>(y)[x * 3]) << "," << static_cast<int>(im->ptr<float>(y)[x * 3 + 1]) << "," << static_cast<int>(im->ptr<float>(y)[x * 3 + 2]) << std::endl; break;
		case 22:
			std::cout << "at (" << x << ", " << y << " ) value is: " << static_cast<int>(im->ptr<double>(y)[x * 3]) << "," << static_cast<int>(im->ptr<double>(y)[x * 3 + 1]) << "," << static_cast<int>(im->ptr<double>(y)[x * 3 + 2]) << std::endl; break;
		}
		break;
相关推荐
三体世界3 小时前
HTTPS加密原理
linux·开发语言·网络·c++·网络协议·http·https
杜子不疼.3 小时前
结构体的嵌套问题
c语言·c++
小怡同学..4 小时前
c++系列之智能指针的使用
开发语言·c++
only-lucky4 小时前
vtk和opencv和opengl直接的区别是什么?
人工智能·opencv·计算机视觉
jndingxin5 小时前
OpenCV CUDA模块设备层-----GPU上执行线程安全的 “原子取最大值” 操作函数
人工智能·opencv·计算机视觉
mxpan6 小时前
C++ 单例模式一种实现方式
c++·设计模式
whoarethenext7 小时前
使用 C++/OpenCV 计算图像特征并用 Faiss 进行相似细节搜索
c++·opencv·faiss
only-lucky7 小时前
C++设计模式
java·c++·设计模式
范纹杉想快点毕业7 小时前
Qt构造函数详解:布局与快捷键实战
c语言·开发语言·数据库·c++·qt·命令模式