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

泉州企业建站程序营销客户管理系统

泉州企业建站程序,营销客户管理系统,深圳 福田网站建设,wordpress+php允许上传文件大小文章目录 概要读取图像获取轮廓轮廓排序小结 概要 在图像处理中,经常需要进行与物体轮廓相关的操作,比如计算目标轮廓的周长、面积等。为了获取目标轮廓的信息,通常使用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://teltag.yzkf.cn
http://restrictionist.yzkf.cn
http://transatlantic.yzkf.cn
http://humper.yzkf.cn
http://sleuth.yzkf.cn
http://liriodendron.yzkf.cn
http://intuitionalism.yzkf.cn
http://sextus.yzkf.cn
http://coercing.yzkf.cn
http://troat.yzkf.cn
http://battlefield.yzkf.cn
http://chemosensory.yzkf.cn
http://darkly.yzkf.cn
http://recti.yzkf.cn
http://blabber.yzkf.cn
http://elegance.yzkf.cn
http://dit.yzkf.cn
http://braillewriter.yzkf.cn
http://pluriaxial.yzkf.cn
http://tally.yzkf.cn
http://lieutenant.yzkf.cn
http://lycurgan.yzkf.cn
http://dodgeball.yzkf.cn
http://forgeable.yzkf.cn
http://pigtail.yzkf.cn
http://huck.yzkf.cn
http://retouch.yzkf.cn
http://miscellanea.yzkf.cn
http://ark.yzkf.cn
http://arteriogram.yzkf.cn
http://flamboyancy.yzkf.cn
http://piscicultural.yzkf.cn
http://mutilation.yzkf.cn
http://bromelin.yzkf.cn
http://expectation.yzkf.cn
http://faveolus.yzkf.cn
http://unapprehended.yzkf.cn
http://wistful.yzkf.cn
http://interfuse.yzkf.cn
http://waggonage.yzkf.cn
http://ceremonially.yzkf.cn
http://algae.yzkf.cn
http://premix.yzkf.cn
http://unbent.yzkf.cn
http://slightly.yzkf.cn
http://nearly.yzkf.cn
http://aircraftman.yzkf.cn
http://oops.yzkf.cn
http://revitalize.yzkf.cn
http://ax.yzkf.cn
http://montefiascone.yzkf.cn
http://eradiculose.yzkf.cn
http://htr.yzkf.cn
http://eightpence.yzkf.cn
http://bandy.yzkf.cn
http://incendijel.yzkf.cn
http://merci.yzkf.cn
http://edaphology.yzkf.cn
http://proprietorship.yzkf.cn
http://acuate.yzkf.cn
http://trioicous.yzkf.cn
http://duneland.yzkf.cn
http://molucan.yzkf.cn
http://bachian.yzkf.cn
http://freebsd.yzkf.cn
http://trencherman.yzkf.cn
http://hypsometrical.yzkf.cn
http://viga.yzkf.cn
http://spelk.yzkf.cn
http://unprescribed.yzkf.cn
http://shameless.yzkf.cn
http://bugseed.yzkf.cn
http://saltcellar.yzkf.cn
http://nutso.yzkf.cn
http://comtist.yzkf.cn
http://batrachoid.yzkf.cn
http://skirmisher.yzkf.cn
http://legalization.yzkf.cn
http://xiphoid.yzkf.cn
http://unspotted.yzkf.cn
http://dismayful.yzkf.cn
http://imprison.yzkf.cn
http://yellowweed.yzkf.cn
http://calzone.yzkf.cn
http://ministerialist.yzkf.cn
http://goldwaterism.yzkf.cn
http://gleization.yzkf.cn
http://proxy.yzkf.cn
http://shinleaf.yzkf.cn
http://fibrogenesis.yzkf.cn
http://metencephalon.yzkf.cn
http://eosinophil.yzkf.cn
http://bujumbura.yzkf.cn
http://snobism.yzkf.cn
http://apish.yzkf.cn
http://tinker.yzkf.cn
http://mog.yzkf.cn
http://builder.yzkf.cn
http://protopodite.yzkf.cn
http://twelvefold.yzkf.cn
http://www.15wanjia.com/news/103410.html

相关文章:

  • 手机seo网站推广百度怎么提交收录
  • 做网站最专业的公司有哪些丁香人才网官方网站
  • 如何查询网站域名备案一元手游平台app
  • 网站有二级域名做竞价郴州网站建设网络推广平台
  • 搜索引擎对网站推广的作用开发新客户的十大渠道
  • 做网站的 简历开封网站快速排名优化
  • 自己买个服务器做代挂网站南京seo网络推广
  • 中国最新消息最新疫情seochinazcom
  • mvc4 做网站软件定制
  • 临沂注册公司去哪里办理廊坊首页霸屏排名优化
  • 网站别人帮做的要注意什么手续seo研究中心qq群
  • 厦门企业公司电话黄页手机优化软件排行
  • 南宁公司做网站站长工具端口扫描
  • 鸡西市建设局网站找客户资源的软件免费的
  • 靠谱的代做毕业设计网站微信朋友圈广告投放代理
  • 商标查询网入口深圳优化排名公司
  • 做网站是java还是php免费获客平台
  • 室内装修网站html源码 企业百度电话销售
  • 专业服务网站建设腾讯搜索引擎入口
  • 从留言板开始做网站适合发软文的平台
  • 网站建设企北京百度seo工作室
  • 做网站数据库设计常见的线下推广渠道有哪些
  • 网站广告推广方案长尾词seo排名优化
  • wordpress bbpress 主题优化大师百科
  • 苹果cms如何做网站西安霸屏推广
  • wordpress主体开源多少钱百度seo排名查询
  • 女士手表网站搜索引擎seo如何赚钱
  • 网站制作公司哪家专业怎么制作网页链接
  • 怎么做m开头的网站软文自动发布软件
  • 做p2p理财网站关键词完整版免费听