今天分享一个OpenCV检测轮廓极点实例,原图如下,我们需要检测出地图中最大轮廓的上下左右四个极点,并进行标注显示。
第一步:阈值处理分割出地图轮廓
第二步:轮廓筛选,找到我们需要的轮廓
第三步:计算对应轮廓的极点坐标并标注
Python OpenCV源码与效果图如下:
import numpy as np import cv2 from PIL import Image, ImageDraw, ImageFont font = cv2.FONT_HERSHEY_SIMPLEX def putText_Chinese(img, text, pt, textColor=(0, 255, 0), textSize=20): left, top = pt[0], pt[1] if (isinstance(img, np.ndarray)): #判断是否OpenCV图片类型 img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) draw = ImageDraw.Draw(img) fontText = ImageFont.truetype("simhei.ttf", textSize, encoding="utf-8") draw.text((left, top), text, textColor, font=fontText) return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR) img = cv2.imread('2.jpg') cv2.imshow('src',img) temp = img.copy() gray = cv2.cvtColor(temp,cv2.COLOR_BGR2GRAY) ret,thresh = cv2.threshold(gray, 130, 250, cv2.THRESH_BINARY) cv2.imshow("thres",thresh) contours,hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) for cnt in contours: (x, y, w, h) = cv2.boundingRect(cnt) if(w > 300 and h > 300): left_most = tuple(cnt[cnt[:, :, 0].argmin()][0]) right_most = tuple(cnt[cnt[:, :, 0].argmax()][0]) top_most = tuple(cnt[cnt[:, :, 1].argmin()][0]) bottom_most = tuple(cnt[cnt[:, :, 1].argmax()][0]) cv2.drawContours(img,cnt,-1,(255,0,0),2) cv2.circle(img,left_most,5,(0,255,0),-1, cv2.LINE_AA) cv2.circle(img,right_most,5,(0,255,0),-1, cv2.LINE_AA) cv2.circle(img,top_most,5,(0,255,0),-1, cv2.LINE_AA) cv2.circle(img,bottom_most,5,(0,255,0),-1, cv2.LINE_AA) img = putText_Chinese(img, "西", left_most, (0, 255, 255), 30) img = putText_Chinese(img, "东", right_most, (0, 255, 255), 30) img = putText_Chinese(img, "北", top_most, (0, 255, 255), 30) img = putText_Chinese(img, "南", bottom_most, (0, 255, 255), 30) img = putText_Chinese(img, "中 国", (int(x + w/2),int(y +h/2)), (255, 255, 0), 70) cv2.imshow('contours', img) cv2.imwrite('result.bmp',img) cv2.waitKey(0) cv2.destroyAllWindows()
程序运行结果:
C++ OpenCV核心代码如下:
box[i] = minAreaRect(Mat(contours[i])); //计算每个轮廓最小外接矩形 if (box[i].size.width < 50 || box[i].size.height < 50) continue; //计算轮廓极值点 Point extLeft = *min_element(contours[i].begin(), contours[i].end(), [](const Point& lhs, const Point& rhs) { return lhs.x < rhs.x; }); Point extRight = *max_element(contours[i].begin(), contours[i].end(), [](const Point& lhs, const Point& rhs) { return lhs.x < rhs.x; }); Point extTop = *min_element(contours[i].begin(), contours[i].end(), [](const Point& lhs, const Point& rhs) { return lhs.y < rhs.y; }); Point extBot = *max_element(contours[i].begin(), contours[i].end(), [](const Point& lhs, const Point& rhs) { return lhs.y < rhs.y; });
C++ OpenCV完整源码将发布在知识星球主题中,欢迎关注公众号:OpenCV与AI深度学习。