#include <cv.h>
#include <highgui.h>
#include <cxcore.h>
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <string>
#define Im(ROW,COL) ((float *)(im->data.fl + im->step/sizeof(float) *(ROW)))[(COL)]
#define Imnew(ROW,COL) ((float *)(imnew->data.fl + imnew->step/sizeof(float) *(ROW)))[(COL)]
//下采样原来的图像,返回缩小2倍尺寸的图像
CvMat * halfSizeImage(CvMat * im)
{
unsigned int i,j;
int w = im->cols/2;
int h = im->rows/2;
CvMat *imnew = cvCreateMat(h, w, CV_8UC1);
//由于IplImage是4通道的step,所有最好保持im输入图像和imnew的step一致。
imnew->step = im->step/2;
printf("\n1、halfsize后的步长:%d",imnew->step);printf("halfsize前的步长:%d\n",im->step);
printf("2、halfsize后的步长:%d",imnew->step);printf("halfsize前的步长:%d\n",im->step);
for ( j = 0; j < h; j++)
for ( i = 0; i < w; i++)
Imnew(j,i)=Im(j*2, i*2);
return imnew;
}
int main( int argc, char* argv[] ) {
IplImage* src = NULL;
IplImage* gray_src = NULL;
CvMat* src_mat;
IplImage* halfsize_src = NULL;
CvMat* halfsize_src_mat;
//读取图片
if((src=cvLoadImage("1.bmp",1)) ==0 )
return -1;
cvNamedWindow("source image",1);
cvNamedWindow("halfSizeImage",1);
cvNamedWindow("src_mat_src",1);
cvShowImage("source image",src);
//转化成单通道图像再处理
gray_src = cvCreateImage(cvSize(src->width,src->height),IPL_DEPTH_8U,1);
cvCvtColor(src,gray_src,CV_BGR2GRAY);
cvNamedWindow("source gray image",1);
cvShowImage("source gray image",gray_src);
//为图像阵列分配内存,假设两幅图像的大小相同,tempMat跟随image1的大小
//
src_mat = cvCreateMat(src->height,src->width,CV_8UC1 );
//
//转换进入Mat数据结构,图像操作使用的是浮点型操作
cvConvert(gray_src,src_mat);
src_mat->step = gray_src->widthStep;
printf("src_mat size:[%d %d]\n",src_mat->cols,src_mat->rows);
//将图像数据还原成图像8位无符号单通道
IplImage* src_mat_src = cvCreateImage(cvGetSize(src),8,1);
cvGetImage(src_mat,src_mat_src);
src_mat_src->widthStep = src->width;//显示时候更改好
printf("src_mat_src %d\n",src_mat_src->widthStep);
cvShowImage("src_mat_src",src_mat_src);
printf("%d %d\n",src_mat->step,gray_src->widthStep);
halfsize_src = cvCreateImage(cvSize(src->width/2,src->height/2),8,1);
halfsize_src_mat = cvCreateMat(src->height/2,src->width/2,CV_8UC1 );
halfsize_src_mat->step = gray_src->widthStep/2;
printf("%d %d",halfsize_src_mat->step,gray_src->widthStep);
halfsize_src_mat = halfSizeImage(src_mat);
printf("size:[%d %d]\n",halfsize_src_mat->width,halfsize_src_mat->height);
// halfsize_src_mat->step=halfsize_src->widthStep;
printf("size:[%d %d]\n",halfsize_src->width,halfsize_src->height);
cvGetImage(halfsize_src_mat,halfsize_src);
halfsize_src->widthStep = gray_src->widthStep/2;
printf("%d \n",halfsize_src->widthStep);
cvShowImage("halfSizeImage",halfsize_src);
printf("%d \n",halfsize_src->widthStep);
// cv::namedWindow("halfSizeImage", 1);
// cv::imshow("halfSizeImage",temp1);
cvWaitKey(0);
cvDestroyWindow("source image");
cvDestroyWindow("halfSizeImage");
cvReleaseImage( &src );
// cvReleaseImage( &src );
}
#include <highgui.h>
#include <cxcore.h>
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <string>
#define Im(ROW,COL) ((float *)(im->data.fl + im->step/sizeof(float) *(ROW)))[(COL)]
#define Imnew(ROW,COL) ((float *)(imnew->data.fl + imnew->step/sizeof(float) *(ROW)))[(COL)]
//下采样原来的图像,返回缩小2倍尺寸的图像
CvMat * halfSizeImage(CvMat * im)
{
unsigned int i,j;
int w = im->cols/2;
int h = im->rows/2;
CvMat *imnew = cvCreateMat(h, w, CV_8UC1);
//由于IplImage是4通道的step,所有最好保持im输入图像和imnew的step一致。
imnew->step = im->step/2;
printf("\n1、halfsize后的步长:%d",imnew->step);printf("halfsize前的步长:%d\n",im->step);
printf("2、halfsize后的步长:%d",imnew->step);printf("halfsize前的步长:%d\n",im->step);
for ( j = 0; j < h; j++)
for ( i = 0; i < w; i++)
Imnew(j,i)=Im(j*2, i*2);
return imnew;
}
int main( int argc, char* argv[] ) {
IplImage* src = NULL;
IplImage* gray_src = NULL;
CvMat* src_mat;
IplImage* halfsize_src = NULL;
CvMat* halfsize_src_mat;
//读取图片
if((src=cvLoadImage("1.bmp",1)) ==0 )
return -1;
cvNamedWindow("source image",1);
cvNamedWindow("halfSizeImage",1);
cvNamedWindow("src_mat_src",1);
cvShowImage("source image",src);
//转化成单通道图像再处理
gray_src = cvCreateImage(cvSize(src->width,src->height),IPL_DEPTH_8U,1);
cvCvtColor(src,gray_src,CV_BGR2GRAY);
cvNamedWindow("source gray image",1);
cvShowImage("source gray image",gray_src);
//为图像阵列分配内存,假设两幅图像的大小相同,tempMat跟随image1的大小
//
src_mat = cvCreateMat(src->height,src->width,CV_8UC1 );
//
//转换进入Mat数据结构,图像操作使用的是浮点型操作
cvConvert(gray_src,src_mat);
src_mat->step = gray_src->widthStep;
printf("src_mat size:[%d %d]\n",src_mat->cols,src_mat->rows);
//将图像数据还原成图像8位无符号单通道
IplImage* src_mat_src = cvCreateImage(cvGetSize(src),8,1);
cvGetImage(src_mat,src_mat_src);
src_mat_src->widthStep = src->width;//显示时候更改好
printf("src_mat_src %d\n",src_mat_src->widthStep);
cvShowImage("src_mat_src",src_mat_src);
printf("%d %d\n",src_mat->step,gray_src->widthStep);
halfsize_src = cvCreateImage(cvSize(src->width/2,src->height/2),8,1);
halfsize_src_mat = cvCreateMat(src->height/2,src->width/2,CV_8UC1 );
halfsize_src_mat->step = gray_src->widthStep/2;
printf("%d %d",halfsize_src_mat->step,gray_src->widthStep);
halfsize_src_mat = halfSizeImage(src_mat);
printf("size:[%d %d]\n",halfsize_src_mat->width,halfsize_src_mat->height);
// halfsize_src_mat->step=halfsize_src->widthStep;
printf("size:[%d %d]\n",halfsize_src->width,halfsize_src->height);
cvGetImage(halfsize_src_mat,halfsize_src);
halfsize_src->widthStep = gray_src->widthStep/2;
printf("%d \n",halfsize_src->widthStep);
cvShowImage("halfSizeImage",halfsize_src);
printf("%d \n",halfsize_src->widthStep);
// cv::namedWindow("halfSizeImage", 1);
// cv::imshow("halfSizeImage",temp1);
cvWaitKey(0);
cvDestroyWindow("source image");
cvDestroyWindow("halfSizeImage");
cvReleaseImage( &src );
// cvReleaseImage( &src );
}