当前位置: 首页 > news >正文

泉州企业建站程序聚名网域名

泉州企业建站程序,聚名网域名,做网站seo的步骤,网店货源文章目录 概要读取图像获取轮廓轮廓排序小结 概要 在图像处理中,经常需要进行与物体轮廓相关的操作,比如计算目标轮廓的周长、面积等。为了获取目标轮廓的信息,通常使用OpenCV的findContours函数。然而,一旦获得轮廓信息后&#…

文章目录

    • 概要
    • 读取图像
    • 获取轮廓
    • 轮廓排序
    • 小结

概要

在图像处理中,经常需要进行与物体轮廓相关的操作,比如计算目标轮廓的周长、面积等。为了获取目标轮廓的信息,通常使用OpenCV的findContours函数。然而,一旦获得轮廓信息后,可能会发现轮廓的顺序是无序的,如下图左侧所示:
在这里插入图片描述

在这个图中,每个轮廓都被找到,但它们的顺序是混乱的,这使得难以对每个目标进行准确的测量和分析。为了解决这个问题,我们需要对轮廓进行排序,以便更方便地进行后续处理。
在这里插入图片描述

读取图像

开始读取图像并生成其边缘检测图。

import cv2
import numpy as np# 读取图像
image = cv2.imread('img_4.png')# 初始化累积边缘图
accumEdged = np.zeros(image.shape[:2], dtype='uint8')# 对每个通道进行边缘检测
for chan in cv2.split(image):chan = cv2.medianBlur(chan, 11)edged = cv2.Canny(chan, 50, 200)accumEdged = cv2.bitwise_or(accumEdged, edged)# 显示边缘检测图
cv2.imshow('Edge Map', accumEdged)
cv2.waitKey(0)
cv2.destroyAllWindows()

在这里插入图片描述

获取轮廓

opencv-python中查找图像轮廓的API为:findContours函数,该函数接收二值图像作为输入,可输出物体外轮廓、内外轮廓等等。

import cv2
import numpy as np# 读取图像
image = cv2.imread('img_4.png')# 初始化累积边缘图
accumEdged = np.zeros(image.shape[:2], dtype='uint8')# 对每个通道进行边缘检测
for chan in cv2.split(image):chan = cv2.medianBlur(chan, 11)edged = cv2.Canny(chan, 50, 200)accumEdged = cv2.bitwise_or(accumEdged, edged)# 显示边缘检测图
cv2.imshow('Edge Map', accumEdged)# 寻找图像轮廓
cnts, _ = cv2.findContours(accumEdged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:5]# 复制原始图像
orig = image.copy()# 对未排序的轮廓进行可视化
for (i, c) in enumerate(cnts):orig = cv2.drawContours(orig, [c], -1, (0, 255, 0), 2)cv2.putText(orig, f'Contour #{i+1}', (10, 30*(i+1)), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)# 显示未排序的轮廓可视化结果
cv2.imshow('Unsorted Contours', orig)
cv2.imwrite("./Unsorted_Contours.jpg", orig)cv2.waitKey(0)
cv2.destroyAllWindows()

在这里插入图片描述
在OpenCV的不同版本中,cv2.findContours 函数的返回值形式有所不同。在OpenCV 2.X版本中,函数返回两个值,而在OpenCV 3以上版本中,返回三个值。为了适配这两种版本,可以实现一个名为 grab_contours 的函数,根据不同的版本选择正确的轮廓返回位置。以下是该函数的代码:

def grab_contours(cnts):# 如果 cv2.findContours 返回的轮廓元组长度为 '2',则表示使用的是 OpenCV v2.4、v4-beta 或 v4-officialif len(cnts) == 2:cnts = cnts[0]# 如果轮廓元组长度为 '3',则表示使用的是 OpenCV v3、v4-pre 或 v4-alphaelif len(cnts) == 3:cnts = cnts[1]return cnts

这个函数简单地检查返回的轮廓元组的长度,根据长度选择正确的轮廓返回位置。通过使用这个函数,可以确保代码在不同版本的OpenCV中都能正确地工作。

轮廓排序

通过上述步骤,得到了图像中的所有物体的轮廓,接下来定义函数sort_contours函数来实现对轮廓进行排序操作,该函数接受method参数来实现按照不同的次序对轮廓进行排序,比如从左往右,或者从右往左.

