// 轮廓绘制 vector<vector<Point>> contours; vector<Vec4i> hierarchy; findContours(binary, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE); for (size_t c = 0; c < contours.size(); c++) { Rect rect = boundingRect(contours[c]); double area = contourArea(contours[c]); if (area < 200) { continue; } int h = rect.height; int w = rect.width; if (h > (3 * w) || h < 20) { continue; } rectangle(src, rect, Scalar(0, 0, 255)); } imshow("result", src);
se = cv.getStructuringElement(cv.MORPH_RECT, (1, 5), (-1, -1)) binary = cv.morphologyEx(binary, cv.MORPH_DILATE, se) out, contours, hierarchy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) for c in range(len(contours)): x, y, w, h = cv.boundingRect(contours[c]) area = cv.contourArea(contours[c]) if area < 200: continue if h > (3*w) or h < 20: continue cv.rectangle(src, (x, y), (x+w, y+h), (0, 0, 255), 1, 8, 0)