opencv图像的读入、灰度化、二值化、反色

平台 wen7+vs2010+opencv2.4.4


#include  
#include
using namespace std;
using namespace cv;

void main(int argc, char* argv[])

{
printf("hello word!\n");


IplImage *src;
src = cvLoadImage("D:\\文件夹\\image.jpg"); //这里将lena.jpg和lena.cpp文件放在同一个文件夹下
cvNamedWindow("src",CV_WINDOW_AUTOSIZE);
cvMoveWindow("src",0,50);
cvShowImage("src",src);


//图像灰度化  
    IplImage* gray_image = cvCreateImage(cvGetSize(src),8,1);  
    cvCvtColor(src,gray_image,CV_BGR2GRAY);
cvNamedWindow("gray",CV_WINDOW_AUTOSIZE);
cvMoveWindow("gray",100,100);
cvShowImage("gray",gray_image);


//-----------------------------------------二值图像-----------------------------------------//


//图像二值化
IplImage*bw_image = cvCreateImage(cvGetSize(gray_image),IPL_DEPTH_8U, 1);
double max_val = 255; //阈值化后的最大值
cvThreshold(gray_image, bw_image, 0, max_val , CV_THRESH_OTSU); //调用OTSU算法的参数设置
cvNamedWindow("bw",CV_WINDOW_AUTOSIZE);
cvMoveWindow("bw",200,150);
cvShowImage("bw",bw_image);


//-----------------------------------------反色图像-----------------------------------------//


//二值图像反色
int ivs_height ,ivs_width ,ivs_step ,ivs_channel ;
int ivs_i,ivs_j,ivs_k;
uchar*data ;
uchar*ivs_data ;
IplImage*ivs_image = cvCreateImage(cvGetSize(gray_image),IPL_DEPTH_8U, 1);

ivs_height    = bw_image->height;
ivs_width     = bw_image->width;
ivs_step      = bw_image->widthStep/sizeof(uchar);
ivs_channel = bw_image->nChannels;
data   = (uchar*)bw_image->imageData;
printf("Processing a %d X %d image with %d channel!\n",ivs_height,ivs_width,ivs_channel);


ivs_data = (uchar*)ivs_image->imageData;


// 反色图像
for(ivs_i=0;ivs_i {
            for(ivs_j=0;ivs_j {
                for(ivs_k=0;ivs_k {
                    ivs_data[ivs_i*ivs_step+ivs_j*ivs_channel+ivs_k]=255-data[ivs_i*ivs_step+ivs_j*ivs_channel+ivs_k];


}//k
}//j
}//i




//显示图像
cvNamedWindow("ivs", CV_WINDOW_AUTOSIZE);//创建窗口
cvMoveWindow("ivs",300,200);//窗口位置
cvShowImage( "ivs",ivs_image);//显示图像



cvWaitKey(0);
cvDestroyAllWindows();
// cvReleaseImage(&src);
// cvReleaseImage(&gray_image);
// cvReleaseImage(&bw_image);
// cvReleaseImage(&ivs_image);


}


注意:若是vc6.0+opencv1.0的平台,只需修改头文件,以上代码是本人,从vc6.0+opencv1.0平台,转移到vs2010+opencv2.4.4平台的,都已运行成功

你可能感兴趣的:(opencv)