前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >OpenCV检测轮廓极点(Python C++)

OpenCV检测轮廓极点(Python C++)

作者头像
Color Space
发布2020-08-21 15:43:50
1.2K0
发布2020-08-21 15:43:50
举报

今天分享一个OpenCV检测轮廓极点实例,原图如下,我们需要检测出地图中最大轮廓的上下左右四个极点,并进行标注显示。

第一步:阈值处理分割出地图轮廓

第二步:轮廓筛选,找到我们需要的轮廓

第三步:计算对应轮廓的极点坐标并标注

Python OpenCV源码与效果图如下:

代码语言:javascript
复制
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核心代码如下:

代码语言:javascript
复制
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;
});
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-08-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 OpenCV与AI深度学习 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档