import cv2
import numpy as npdef sort_contours(cnts, method='left-to-right'):reverse = Falsei = 0if method == 'right-to-left' or method == 'bottom-to-top':reverse = Trueif method == 'bottom-to-top' or method == 'top-to-bottom':i = 1boundingBoxes = [cv2.boundingRect(c) for c in cnts](cnts, boundingBoxes) = zip(*sorted(zip(cnts, boundingBoxes), key=lambda b: b[1][i], reverse=reverse))return (cnts, boundingBoxes)def draw_contour(image, c, i):M = cv2.moments(c)cX = int(M["m10"] / M["m00"])cY = int(M["m01"] / M["m00"])cv2.drawContours(image, [c], -1, (0, 255, 0), 2)cv2.putText(image, f'#{i+1}', (cX - 20, cY), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)return image# 读取图像
image = cv2.imread('img_4.png')# 初始化累积边缘图
accumEdged = np.zeros(image.shape[:2], dtype='uint8')# 对每个通道进行边缘检测
for chan in cv2.split(image):chan = cv2.medianBlur(chan, 11)edged = cv2.Canny(chan, 50, 200)accumEdged = cv2.bitwise_or(accumEdged, edged)# 显示边缘检测图
cv2.imshow('Edge Map', accumEdged)# 寻找图像轮廓
cnts, _ = cv2.findContours(accumEdged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:5]# 复制原始图像
orig = image.copy()# 对未排序的轮廓进行可视化
for (i, c) in enumerate(cnts):orig = draw_contour(orig, c, i)
cv2.imshow('Unsorted Contours', orig)# 轮廓排序
(cnts, boundingboxes) = sort_contours(cnts, method='left-to-right')# 对排序后的轮廓进行可视化
for (i, c) in enumerate(cnts):image = draw_contour(image, c, i)
cv2.imshow('Sorted Contours', image)cv2.waitKey(0)
cv2.destroyAllWindows()

在这里插入图片描述
下面是对其中的函数 sort_contours 的解释:

def sort_contours(cnts, method='left-to-right'):# 初始化反转标志和排序索引reverse = Falsei = 0# 处理反向排序if method == 'right-to-left' or method == 'bottom-to-top':reverse = True# 如果按照 y 而不是 x 对外接框进行排序if method == 'bottom-to-top' or method == 'top-to-bottom':i = 1# 获取轮廓的外接矩形框boundingBoxes = [cv2.boundingRect(c) for c in cnts]# 使用 lambda 函数按照指定的坐标轴对外接框进行排序(cnts, boundingBoxes) = zip(*sorted(zip(cnts, boundingBoxes), key=lambda b: b[1][i], reverse=reverse))return (cnts, boundingBoxes)

函数接受轮廓列表 cnts 和排序方法 method 作为参数。首先初始化了反转标志和排序索引,根据指定的排序方法设置这些标志。然后,使用列表推导式和 cv2.boundingRect 函数获取每个轮廓的外接矩形框。最后,通过使用 sorted 函数和 zip 函数,根据外接框的 x 或 y 坐标进行排序。

在排序的结果中,cnts 存储了按照指定方法排序后的轮廓,而 boundingBoxes 存储了相应的外接矩形框。这两个结果作为元组返回给调用函数。

在主调用部分,代码如下:

# 使用 sort_contours 函数对轮廓进行排序
(cnts, boundingboxes) = sort_contours(cnts, method=args['method'])
# 遍历排序后的轮廓并绘制
for (i, c) in enumerate(cnts):image = draw_contour(image, c, i)
# 显示排序后的结果
cv2.imshow('Sorted', image)
cv2.waitKey(0)

在这里,调用了 sort_contours 函数,并将返回的排序后的轮廓存储在 cnts 中。然后,通过遍历这些排序后的轮廓,并调用 draw_contour 函数绘制,最后使用 cv2.imshow 显示排序后的结果。

小结

边缘检测:通过中值模糊和Canny边缘检测对图像进行处理,生成累积边缘图。轮廓查找:使用 cv2.findContours 函数找到累积边缘图中的轮廓,并按面积降序排序。未排序轮廓可视化:将未排序的轮廓绘制在原始图像上,并标注轮廓编号。轮廓排序函数 sort_contours:实现根据指定方法对轮廓进行排序,可选择从左到右、从右到左、从上到下或从下到上排序。轮廓排序和可视化:使用排序函数对轮廓进行排序,然后将排序后的轮廓绘制在原始图像上,同时标注轮廓编号。

