void myGUI1a() { cvNamedWindow("Video"); CvCapture* capture = cvCreateFileCapture("盲探.rm"); IplImage* in_frame; if(capture!=NULL) { in_frame = cvQueryFrame(capture); IplImage* out_frame = cvCreateImage(cvGetSize(in_frame), in_frame->depth, 1); IplImage* out_frame2 = cvCreateImage(cvGetSize(in_frame), in_frame->depth, 1); while(true) { in_frame = cvQueryFrame(capture); cvCvtColor(in_frame, out_frame, CV_RGB2GRAY); cvCanny(out_frame, out_frame2, 10, 100, 3); cvShowImage("Video", out_frame2); if(cvWaitKey(33) == 27) break; } } } void myGUI1b() { cvNamedWindow("Video", CV_WINDOW_NORMAL); CvCapture* capture = cvCreateFileCapture("盲探.rm"); IplImage* in_frame; if(capture!=NULL) { in_frame = cvQueryFrame(capture); IplImage* gray = cvCreateImage(cvGetSize(in_frame), in_frame->depth, 1); IplImage* imgGray = cvCreateImage(cvGetSize(in_frame), in_frame->depth, 3); IplImage* canny = cvCreateImage(cvGetSize(in_frame), in_frame->depth, 1); IplImage* imgCanny = cvCreateImage(cvGetSize(in_frame), in_frame->depth, 3); IplImage* all_frame = cvCreateImage(cvSize(3*in_frame->width, in_frame->height), in_frame->depth, in_frame->nChannels); cvZero(all_frame); IplImage* img1 = cvCreateImageHeader(cvGetSize(in_frame), in_frame->depth, 3); IplImage* img2 = cvCreateImageHeader(cvGetSize(in_frame), in_frame->depth, 3); IplImage* img3 = cvCreateImageHeader(cvGetSize(in_frame), in_frame->depth, 3); /*这些在声明的时候已经设置,origin默认 img1->origin = in_frame->origin; img1->depth = in_frame->depth; img1->nChannels = 3; img2->origin = in_frame->origin; img2->depth = in_frame->depth; img2->nChannels = 3; img3->origin = in_frame->origin; img3->depth = in_frame->depth; img3->nChannels = 3; */ img1->widthStep = img2->widthStep = img3->widthStep = all_frame->widthStep; //注意不要漏了这个 while(true) { in_frame = cvQueryFrame(capture); if(in_frame == NULL) break; cvCvtColor(in_frame, gray, CV_RGB2GRAY); cvCvtColor(gray, imgGray, CV_GRAY2BGR); cvCanny(gray, canny, 10, 100); cvCvtColor(canny, imgCanny, CV_GRAY2BGR); //set position of imageData img1->imageData = all_frame->imageData; cvCopy(in_frame, img1); img2->imageData = all_frame->imageData + in_frame->widthStep; cvCopy(imgGray, img2); //cvCopy(gray, img2); img3->imageData = all_frame->imageData + 2*in_frame->widthStep; cvCopy(imgCanny, img3); //cvCopy(canny, img3); CvFont textfont = cvFont(10.0, 1); cvInitFont(&textfont, CV_FONT_HERSHEY_SIMPLEX, 0.5f, 0.5f, 0, 1);; cvPutText(all_frame, "Source Image", cvPoint(10, 20), &textfont, cvScalar(0, 0, 255)); cvPutText(all_frame, "Gray Image", cvPoint(in_frame->width + 10, 20), &textfont, cvScalar(255, 0, 0)); cvPutText(all_frame, "Canny Image", cvPoint(2*in_frame->width + 10, 20), &textfont, cvScalar(0, 255, 0)); cvShowImage("Video", all_frame); if(cvWaitKey(33) == 27) break; } } }
《学习OpenCV(中文版)》第4章 练习1
最新推荐文章于 2024-06-04 19:23:33 发布