原文地址:Opencv 获取每个像素值
作者:火星_宅男
话不多说,新手在不断学习,刚刚看见读取每个像素点值很是好奇。
#include <highgui.h>
#include <iostream>
using namespace std;
int main()
{
IplImage* src = cvLoadImage("e:/11.bmp",CV_LOAD_IMAGE_COLOR);
//方法一:
CvScalar s = cvGet2D(src,200,201);//获取像素点为(201,200)点的BGR的值
//注:此处的x,y值是反的,具体不解释
cout<<s.val[0]<<'t'<<s.val[1]<<'t'<<s.val[2]<<endl;//输出B值,G值,R值
//方法二:此处输出的时候进行强制类型转换就可以得到和上面一样的值。不能直接输出uchar?
//坐标(200,201)的像素值
//坐标(200,201)的像素值
cout<<(int)(
(uchar*)(src->imageData+200*src->widthStep))[3*201]<<endl;
cout<<(int)((uchar*)(src->imageData+200*src->widthStep))[3*201+1]<<endl;
cout<<(int)((uchar*)(src->imageData+200*src->widthStep))[3*201+1]<<endl;
cvReleaseImage(&src);
return 0;
}
晚安!