题目
opencv如何计算图像中物体的像素值
提问时间:2021-02-17
答案
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;
}
其实还有更好的方法,例如将其封装成类,调用起来更加方便,效率也很高.
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的值和该切线方程.
我想写一篇关于奥巴马的演讲的文章,写哪一篇好呢?为什么好
奥巴马演讲不用看稿子.为什么中国领导演讲要看?
想找英语初三上学期的首字母填空练习……
英语翻译
最新试题
- 1求写一遍初二英语书面表达
- 2改变物体内能的两种方式的区别.
- 3keep locked with the chain
- 4热交换一般通过哪三种方式完成.换热器在发电厂中按其用途的 结构分为三类,都是什么换热器
- 5肩负重任的负是什么意思
- 6试述酸中毒或碱中毒与血钾浓度的变化,相互关系
- 7How ___grams of flour will you buy? A How much B How many C How heavy D How big
- 8从一列数字中,用什么函数可以统计大于a,小于b的个数
- 9怎样算公路的面积长120米宽2米厚15公分总共有多少面积?
- 10(6cos^4x-sin^2x-1)/(2(2cos^2x -1))怎么化简
热门考点
- 129.a moment and I will go to your rescue.B
- 2the more careless you are是倒装吗
- 3把诗句每一个字都组成一个词语
- 4换季时一件原价400元的冬装打折出售,售价比原价便宜了100元,问折扣率是多少
- 5高一化学关于摩尔质量的,物质的分子数,原子数,电子数,质子数等 ,还有气体摩尔质量,那些怎么算,公式
- 6浅探小学英语教学中如何渗透音标教学
- 7已知定义在[1,4]上的函数f(x)=x^2-2bx+b/4,b≥1.①求f(x)的最小值g(b);②求g(b)的最大值M.
- 8秦朝通过______(填“中央”或“地方”或“中央和地方”)的行政机构管理人民、征收赋税
- 9post-pregnancy 什么意思?
- 10甲、乙两数的商是8.5,如果甲数扩大10倍,乙数缩小10倍,这时甲、乙两数的商是( ) A.8.5 B.850 C.0.085