opencv得到一副图像的灰度值问题

怎么用opencv得到一副灰度图像的灰度值啊,很着急,很菜,求高手指教。#include"cv.h"
#include"highgui.h"
#include <iostream>
using namespace std;

int main(int argc, char** argv)
{
IplImage* src = cvLoadImage( "0.bmp", 0 ); //导入图片
int width=src->width;//图片宽度
int height = src->height;//图片高度

for (size_t row=0;row<height;row++)
{
uchar* ptr = (uchar*)src->imageData+row*src->width;//获得灰度值数据指针
for (size_t cols=0;cols<width;cols++)
{
int intensity=ptr[cols];
cout<<intensity<<" ";
}
}

return 0;
}
这个代码总是说cout<<intensity<<" ";这里有问题。分不多,求帮帮。

#include "stdafx.h"
#include <iostream>
using namespace std;
#include <cv.h>
#include <highgui.h>
#include <cxcore.h>
int _tmain(int argc, char** argv)
{
 IplImage* src = cvLoadImage("1.png",0);//导入图片
 int width = src->width;//图片宽度
 int height = src->height;//图片高度
 
 for(int row=0;row<height;row++)
 {
  uchar* ptr = (uchar*)src->imageData+row*src->width;//获得灰度值数据指针
  for(int cols=0;cols<width;cols++)
  {
   uchar intensity=ptr[cols];//数据类型
   cout<<(int)intensity<<"  ";//强制转换
  }
 }
 return 0;  
}

注释部分修改一下就可以了,测试结果:

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-07-29
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

#include "stdafx.h"
#include <iostream>
using namespace std;
#include <cv.h>
#include <highgui.h>
#include <cxcore.h>
int _tmain(int argc, char** argv)
{
IplImage* src = cvLoadImage("1.png",0);//导入图片
int width = src->width;//图片宽度
int height = src->height;//图片高度

for(int row=0;row<height;row++)
{
uchar* ptr = (uchar*)src->imageData+row*src->width;//获得灰度值数据指针
for(int cols=0;cols<width;cols++)
{
uchar intensity=ptr[cols];//数据类型
cout<<(int)intensity<<" ";//强制转换
}
}
return 0;
}

注释部分修改一下就可以了,测试结果:
第2个回答  2013-11-16
h;//获得灰度值数据指针
相似回答