【练习5.5】图像求差的绝对值、 开运算

 

提纲
题目要求
程序代码
结果图片

 

 

 

  

题目要求:

 拍一张某场景的图片,然后相机不动,在此场景中心位置放一个鼠标,再拍一张图片,将其载入电脑并转换为8位灰度图像

a、求其差的绝对值并显示结果,他应该是一个带有噪声的鼠标的掩码,

b、对结果图像进行二值化操作,剔除噪声的同时并保留鼠标,超过阈值的像素应设为255

c、在图像上进行CV_MOP_OPEN操作,进一步清除噪声

 

程序代码:

  1 // OpenCVExerciseTesting.cpp : 定义控制台应用程序的入口点。
  2 //
  3 //D:\\Work\\Work_Programming\\Source\\Image\\lena.jpg
  4 
  5 
  6 #include "stdafx.h"
  7 #include <cv.h>
  8 #include <highgui.h>
  9 #include <iostream>
 10 using namespace cv;
 11 using namespace std;
 12 //函数声明-->--->-->--->-->--->-->--->//
 13 
 14  
 15 
 16 //<--<--<--<--<--<--<--<--<--函数声明//
 17 
 18 int _tmain(int argc, _TCHAR* argv[])
 19 {
 20     const char * fileName1 = "D:\\Work\\Work_Programming\\Source\\Image\\临时\\场景1.jpg";
 21     const char * fileName2 = "D:\\Work\\Work_Programming\\Source\\Image\\临时\\场景2.jpg";
 22     IplImage * src1 = cvLoadImage(fileName1, CV_LOAD_IMAGE_GRAYSCALE);
 23     IplImage * src2 = cvLoadImage(fileName2, CV_LOAD_IMAGE_GRAYSCALE);
 24     assert(src1);
 25     assert(src2);
 26 
 27     //IplImage * img = cvCreateImage(cvSize(100, 100), IPL_DEPTH_8U, 1);
 28     //assert(img); 
 29     //cvZero(img);
 30     //cvSetReal2D(img, 49, 49, 255);    
 31      
 32     cvNamedWindow("原始图像1", 0);    
 33     cvNamedWindow("原始图像2", 0);
 34     cvNamedWindow("题目_a", 0);
 35     cvNamedWindow("题目_b", 0);
 36     cvNamedWindow("题目_c", 0);
 37     //cvNamedWindow("题目_d", 0);
 38 
 39     /*char * imageName = "E:\\Testing\\Image\\ExerciseWindow.jpg";
 40     cvSaveImage(imageName, img);*/
 41     cvShowImage("原始图像1", src1);
 42     cvShowImage("原始图像2", src2);
 43      
 44     //---------------------------a:开始--------------------------------//
 45 
 46     IplImage * diff12 = cvCloneImage(src1);
 47     cvZero(diff12);
 48 
 49     cvAbsDiff(src1, src2, diff12);
 50     cvShowImage("题目_a", diff12);
 51 
 52     /*char * saveName = "E:\\Testing\\Image\\SavePath\\diff12.jpg";
 53     cvSaveImage(saveName, diff12);*/
 54 
 55     //---------------------------a:结束--------------------------------//    
 56 
 57 
 58     //---------------------------b:开始--------------------------------//
 59 
 60     IplImage * cleandiff = cvCloneImage(diff12);
 61     cvZero(cleandiff);
 62 
 63     //针对有很强照明或反射梯度的图像,需要根据梯度进行阈值化时,自适应技术非常有用,看图5-24的处理结果对比很明显
 64     cvThreshold(diff12, cleandiff, 64, 255, CV_THRESH_BINARY);
 65 
 66     cvShowImage("题目_b", cleandiff);
 67 
 68     /*saveName = "E:\\Testing\\Image\\SavePath\\image_open.jpg";
 69     cvSaveImage(saveName, cleandiff);*/
 70 
 71     //---------------------------b:结束--------------------------------//    
 72 
 73     //---------------------------c:开始--------------------------------//
 74 
 75     IplImage * dirtydiff = cvCloneImage(cleandiff);
 76     cvZero(dirtydiff);
 77 
 78     IplConvKernel * kernel = cvCreateStructuringElementEx(3, 3, 1, 1, CV_SHAPE_RECT);
 79     cvMorphologyEx(cleandiff, dirtydiff, NULL, kernel, CV_MOP_OPEN,2);
 80 
 81     cvShowImage("题目_c", dirtydiff);
 82 
 83     /*saveName = "E:\\Testing\\Image\\SavePath\\image_close.jpg";
 84     cvSaveImage(saveName, dirtydiff);*/
 85 
 86     //---------------------------c:结束--------------------------------//    
 87     
 88     
 89     cvWaitKey(0);
 90 
 91     cvReleaseImage(&src1);
 92     cvReleaseImage(&src2);
 93     cvReleaseImage(&diff12);
 94     cvReleaseImage(&cleandiff);
 95     cvReleaseImage(&dirtydiff);
 96 
 97     cvDestroyWindow("原始图像1");     
 98     cvDestroyWindow("原始图像2");
 99     cvDestroyWindow("题目_a");
100     cvDestroyWindow("题目_b");
101     cvDestroyWindow("题目_c");
102     //cvDestroyWindow("题目_d");
103 
104 
105     return 0;
106 }
107  

 

结果图片:

 
 
posted on 2015-04-19 19:58  毋忆典藏  阅读(457)  评论(0编辑  收藏  举报