// 第五题#include#include int main(int argc, char **argv){ int i,j; IplImage *img = cvCreateImage(cvSize(210,210), 8, 1); cvSetZero(img); for(i = 0;i<10; i++) { cvSetImageROI(img, cvRect(i*10, i*10, img->width-2*i*10, img->height-2*i*10)); cvSet(img,cvScalar(i*20),NULL); } cvResetImageROI(img); cvNamedWindow("example_p104_5", 1); cvShowImage("example_p104_5", img); cvWaitKey(0); cvReleaseImage(&img); cvDestroyWindow("example_p104_5");}
// 第六题#include#include int main(int argc, char *argv[]){ IplImage *img, *img1, *img2; char src[] = "F:\\test\\p104_6\\p104_6\\lena.jpg"; img = cvLoadImage(src,1); if( img == NULL ) return 1; img1 = cvCreateImageHeader(cvSize(20,30),img->depth,img->nChannels); img1->origin = img->origin; img1->widthStep = img->widthStep; img1->imageData = (char *)cvPtr2D(img,5,10,NULL); cvNot(img1,img1); img2 = cvCreateImageHeader(cvSize(20,30),img->depth,img->nChannels); img2->origin = img->origin; img2->widthStep = img->widthStep; img2->imageData = (char *)cvPtr2D(img,50,60,NULL); cvNot(img2,img2); cvNamedWindow("p104_6",CV_WINDOW_AUTOSIZE); cvShowImage("p104_6",img); cvWaitKey(0); cvReleaseImage(&img); cvReleaseImage(&img1); cvReleaseImage(&img2); cvDestroyWindow("p104_6");}
。。。啊,我加载的图片太大了,结果的小方块太小了。。。
// 第七题#include#include int main(int argc, char *argv[]){ IplImage *img, *red, *green, *blue; char src[] = "F:\\test\\p104_7\\p104_7\\fruits.jpg"; img =cvLoadImage(src,1); if( img == NULL) return 1; red = cvCreateImage(cvSize(img->width, img->height), img->depth, 1); green = cvCreateImage(cvSize(img->width, img->height), img->depth, 1); blue = cvCreateImage(cvSize(img->width, img->height), img->depth, 1); cvSplit(img, red, green, blue, NULL); cvZero(img); cvMerge(NULL, green, NULL, NULL, img); cvNamedWindow("p104_7", 1); cvShowImage("p104_7", img); cvWaitKey(0); IplImage *clone1 = cvCreateImage(cvGetSize(green), green->depth, green->nChannels); IplImage *clone2 = cvCreateImage(cvGetSize(green), green->depth, green->nChannels); cvCopy(green, clone1, NULL); cvCopy(green, clone2, NULL);// IplImage *clone1 = (IplImage *)cvClone(green);// IplImage *clone2 = (IplImage *)cvClone(green); double min_val = 0.0, max_val = 0.0; cvMinMaxLoc(green, &min_val, &max_val, NULL, NULL, NULL); unsigned char thresh = (unsigned char)((max_val-min_val)/2.0); cvSet(clone1, cvScalar(thresh), NULL); cvSetZero(clone2); cvCmp(green, clone1, clone2, CV_CMP_GE); cvSubS(green, cvScalar(thresh/2), green, clone2); cvShowImage("p104_7", green); cvWaitKey(0); cvReleaseImage(&img); cvReleaseImage(&red); cvReleaseImage(&green); cvReleaseImage(&blue); cvDestroyWindow("p104_7"); return 0;}