=======================================================================
这课后题吧,真的是不写不行,书看着很带劲儿,一到做练习题就熄火,这样怎么行!于是就写吧,不会就百度,不过更厉害的还是StackOverflow、Github,收获很多!
=======================================================================
cv::imread()标志位的解释:
flags = -1:imread按解码得到的方式读入图像
flags = 0:imread按单通道的方式读入图像,即灰白图像
flags = 1:imread按三通道方式读入图像,即彩色图像
codes
这是测试时用的图像
第一题
/*
读取一副带有纹理的图像,使用cv::GaussianBlur平滑
a.使用对称3*3、5*5、9*9和11*11大小的窗口平滑并显示结果。
b.通过用5*5高斯滤波器平滑图像两次,输出结果是否和11*11
滤波器平滑一次几乎相同?为什么?
*/
cv::Mat src = cv::imread(argv[1], 0);
cv::Mat tmp;
cv::imshow("source image:", src);
cv::GaussianBlur(src, tmp, Size(3, 3), 0);
cv::imshow("question a : 3*3 window", tmp);
cv::GaussianBlur(src, tmp, Size(5, 5), 0);
cv::imshow("question a :5 * 5", tmp);
cv::GaussianBlur(src, tmp, Size(9, 9), 0);
cv::imshow("question a :9 * 9", tmp);
cv::GaussianBlur(src, tmp, Size(11, 11), 0);
cv::imshow("question a :11 * 11", tmp);
1111的模糊效果比两次55的更强
/*
question b, answer:不相同,所使用的核是不同的,两次5*5对应的核,
和一次11*11所用的核是不一样的。
*/
cv::GaussianBlur(src, tmp, Size(5, 5), 0);
cv::GaussianBlur(tmp, tmp, Size(5, 5), 0);
cv::imshow("question b: two operation 5*5", tmp);
第二题
/*
-------------------------------------------------------------------------
question 2
*/
cv::Mat m(Size(100, 100), CV_8UC1, cv::Scalar::all(0));
m.at<char>(50, 50) = 255;
cv::GaussianBlur(m, tmp, Size(5, 5), 0);
cv::imshow("question 2 a : 5 * 5", tmp);
GaussianBlur(m, tmp, Size(9, 9), 0);
imshow("question 2 b : 9 * 9", tmp);
Mat m2;
GaussianBlur(m, m2, Size(5, 5), 0);
GaussianBlur(m2, m2, Size(5, 5), 0);
imshow("question 2 c : 5 * 5 twice", m2);
第三题
在GaussianBlur()中我没有看到参数param1、param2、param3的定义
下面是它的源码中的文档说明:(这一题可能有误,略过)
CV_EXPORTS_W void GaussianBlur( InputArray src, OutputArray dst, Size ksize,
double sigmaX, double sigmaY = 0,
int borderType = BORDER_DEFAULT );
......
......
This filter does not work inplace.
@param src Source 8-bit or floating-point, 1-channel or 3-channel image.
@param dst Destination image of the same size and type as src .
@param d Diameter of each pixel neighborhood that is used during filtering. If it is non-positive,
it is computed from sigmaSpace.
@param sigmaColor Filter sigma in the color space. A larger value of the parameter means that
farther colors within the pixel neighborhood (see sigmaSpace) will be mixed together, resulting
in larger areas of semi-equal color.
@param sigmaSpace Filter sigma in the coordinate space. A larger value of the parameter means that
farther pixels will influence each other as long as their colors are close enough (see sigmaColor
). When d\>0, it specifies the neighborhood size regardless of sigmaSpace. Otherwise, d is
proportional to sigmaSpace.
@param borderType border mode used to extrapolate pixels outside of the image, see #BorderTypes
第四题
这是test3
这是test4
这是做差之后
结果分析
一、可以看到differ(做差后的图像)不是黑色。
二、之后我将两幅图读取为灰度图后进行做差,发现结果与一相似。
三、当我用test3减去test3时我发现,得到的结果是黑色的。
从中可以看到test3-test4的结果不是黑色图像的原因是,第二次拍摄时镜头有偏移。
/*
question 4 :a
*/
cv::Mat src2, src3,differ;
src2=cv::imread(argv[2],0);
src3 = cv::imread(argv[3],0);
absdiff(src2, src3, differ);
cv::namedWindow("src2 and src3 by differ : ", WINDOW_AUTOSIZE);
cv::pyrDown(differ, differ); //图像太大,于是通过基于2的降采样,将图像尺寸缩小一半
cv::pyrDown(differ, differ); //还是有些大,再缩小一遍
cv::imshow("src2 and src3 by differ : ", differ);
/*
b,c
*/
cv::Mat cleandiff,dirtydiff;
cv::erode(differ, cleandiff,cv::Mat());
cv::dilate(cleandiff, cleandiff, cv::Mat());
cv::imshow("first erode,second dilate : ", cleandiff);
腐蚀操作缩减了明亮区域
膨胀操作扩张了明亮区域
于是左图明亮区域会比右图中的明亮区域暗。
第五题
这是test5
这是test6
这是结果:
先进行阈值化操作
在使用形态学开操作,进一步去除噪声。
使用腐蚀操作,然后与源图像异或,获取杯子的边缘轮廓。
/*
question 5 :a b c d
*/
//a
cv::Mat src5, src6,differ6;
src5 = cv::imread("./test5.jpg", 0);
src6 = cv::imread("./test6.jpg", 0);
cv::absdiff(src5, src6, differ6);
cv::pyrDown(differ6, differ6); //图像太大,于是通过基于2的降采样,将图像尺寸缩小一半
cv::pyrDown(differ6, differ6); //还是有些大,再缩小一遍
cv::imshow("src5 src6 differ6: ", differ6);
//b,设置THRESH_OTSU标志位,使用Otsu算法,自动决定最优的阈值
cv::threshold(differ6, differ6, 60, 255, THRESH_OTSU);
imshow("threshold thresh=60 by Otsu adaptive 60 ", differ6);
//对图像开操作进一步去除噪声
cv::Mat element = getStructuringElement(MORPH_RECT, Size(13, 13));
morphologyEx(differ6, differ6,MORPH_OPEN, element);
imshow("morphologuEx open operatioin", differ6);
//再进行腐蚀操作
cv::erode(differ6, differ6,cv::Mat());
imshow("erode operatioin", differ6);
//与原图像异或
cv::Mat output;
//异或过程中两幅图像大小要一致,由于differ6在前面经历了两次降采样,所以在此对源图像也进行相同操作
//两图像大小不一致会导致未经处理的异常,在内存位置处...
cv::pyrDown(src6, src6); //图像太大,于是通过基于2的降采样,将图像尺寸缩小一半
cv::pyrDown(src6, src6); //还是有些大,再缩小一遍
cv::bitwise_xor(src6, differ6,output);
imshow("bitwise_xor operatioin", output);
暂时先写到这里,后续更新。