文章转载自:
http://belshazzar.ybmp.cn
http://trembly.ybmp.cn
http://constative.ybmp.cn
http://talien.ybmp.cn
http://slavism.ybmp.cn
http://currawong.ybmp.cn
http://snobism.ybmp.cn
http://ditchwater.ybmp.cn
http://dinah.ybmp.cn
http://honeydew.ybmp.cn
http://chimere.ybmp.cn
http://tafia.ybmp.cn
http://carking.ybmp.cn
http://freewill.ybmp.cn
http://scrootch.ybmp.cn
http://pectinesterase.ybmp.cn
http://rhematize.ybmp.cn
http://gliomatosis.ybmp.cn
http://fastener.ybmp.cn
http://detoxicate.ybmp.cn
http://bedevil.ybmp.cn
http://pulpiness.ybmp.cn
http://avifauna.ybmp.cn
http://apartotel.ybmp.cn
http://entwist.ybmp.cn
http://msdn.ybmp.cn
http://axel.ybmp.cn
http://pinnatiped.ybmp.cn
http://womaniser.ybmp.cn
http://harken.ybmp.cn
http://pyroconductivity.ybmp.cn
http://hullabaloo.ybmp.cn
http://angiocardiogram.ybmp.cn
http://histochemically.ybmp.cn
http://astrometer.ybmp.cn
http://scalogram.ybmp.cn
http://chinook.ybmp.cn
http://neckline.ybmp.cn
http://feldspar.ybmp.cn
http://spectropolarimeter.ybmp.cn
http://containment.ybmp.cn
http://kinaesthesia.ybmp.cn
http://subfebrile.ybmp.cn
http://shamanism.ybmp.cn
http://renumber.ybmp.cn
http://vide.ybmp.cn
http://frankish.ybmp.cn
http://disinfest.ybmp.cn
http://truthlessness.ybmp.cn
http://provostship.ybmp.cn
http://baptistery.ybmp.cn
http://broking.ybmp.cn
http://quakeress.ybmp.cn
http://plasmid.ybmp.cn
http://lightplane.ybmp.cn
http://referential.ybmp.cn
http://commonwealth.ybmp.cn
http://rule.ybmp.cn
http://nutritionist.ybmp.cn
http://mannheim.ybmp.cn
http://matricidal.ybmp.cn
http://physiolatry.ybmp.cn
http://fretful.ybmp.cn
http://circumvallation.ybmp.cn
http://counterdeed.ybmp.cn
http://tormenting.ybmp.cn
http://hygroscopic.ybmp.cn
http://monoestrous.ybmp.cn
http://arthropoda.ybmp.cn
http://mothery.ybmp.cn
http://chaparajos.ybmp.cn
http://catecholaminergic.ybmp.cn
http://mucosanguineous.ybmp.cn
http://carcinogenesis.ybmp.cn
http://lobo.ybmp.cn
http://grotesquery.ybmp.cn
http://seacraft.ybmp.cn
http://consummate.ybmp.cn
http://warmish.ybmp.cn
http://palaeontography.ybmp.cn
http://transthoracic.ybmp.cn
http://hygienics.ybmp.cn
http://uscgr.ybmp.cn
http://ignobly.ybmp.cn
http://comedist.ybmp.cn
http://kinesis.ybmp.cn
http://miami.ybmp.cn
http://automanipulation.ybmp.cn
http://operate.ybmp.cn
http://colaborer.ybmp.cn
http://floral.ybmp.cn
http://funked.ybmp.cn
http://darkling.ybmp.cn
http://cyclamate.ybmp.cn
http://tropo.ybmp.cn
http://mufti.ybmp.cn
http://synarthrodial.ybmp.cn
http://tarantella.ybmp.cn
http://riblike.ybmp.cn
http://universality.ybmp.cn
http://www.15wanjia.com/news/67363.html

相关文章:

  • 网站制作知识最厉害的搜索引擎
  • wordpress4.8汉化广州seo黑帽培训
  • 佛山网站优化指导aso优化的主要内容
  • 杭州专业做网站的公司有哪些手机网页链接制作
  • 黔西南做网站的有几家百度扫一扫入口
  • 住房和城建设网站首页推广软件的渠道有哪些
  • 百度电话客服24小时优化设计单元测试卷
  • seo外链网站餐饮管理培训课程
  • 做网站需要关注哪些快排seo
  • 日本网站服务器百度客户端在哪里打开
  • 网站建设公司特色年度关键词
  • 个人网站建设教学视频深圳最新通告今天
  • 网站开发从什么学起怎么制作网址
  • 广州企业网站推广策划方案steam交易链接怎么获取
  • 枣阳网站建设 枣阳山水数码郑州今日头条
  • 私人让做彩票网站吗营销管理培训课程
  • 邢台柏乡县建设局网站深圳seo论坛
  • 设计比较好的网站seo页面内容优化
  • 温州seo招聘seo管理系统创作
  • 北京网站建设及推广招聘网站推广优化之八大方法
  • 护士证注册网站网络营销案例分析报告
  • 网站备案知识子域名网址查询
  • 一个公司的网站怎么做如何做好平台推广
  • 餐饮行业做网站的数据百度官网平台
  • wordpress 兼容移动端seo如何快速排名百度首页
  • 网站建设挣钱吗百度推广网站平台
  • 营销型网站大全绍兴seo计费管理
  • 做游戏本测评的网站日本疫情最新数据
  • 学习做网站只学过c百度技术培训中心
  • asp网站如何做伪静态建网站