1、创建一个100*100的3通道图像,先把图设置成黑色,再用cvCircle画一个圆,最后显示之。
#include "stdafx.h" #include <cv.h> #include <cxcore.h> #include <highgui.h> #include <stdlib.h> int _tmain(int argc, _TCHAR* argv[]) { //IPlImage * img = cvCreatMat( IplImage * img = cvCreateImage( cvSize(100,100), IPL_DEPTH_8U, 3);//创建一个IplImage对象,尺寸100*100,字节型,3通道 cvZero(img);//将像素值全部置为0 cvCircle(img,cvPoint(50,50), 45, cvScalar(255,255,255,0.5), 1, 8, 0);//画一个圈 cvNamedWindow("show an image", CV_WINDOW_AUTOSIZE); cvShowImage("show an image", img); cvWaitKey(0); cvReleaseImage(&img); cvDestroyWindow("show an image"); return 0; }结果:
2、创建一个100*100的3通道图像,先把图设置成黑色,再通过cvPtr2D将指针指向绿色通道,以(20,5)和(40,20)为定点画一个长方形
#include <cv.h> #include <cxcore.h> #include <highgui.h> #include <stdlib.h> int _tmain(int argc, _TCHAR* argv[]) { IplImage * img = cvCreateImage( cvSize(100,100), IPL_DEPTH_8U, 3);//创建一个IplImage对象 cvZero(img);//将像素值全部置为0 int top = 20,left = 5, bottom = 40, right = 20; for(;top <= bottom;top++)//两条竖线 { *(cvPtr2D(img,top,left)+1)=255;//green *(cvPtr2D(img,top,right)+1)=255;//green } top = 20; for(left = 5;left <= right; left++)//两条横线 { //*(cvPtr2D(img,top,left))=255;//blue *(cvPtr2D(img,top,left)+1)=255;//green //*(cvPtr2D(img,top,left)+2)=255;//red *(cvPtr2D(img,bottom,left)+1)=255;//green } cvNamedWindow("show an image", CV_WINDOW_AUTOSIZE); cvShowImage("show an image", img); cvWaitKey(0); cvReleaseImage(&img); cvDestroyWindow("show an image"); return 0; }
结果:
3、使用ROI和cvSet创建一个金字塔式的图
#include "stdafx.h" #include <cv.h> #include <cxcore.h> #include <highgui.h> int _tmain(int argc, _TCHAR* argv[]) { IplImage * img = cvCreateImage(cvSize(210,210),IPL_DEPTH_8U,1); cvZero(img); int sclar; int add; int img_x, img_y; sclar = 0; add = 0; img_x = 0; img_y = 0; while(add < 200) { //设置感兴趣区域 cvSetImageROI(img,cvRect(img_x, img_y, 210-sclar, 210-sclar)); cvSet(img, cvScalar(add)); img_x = img_x + 10; img_y = img_y + 10; add = add + 20; sclar = sclar + 20; //释放感兴趣区域 cvResetImageROI(img); } //显示图像 cvNamedWindow("Source"); cvShowImage("Source",img); cvWaitKey(0); cvDestroyWindow("Source"); cvReleaseImage(&img); return 0; }
结果:
4、针对一个一幅图像创建两个图像头,分别指向图像中的一个区域,再对两个图像头应用cvNot函数,然后显示图像
#include "stdafx.h" #include <cv.h> #include <cxcore.h> #include <highgui.h> int _tmain(int argc, _TCHAR* argv[]) { IplImage * img = cvLoadImage("lena.jpg"); IplImage * imgHeaderA = cvCreateImageHeader(cvSize(20,30),img->depth, img->nChannels); imgHeaderA->origin = img->origin; imgHeaderA->widthStep = img->widthStep; IplImage * imgHeaderB = cvCreateImageHeader(cvSize(20,30),img->depth, img->nChannels); imgHeaderB->origin = img->origin; imgHeaderB->widthStep = img->widthStep; imgHeaderA->imageData = img->imageData + img->widthStep * 10 + img->nChannels * 5; imgHeaderB->imageData = img->imageData + img->widthStep * 50 + img->nChannels * 60; cvNot(imgHeaderA,imgHeaderA); cvNot(imgHeaderB,imgHeaderB); cvNamedWindow("show an image", CV_WINDOW_AUTOSIZE); cvShowImage("show an image", img); cvWaitKey(0); cvReleaseImage(&img); cvReleaseImageHeader(&imgHeaderA); cvReleaseImageHeader(&imgHeaderB); cvDestroyWindow("show an image"); return 0; }结果: