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;
相关推荐
WSSWWWSSW33 分钟前
Jupyter Notebook 中高效处理和实时展示来自 OpenCV 和 Pillow 的图像数据探究
opencv·jupyter·pillow
R-G-B2 小时前
【08】C++实战篇——C++ 生成动态库.dll 及 C++调用DLL,及实际项目中的使用技巧
c++·c++ 生成动态库.dll·c++ 生成静态库.lib·c++调用动态库.dll·c++调用静态库.lib·c++调用dll·c++调用lib
朝朝又沐沐3 小时前
算法竞赛阶段二-数据结构(40)数据结构栈的STL
开发语言·数据结构·c++·算法
Tony沈哲4 小时前
LLM + 图像处理的第一步:用自然语言驱动调色逻辑
opencv·llm
Antonio9154 小时前
【网络编程】WebSocket 实现简易Web多人聊天室
前端·网络·c++·websocket
kv18304 小时前
opencv解迷宫
人工智能·opencv·计算机视觉·广度优先搜索·图算法
bright_colo5 小时前
Python-初学openCV——图像预处理(六)
人工智能·opencv·计算机视觉
清朝牢弟5 小时前
Ubuntu系统VScode实现opencv(c++)图像放缩与插值
c++·vscode·opencv·ubuntu·计算机视觉
呆瑜nuage5 小时前
list的使用和模拟
c++·list