# week2_code **Repository Path**: sika0819/week2_code ## Basic Information - **Project Name**: week2_code - **Description**: 第二周源码 - **Primary Language**: C++ - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2019-04-14 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 第一题 ## 原图 ### 代码 ```c++ char fn[] = "C:\\opencv\\sources\\samples\\data\\lena.jpg"; Mat img = imread(fn); imshow("lena",img); ``` ### 结果 ![origin](origin.png) ## 平均滤波 ### 代码 ```c++ Mat blurImg; blur(img, blurImg, Size(5, 5)); imshow("MeanFilter", blurImg); ``` ### 结果 ![MeanFilter](MeanFilter.png) ## 高斯滤波 ### 代码 ```c++ Mat gausImg; GaussianBlur(img, gausImg,Size(5,5),1.5); imshow("GaussianBlur", gausImg); ``` ### 结果 ![GaussianBlur](GaussianBlur.png) ## 中值滤波 ### 代码 ```c++ Mat medianImg; medianBlur(img, medianImg, 5); imshow("MedianBlur", medianImg); ``` ### 结果 ![MedianBlur](MedianBlur.png) ## 结果分析 可以明显看出,中值滤波的效果最好,图像失真较少,背景的噪点也消去了很多。平均滤波失真最严重,高斯滤波比中值滤波模糊一些,但角色脸部比平均滤波清晰。 # 第二题 ## Sobel算子 ### 代码 ```c++ Mat gray; cvtColor(img, gray, COLOR_BGR2GRAY);//灰度化 Mat sobelImg; Sobel(gray, sobelImg, CV_8U, 1, 1);//两个方向边缘都求 imshow("sobel", sobelImg); ``` ### 结果 ![sobel](sobel.png) ## Canny算子 ### 代码 ```c++ Mat gray; cvtColor(img, gray, COLOR_BGR2GRAY);//灰度化 Mat cannyImg; Canny(gray, cannyImg,125,350); imshow("Canny", cannyImg); ``` ### 结果 ![canny](Canny.png) ## 结果分析 可以明显看出,Canny算子的效果比Sobel算子好很多。Canny算子会去除很多干扰点,并且能将断掉的线相连。尤其时帽子部分,很多褶皱并没有被识别为边缘。但是由于指定的阈值比较高,所以丢失了很多信息,经过后续处理,会好很多。 # 第三题 ## 代码 ```c++ class Histogram1D {//一个柱状图 private: int histSize[1];//高度 float hranges[2];//高度范围 const float* ranges[1];//范围 int channels[1];//通道 public: Histogram1D() {//初始化 histSize[0] = 256;//灰度值大小为0~255总共256个 hranges[0] = 0.0f; hranges[1] = 255.0f; ranges[0] = hranges; channels[0] = 0; } MatND getHistogram(const Mat &image) { MatND hist; calcHist(&image, 1, channels, Mat(), hist, 1, histSize, ranges); return hist; } Mat getHistogramImage(const Mat &image) { MatND hist = getHistogram(image); double maxValue = 0.0f; double minValue = 0.0f; minMaxLoc(hist, &minValue, &maxValue); Mat histImg(histSize[0], histSize[0], CV_8U, Scalar(255)); int hpt = static_cast(0.9*histSize[0]); for (int h = 0; h < histSize[0]; h++) { float binValue = hist.at(h); int intensity = static_cast(binValue*hpt / maxValue); line(histImg, Point(h, histSize[0]), Point(h, histSize[0] - intensity), Scalar::all(0)); } return histImg; } }; ``` ## 原图 ![pic2](pic2.png) ## 灰度直方图 ![histogram](histogram.png) ## 大津算法 ![pic2_bw](pic2_bw.png) # 第四题 ## 代码 ```c++ char fn[] = "E:\\VSCode\\week2_code\\rice.jpg"; Mat img = imread(fn,0); Mat bw; adaptiveThreshold(img, bw, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY,101,1); imshow("bw", bw); Mat element = getStructuringElement(MORPH_CROSS, Size(3, 3)); morphologyEx(bw, bw,MORPH_OPEN,element); Mat seg = bw.clone(); vector> cnts; int count = 0; findContours(seg, cnts, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE); string strCount; string areaStr; for (int i = cnts.size() - 1; i >= 0; i--) { vector c = cnts[i]; double area = contourArea(c); if (area < 10) continue; count++; Rect rect = boundingRect(c); double length= arcLength(c, true); std::cout << "count:" << count << " area:" << area <<" length:"<> strCount; ss << area; ss >> areaStr; putText(img,strCount,Point(rect.x,rect.y), FONT_HERSHEY_COMPLEX,0.5,Scalar(0,0xff,0)); } imshow("rice", img); waitKey(); ``` ## 原图 ![rice](rice.jpg) ## 结果 ![rice_result](rice_result.jpg) # 第五题 ## 代码 ```c++ char fn[] = "D:\\opencv\\sources\\samples\\data\\box.png"; Mat img = imread(fn); //Mat reshapeImg; //resize(img, reshapeImg, Size(500, 500)); imshow("origin", img); double keypointNum = 1000; Ptr surf = xfeatures2d::SURF::create(keypointNum); Ptr orb = ORB::create(keypointNum); vector keyPoints1,keyPoints2,keyPoints3; Mat descriptor1,descriptor2,descriptor3; surf->detectAndCompute(img, noArray(), keyPoints1, descriptor1); orb->detectAndCompute(img, noArray(), keyPoints2, descriptor2); drawKeypoints(img, keyPoints1, descriptor1, Scalar(0,0,255)); drawKeypoints(img, keyPoints2, descriptor2, Scalar(0, 0, 255)); FAST(img, keyPoints3, 40); drawKeypoints(img, keyPoints3, descriptor3, Scalar(0, 0, 255)); imshow("surf", descriptor1); imshow("orb", descriptor2); imshow("fast", descriptor3); ``` ## 棋盘格 ### 原图 ![origin](origin.jpg) ### SIFT ![SIFT](sift_result.jpg) ### ORB ![ORB](orb_result.jpg) ### SURF ![SURF](surf_result.jpg) ### FAST ![FAST](fast_result.jpg) ### Harris角点检测 ![harris_result](harris_result.jpg) ## 自选图片 ![box](box.png) ### SURF ![SURF](surf_pic_result.jpg) ### ORB ![ORB](orb_pic_result.jpg) ### FAST ![Fast](fast_pic_result.jpg) ### Harris ![Harris](harris_pic_result.jpg) ## 结果分析 从棋盘格来看,Fast检测结果非常差。SIFT存在一定漏检,Surf好很多,但是把很多棋盘格的中心点也作为特征点对待了。ORB基本坐落在角点周围 从自选图片来看,Fast也有很多漏检。ORB没有把字检测出来,Harrsi和Surf检测到了字,Harris的结果最精确