opencv如何计算图像中物体的像素值
题目
opencv如何计算图像中物体的像素值
答案
OpenCV中获取图像某一像素值
This is a basic example for the OpenCV.
First we must know the structure of IplImage:
IPL image:
IplImage
|-- int nChannels; // Number of color channels (1,2,3,4)
|-- int depth; // Pixel depth in bits:
| // IPL_DEPTH_8U,IPL_DEPTH_8S,
| // IPL_DEPTH_16U,IPL_DEPTH_16S,
| // IPL_DEPTH_32S,IPL_DEPTH_32F,
| // IPL_DEPTH_64F
|-- int width; // image width in pixels
|-- int height; // image height in pixels
|-- char* imageData; // pointer to aligned image data
| // Note that color images are stored in BGR order
|-- int dataOrder; // 0 - interleaved color channels,
| // 1 - separate color channels
| // cvCreateImage can only create interleaved images
|-- int origin; // 0 - top-left origin,
| // 1 - bottom-left origin (Windows bitmaps style)
|-- int widthStep; // size of aligned image row in bytes
|-- int imageSize; // image data size in bytes = height*widthStep
|-- struct _IplROI *roi;// image ROI.when not NULL specifies image
| // region to be processed.
|-- char *imageDataOrigin; // pointer to the unaligned origin of image data
| // (needed for correct image deallocation)
|
|-- int align; // Alignment of image rows:4 or 8 byte alignment
| // OpenCV ignores this and uses widthStep instead
|-- char colorModel[4]; // Color model - ignored by OpenCV
//------------------------------------------------------------------------------int main(int argc,char* argv[])
...{
IplImage *img=cvLoadImage("c://fruitfs.bmp",1);
CvScalar s;
for(int i=0;iheight;i++)...{
for(int j=0;jwidth;j++)...{
s=cvGet2D(img,i,j); // get the (i,j) pixel value
printf("B=%f,G=%f,R=%f ",s.val[0],s.val[1],s.val[2]);
s.val[0]=111;
s.val[1]=111;
s.val[2]=111;
cvSet2D(img,i,j,s);//set the (i,j) pixel value
}
}
cvNamedWindow("Image",1);
cvShowImage("Image",img);
cvWaitKey(0); //等待按键
cvDestroyWindow( "Image" );//销毁窗口
cvReleaseImage( &img ); //释放图像
return 0;
}
其实还有更好的方法,例如将其封装成类,调用起来更加方便,效率也很高.
举一反三
已知函数f(x)=x,g(x)=alnx,a∈R.若曲线y=f(x)与曲线y=g(x)相交,且在交点处有相同的切线,求a的值和该切线方程.
我想写一篇关于奥巴马的演讲的文章,写哪一篇好呢?为什么好
最新试题
热门考点
- 语文挑错别字
- 快来人...
- Tom doesn it have a tennis racket改为肯定句
- 如图,双曲线y=-2/x和y=-8/x在第二象限中的图象,点A在y=-8/x的图像上,点A的横坐标为m(m<0),AC//y轴交y=-2/x的图像于点C,AB、DC均平行于x轴,分别交y=-2/x,y
- 某小区准备造一个容积108立方米的无盖长方体水池,怎样设计尺寸才使表面积最小?
- 一个数的1.5倍是3.1与0.5的和的6倍
- 请教一下问别人来自哪里的英文句子怎么写
- 一辆汽车在与铁路平行的公路上行驶,追赶一列长320cm的列车,已知汽车的速度是54km/h,火车的速度是36km/h
- 把以下的谚语转化成成语
- what events为什么有s而what year没s