1 //--------------------------------------【程序说明】------------------------------------------- 2 // 在图像处理中总会遇到二值图像,故对二值图像中区域处理在所难免; 3 // 提取自己想要的区域部分对其处理; 4 // 此函数简单实现高亮度区域处理; 5 //------------------------------------------------------------------------------------------------ 6 //---------------------------------【头文件、命名空间包含部分】---------------------------- 7 // 描述:包含程序所使用的头文件和命名空间 8 //------------------------------------------------------------------------------------------------- 9 #include <opencv2/opencv.hpp>//头文件 10 using namespace cv;//包含cv命名空间 11 using namespace std; 12 13 //-----------------------------------【全局变量声明部分】-------------------------------------- 14 // 描述:全局变量的声明 15 //----------------------------------------------------------------------------------------------- 16 Mat g_srcImage; 17 Mat g_grayImage; 18 Mat g_dstImage; 19 int g_nThresh_max = 255; 20 vector <vector<Point>> g_vContours; 21 22 //-----------------------------------【main( )函数】-------------------------------------------- 23 int main() 24 { 25 // 读入待处理原始图像 26 g_srcImage = imread("C:/Users/Administrator/Desktop/2.jpg"); 27 if(!g_srcImage.data ) { printf("读取图片错误,请确定目录下是否有imread函数指定的图片存在~! "); return false; } 28 imshow("原始图",g_srcImage); 29 30 //-------------------------------------------------------------------------------------------------- 31 32 //--------------------------根据高亮度特征-------------------------------------------- 33 cvtColor(g_srcImage,g_grayImage,CV_BGR2GRAY); 34 imshow("灰度图",g_grayImage); 35 //adaptiveThreshold(g_grayImage,g_grayImage,g_nThresh_max,ADAPTIVE_THRESH_MEAN_C,THRESH_BINARY,3,0);//自动阈值 36 //threshold(g_grayImage,g_grayImage,100,255,THRESH_BINARY); 37 threshold(g_grayImage,g_grayImage,0,g_nThresh_max,CV_THRESH_OTSU); 38 imshow("高亮度图像",g_grayImage); 39 //waitKey(0); 40 41 //----------------------------------------------------------------------------------------------------------- 42 43 // 进行闭运算操作 44 Mat element = getStructuringElement(MORPH_RECT, Size(4, 4)); 45 morphologyEx(g_grayImage,g_dstImage, MORPH_CLOSE, element); 46 47 // 查找需要填充区域的轮廓 48 49 vector <Vec4i> hierarchy; 50 findContours(g_dstImage,g_vContours,hierarchy,CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE); 51 52 // 将轮廓内填充 53 if( !g_vContours.empty() & !hierarchy.empty() ) 54 { 55 for (int idx=0;idx < g_vContours.size();idx++) 56 { 57 58 drawContours(g_dstImage,g_vContours,idx,Scalar::all(255),CV_FILLED,8);//填充轮廓内部 59 60 } 61 } 62 63 //------------------------------------------------------------------------------------------------ 64 65 66 //-----------------------------取面积最大的一块---------------------------------------- 67 double maxArea = 0; 68 vector <Point> maxContour; 69 findContours(g_dstImage,g_vContours,hierarchy,CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE); 70 if( !g_vContours.empty() && !hierarchy.empty() ) 71 { 72 for (int idx=0;idx < g_vContours.size();idx++) 73 { 74 double contArea = contourArea(g_vContours[idx]);//当前区域的面积 75 // 求最大面积的区域 76 if( contArea>maxArea ) 77 { 78 maxArea = contArea; 79 maxContour = g_vContours[idx]; 80 } 81 } 82 } 83 84 // 将轮廓转为矩形框 85 Rect maxRect = boundingRect(maxContour); 86 87 88 // 显示连通域 89 Mat result1, result2; 90 91 g_dstImage.copyTo(result1); 92 g_dstImage.copyTo(result2); 93 94 for (size_t i = 0; i < g_vContours.size(); i++) 95 { 96 Rect r = boundingRect(g_vContours[i]); 97 rectangle(result1, r,Scalar(255)); 98 } 99 imshow("all regions", result1) ; 100 101 rectangle(result2, maxRect, Scalar(255)); 102 imshow("largest region", result2) ; 103 104 waitKey(0); 105 106 }