1.ImlImage数据结构
typedef struct _IplImage
{
int nSize; /* sizeof(IplImage) */
int ID; /* version (=0)*/
int nChannels; /* Most of OpenCV functions support 1,2,3 or 4 channels */
int alphaChannel; /* Ignored by OpenCV */
int depth; /* Pixel depth in bits: IPL_DEPTH_8U, IPL_DEPTH_8S, IPL_DEPTH_16S,
IPL_DEPTH_32S, IPL_DEPTH_32F and IPL_DEPTH_64F are supported. */
char colorModel[4]; /* Ignored by OpenCV */
char channelSeq[4]; /* ditto */
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 align; /* Alignment of image rows (4 or 8).
OpenCV ignores it and uses widthStep instead. */
int width; /* Image width in pixels. */
int height; /* Image height in pixels. */
struct _IplROI *roi; /* Image ROI. If NULL, the whole image is selected. */
struct _IplImage *maskROI; /* Must be NULL. */
void *imageId; /* " " */
struct _IplTileInfo *tileInfo; /* " " */
int imageSize; /* Image data size in bytes
(==image->height*image->widthStep
in case of interleaved data)*/
char *imageData; /* Pointer to aligned image data. */
int widthStep; /* Size of aligned image row in bytes. */
int BorderMode[4]; /* Ignored by OpenCV. */
int BorderConst[4]; /* Ditto. */
char *imageDataOrigin; /* Pointer to very origin of image data
(not necessarily aligned) -
needed for correct deallocation */
}
IplImage;
2.访问图像数据
HSV图像:H,色调;S:饱和度;I:亮度
eg.最大化HSI图像的S和V部分。
#include"cv.h"
#include"highgui.h"
#include<iostream>
using namespace std;
void saturate_sv( IplImage* img )
{
for( int y=0; y<img->height; y++ )
{
/*注意IplImage与CvMat的区别*/
uchar* ptr = (uchar*) (img->imageData + y * img->widthStep);
for( int x=0; x<img->width; x++ )
{
ptr[3*x+1] = 255;//S
ptr[3*x+2] = 255;//V
}
}
}
int main(int argc,char* argv[])
{
argv[1]="/home/xxx/desktop/lena.jpg";
IplImage* image=cvLoadImage(argv[1]);
saturate_sv(image);
cvNamedWindow("Test",CV_WINDOW_AUTOSIZE);
cvShowImage("Test",image);
cvWaitKey(0);
//...
return 0;
}
3.对ROI和WidthStep的补充
使用他们通常可以提高计算机视觉代码的执行速度,因为他们可以对一小部分区域进行操作,而不是整幅图像。
(1)ROI
void cvSetImageROI(IplImage* image, CvRect rect);//设置ROI
void cvResetImageROI(IplImage* image);//Reset ROI
image:图像指针
rect:矩形大小
eg.
#include <cv.h>
#include <highgui.h>
int main(int argc, char** argv)
{
argv[1]="/home/xxx/desktop/lena.jpg";
IplImage* src=cvLoadImage(argv[1]);
int x = 200;
int y = 200;
int width = 500;
int height = 500;
int add = 150;
/*在src中设置ROI*/
cvSetImageROI(src, cvRect(x,y,width,height));
/*将感兴趣的区域中的B通道的像素加上add*/
cvAddS(src, cvScalar(add),src);
cvResetImageROI(src);//释放区域
cvNamedWindow( "Roi_Add",CV_WINDOW_AUTOSIZE);
cvShowImage( "Roi_Add", src );
cvWaitKey(0);
return 0;
}
(2)使用widthStep来达到以上效果
// Assuming IplImage *interest_img; and
// CvRect interest_rect;
// Use widthStep to get a region of interest
//
// (Alternate method)
//
IplImage *sub_img = cvCreateImageHeader(
cvSize(
interest_rect.width,
interest_rect.height
),
interest_img->depth,
interest_img->nChannels
);
sub_img->origin = interest_img->origin;
sub_img->widthStep = interest_img->widthStep;
sub_img->imageData = interest_img->imageData +
interest_rect.y * interest_img->widthStep +
interest_rect.x * interest_img->nChannels;
cvAddS( sub_img, cvScalar(1), sub_img );
cvReleaseImageHeader(&sub_img);
typedef struct _IplImage
{
int nSize; /* sizeof(IplImage) */
int ID; /* version (=0)*/
int nChannels; /* Most of OpenCV functions support 1,2,3 or 4 channels */
int alphaChannel; /* Ignored by OpenCV */
int depth; /* Pixel depth in bits: IPL_DEPTH_8U, IPL_DEPTH_8S, IPL_DEPTH_16S,
IPL_DEPTH_32S, IPL_DEPTH_32F and IPL_DEPTH_64F are supported. */
char colorModel[4]; /* Ignored by OpenCV */
char channelSeq[4]; /* ditto */
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 align; /* Alignment of image rows (4 or 8).
OpenCV ignores it and uses widthStep instead. */
int width; /* Image width in pixels. */
int height; /* Image height in pixels. */
struct _IplROI *roi; /* Image ROI. If NULL, the whole image is selected. */
struct _IplImage *maskROI; /* Must be NULL. */
void *imageId; /* " " */
struct _IplTileInfo *tileInfo; /* " " */
int imageSize; /* Image data size in bytes
(==image->height*image->widthStep
in case of interleaved data)*/
char *imageData; /* Pointer to aligned image data. */
int widthStep; /* Size of aligned image row in bytes. */
int BorderMode[4]; /* Ignored by OpenCV. */
int BorderConst[4]; /* Ditto. */
char *imageDataOrigin; /* Pointer to very origin of image data
(not necessarily aligned) -
needed for correct deallocation */
}
IplImage;
2.访问图像数据
HSV图像:H,色调;S:饱和度;I:亮度
eg.最大化HSI图像的S和V部分。
#include"cv.h"
#include"highgui.h"
#include<iostream>
using namespace std;
void saturate_sv( IplImage* img )
{
for( int y=0; y<img->height; y++ )
{
/*注意IplImage与CvMat的区别*/
uchar* ptr = (uchar*) (img->imageData + y * img->widthStep);
for( int x=0; x<img->width; x++ )
{
ptr[3*x+1] = 255;//S
ptr[3*x+2] = 255;//V
}
}
}
int main(int argc,char* argv[])
{
argv[1]="/home/xxx/desktop/lena.jpg";
IplImage* image=cvLoadImage(argv[1]);
saturate_sv(image);
cvNamedWindow("Test",CV_WINDOW_AUTOSIZE);
cvShowImage("Test",image);
cvWaitKey(0);
//...
return 0;
}
3.对ROI和WidthStep的补充
使用他们通常可以提高计算机视觉代码的执行速度,因为他们可以对一小部分区域进行操作,而不是整幅图像。
(1)ROI
void cvSetImageROI(IplImage* image, CvRect rect);//设置ROI
void cvResetImageROI(IplImage* image);//Reset ROI
image:图像指针
rect:矩形大小
eg.
#include <cv.h>
#include <highgui.h>
int main(int argc, char** argv)
{
argv[1]="/home/xxx/desktop/lena.jpg";
IplImage* src=cvLoadImage(argv[1]);
int x = 200;
int y = 200;
int width = 500;
int height = 500;
int add = 150;
/*在src中设置ROI*/
cvSetImageROI(src, cvRect(x,y,width,height));
/*将感兴趣的区域中的B通道的像素加上add*/
cvAddS(src, cvScalar(add),src);
cvResetImageROI(src);//释放区域
cvNamedWindow( "Roi_Add",CV_WINDOW_AUTOSIZE);
cvShowImage( "Roi_Add", src );
cvWaitKey(0);
return 0;
}
(2)使用widthStep来达到以上效果
// Assuming IplImage *interest_img; and
// CvRect interest_rect;
// Use widthStep to get a region of interest
//
// (Alternate method)
//
IplImage *sub_img = cvCreateImageHeader(
cvSize(
interest_rect.width,
interest_rect.height
),
interest_img->depth,
interest_img->nChannels
);
sub_img->origin = interest_img->origin;
sub_img->widthStep = interest_img->widthStep;
sub_img->imageData = interest_img->imageData +
interest_rect.y * interest_img->widthStep +
interest_rect.x * interest_img->nChannels;
cvAddS( sub_img, cvScalar(1), sub_img );
cvReleaseImageHeader(&sub_img);