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;
相关推荐
Q741_1473 小时前
C++ 力扣 76.最小覆盖子串 题解 优选算法 滑动窗口 每日一题
c++·算法·leetcode·双指针·滑动窗口
Incredibuild10 小时前
DevSecOps 集成 CI/CD Pipeline:实用指南
c++·ci/cd·devsecops
爱炸薯条的小朋友11 小时前
C#由Dictionary不正确释放造成的内存泄漏问题与GC代系
开发语言·opencv·c#
君鼎13 小时前
More Effective C++ 条款01:仔细区别 pointers 和 references
c++
肥仔哥哥193014 小时前
基于OpenCv做照片分析(Java)
java·人工智能·opencv·图像原理
工藤新一¹15 小时前
C/C++ 数据结构 —— 树(2)
c语言·数据结构·c++·二叉树··c/c++
源远流长jerry15 小时前
STM32之DMA详解
linux·网络·c++·stm32·单片机·嵌入式硬件
是店小二呀15 小时前
【C++】智能指针底层原理:引用计数与资源管理机制
android·java·c++
FirstFrost --sy16 小时前
map和set的使⽤
c++·set·map
不午睡的探索者17 小时前
FFmpeg + WebRTC:音视频开发的两大核心利器
c++·github·音视频开发