OpenCV不规则ROI提取
生活随笔
收集整理的這篇文章主要介紹了
OpenCV不规则ROI提取
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
提取不規(guī)則感興趣區(qū)域
void anomalyRoi(Mat &src, Mat &dst) {Mat original = src.clone();//高斯濾波GaussianBlur(original, original, Size(3, 3), 11, 11, 4);//灰度化cvtColor(original, original, COLOR_RGB2GRAY);//二值化Mat threshold_image;threshold(original, threshold_image, 90, 225, 3);//形態(tài)學(xué)操作,Size的第個(gè)值是水平方向,第二值是垂直方向Mat morphologyKernel = getStructuringElement(MORPH_RECT, Size(1, 3));//閉運(yùn)算morphologyEx(threshold_image, threshold_image, CV_MOP_CLOSE, morphologyKernel);Canny(threshold_image, threshold_image, 45, 45 * 3);imshow("Canny", threshold_image);vector<vector<Point>> contours;vector<Vec4i> hireachy;//找輪廓findContours(threshold_image, contours, hireachy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE, Point(-1, -1));int flag_count = 0;///vector< vector<Point>> contours_poly(contours.size());//建一個(gè)全黑的圖像Mat show_threImage = Mat::zeros(threshold_image.size(), CV_8UC3);double s_area = 0;for (size_t i = 0; i < contours.size(); i++){//在全黑的圖像上畫輪廓drawContours(show_threImage, contours, static_cast<int>(i), Scalar(255, 255, 255), 2, 8, hireachy, 0, Point()); //利用面積進(jìn)行判斷是否為最大區(qū)域double area = contourArea(contours[i]);if (s_area < area){s_area = area;}else{s_area = s_area;}if (area == s_area){flag_count = static_cast<int>(i);}else{flag_count = flag_count;}}imshow("Draw_Image_Contours", show_threImage);Mat gray(src.size(), src.type(), Scalar(0, 0, 0));drawContours(gray, contours, flag_count, Scalar(255, 255, 255), 4, 8, hireachy, 0, Point());namedWindow("gray", 0);imshow("gray", gray);//為了找內(nèi)部的一個(gè)漫水填充的種子點(diǎn)Rect s = boundingRect(contours[flag_count]);//黑色區(qū)域變成白色,遇到白色區(qū)域停止floodFill(gray, Point(s.x + s.width / 2, s.y + s.height / 2), Scalar(255,255,255));bitwise_and(src, gray, dst); }總結(jié)
以上是生活随笔為你收集整理的OpenCV不规则ROI提取的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: OpenCV的滑块与回调函数
- 下一篇: 使用Exiv2读取图像属性的详细信息