上一篇的程序代码列出了IplImage主要用的变量类型,基本上,有这些信息就够用了,而IplImage实际上有那些变量类型呢?请看以下列表
+号以下代表IplImage可用变量类型,-号以下则代表着OpenCV少用,或甚至不用的变量类型.
再来,是用cvLoadImage()来实例IplImage数据结构图像处理相关的算法.
IplImage数据结构灰度图的实例
- #include <cv.h>
- #include <highgui.h>
- #include <stdio.h>
-
- uchar Blue[640][480];
- uchar Green[640][480];
- uchar Red[640][480];
- uchar Gray[640][480];
-
- int main()
- {
- IplImage *Image1;
- Image1 = cvLoadImage("rain.jpg",1);
- cvNamedWindow("Color",1);
- cvShowImage("Color",Image1);
- cvWaitKey(0);
-
-
- for(int i=0;i<Image1->height;i++)
- {
- for(int j=0;j<Image1->widthStep;j=j+3)
- {
- Blue[i][(int)(j/3)]=Image1->imageData[i*Image1->widthStep+j];
- Green[i][(int)(j/3)]=Image1->imageData[i*Image1->widthStep+j+1];
- Red[i][(int)(j/3)]=Image1->imageData[i*Image1->widthStep+j+2];
- }
- }
-
-
- for(int i=0;i<Image1->height;i++)
- {
- for(int j=0;j<Image1->width;j++)
- {
- Gray[i][j]=(uchar)(0.299*Red[i][j] + 0.587*Green[i][j] + 0.114*Blue[i][j]);
- Red[i][j]=Gray[i][j];
- Green[i][j]=Gray[i][j];
- Blue[i][j]=Gray[i][j];
- }
- }
-
-
- for(int i=0;i<Image1->height;i++)
- {
- for(int j=0;j<Image1->widthStep;j=j+3)
- {
- Image1->imageData[i*Image1->widthStep+j]=Blue[i][(int)(j/3)];
- Image1->imageData[i*Image1->widthStep+j+1]=Green[i][(int)(j/3)];
- Image1->imageData[i*Image1->widthStep+j+2]=Red[i][(int)(j/3)];
- }
- }
-
- cvSaveImage("rain_gray.jpg",Image1);
- cvNamedWindow("Gray Level",1);
- cvShowImage("Gray Level",Image1);
- cvWaitKey(0);
-
- cvReleaseImage(&Image1);
- cvDestroyWindow("Gray Level");
- }
原始图片(大小是:高度640 x 宽度480):
执行结果:
(1)
(2)Gray=(Red+Green+Blue)/3,错误的灰级结果
上面的程序代码分成三部分
1.imageData转RGB(Load Image RGB Values)
2.RGB算法实例(Implement Algorithms)
3.RGB转imageData(Save Image RGB Values)
中间那部分的灰级是用转灰级的算法实例出来的,灰级算法转换并不是Gray=(Red+Green+Blue)/3,因为人的眼睛看到的色域范围并不是红 绿蓝各256的,因为人的眼睛对于能看见绿色的比率比较高,占58%,对人来讲能看见绿色的范围占快60%而相对的红色跟蓝色的能看见的区域就比较少,用 加 起来除以3将会是不正确的结果.
对于一般学术论文,创造新的算法,或是图像处理相关作业算法实例,都可以藉由这种方式来对 IplImage数据结构读取RGB值,就如同中值滤波器,转灰度图,小波转换等算法实例都可以在 /* Implement Algorithms */下面的for循环制作,接着将它转回IplImage数据结构,存成图像文件,时间复杂度也只是O(n2)而已,不会造成浪费太多的时间,而OpenCV可以做到的强大功能是,大部分的图像文件格式都可以打开做转换,经由算法处理做更多的应用.
(fzzl) |