《学习OpenCV》第五章课后题5

来源:互联网 发布:linux mysql 配置详解 编辑:程序博客网 时间:2024/06/05 01:15

题目说明:首先拍摄一张某场景的照片,然后摄像机不动,在此场景中心位置放一个咖啡杯,再拍摄一张照片,将其载入电脑并都转换为8位灰度图像。
a.取其差的绝对值并显示结果,它应该是一个带有噪声的咖啡杯掩码。
b.对结果图像进行二值化阈值操作,剔除噪声的同时并保留咖啡杯。超过阈值的像素应该设为255.显示结果。
c.在图像上进行CV_MOP_OPEN操作,以进一步清除噪声。

#include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>#include <opencv2/imgproc/imgproc.hpp>using namespace cv;int main(){    Mat img1 = imread("cup_1.jpg",CV_LOAD_IMAGE_GRAYSCALE);    Mat img2 = imread("cup_2.jpg",CV_LOAD_IMAGE_GRAYSCALE);    Mat abs_diff;    absdiff(img1,img2,abs_diff);    Mat thres_binary;    threshold(abs_diff,thres_binary,20,255,THRESH_BINARY);    Mat mop_open;    Mat kernel = getStructuringElement(MORPH_RECT, Size(3,3), Point(-1,-1));    morphologyEx(thres_binary,mop_open,MORPH_OPEN,kernel);    imshow("abs_diff",abs_diff);    imshow("thres_binary",thres_binary);    imshow("mop_open",mop_open);    imwrite("abs_diff.jpg",abs_diff);    imwrite("thres_binary.jpg",thres_binary);    imwrite("mop_open.jpg",mop_open);    waitKey(0);}

原图
二值化
开运算

0 0