opencv: 基本知识(二);
1、Mat与IplImage之间的相互转换;
//IplImage—>Mat //EXAMPLE: //浅拷贝: IplImage* pBinary=cvLoadImage("c://temp.jpg",0); Mat Img; Img=cvarrToMat(pBinary); //深拷贝只需要再在Mat里创建一个新的Mat对象,然后进行数据的复制,再用上述的函数进行数据头的复制(浅拷贝): IplImage* pBinary=cvLoadImage("c://temp.jpg", 0); Mat ImgTemp; Img=cvarrToMat(pBinary); Mat Img = ImgTemp.clone(); //Mat—>IplImage //EXAMPLE: //浅拷贝: Mat Img=imread("1.jpg"); IplImage* pBinary = &IplImage(Img); //深拷贝只要再加一次复制数据: IplImage *input = cvCloneImage(pBinary);
2、随机数;
标准库提供了随机函数std::srand,std::rand来生成随机数;
std::srand(unsigned(time(NULL)); std::rand(); //生成随机数;
3、矩阵缩放;
opencv的图像缩放函数为cv::resize();
Resizes an image. C++: void resize(InputArray src, OutputArray dst, Size dsize, double fx=0, double fy=0, int interpolation=INTER_LINEAR ) Python: cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]]) → dst C: void cvResize(const CvArr* src, CvArr* dst, int interpolation=CV_INTER_LINEAR ) Python: cv.Resize(src, dst, interpolation=CV_INTER_LINEAR) → None Parameters: src – Source image. dst – Destination image. It has the size dsize (when it is non-zero) or the size computed from src.size() , fx , and fy . The type of dst is the same as of src . dsize – Destination image size. If it is zero, it is computed as: Either dsize or both fx and fy must be non-zero. fx – Scale factor along the horizontal axis. When it is 0, it is computed as fy – Scale factor along the vertical axis. When it is 0, it is computed as interpolation – Interpolation method: INTER_NEAREST - a nearest-neighbor interpolation INTER_LINEAR - a bilinear interpolation (used by default) INTER_AREA - resampling using pixel area relation. It may be a preferred method for image decimation, as it gives moire’-free results. But when the image is zoomed, it is similar to the INTER_NEAREST method. INTER_CUBIC - a bicubic interpolation over 4x4 pixel neighborhood INTER_LANCZOS4 - a Lanczos interpolation over 8x8 pixel neighborhood:
使用方法:
cv::Mat(srcIMg,dstImg,dstImg.Size,0,0,cv::INTER_CUBIC); //fx,fy,将按照dstImg.size与srcImg的比值来进行设置 或者 cv::Mat(srcImg,dstImg,cv::Size(), 0.5,0.5,cv::INTER_CUBIC);