• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Python OpenCV实现测量图片物体宽度

python 搞代码 4年前 (2022-01-07) 20次浏览 已收录 0个评论

这篇文章主要介绍了Python OpenCV实现测量图片物体宽度,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

一、 题目描述

测量所给图片的高度,即上下边缘间的距离。

思路:

  • 将图片进行阈值操作得到二值化图片。
  • 截取只包含上下边框的部分,以便于后续的轮廓提取
  • 轮廓检测
  • 得到结果

二、 实现过程

1.用于给图片添加中文字符

 #用于给图片添加中文字符 def ImgText_CN(img, text, left, top, textColor=(0, 255, 0), textSize=20): if (isinstance(img, np.ndarray)): #判断是否为OpenCV图片类型 img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) draw = ImageDraw.Draw(img) fontText = ImageFont.truetype(r'C:\Windows\Fonts\simsun.ttc', textSize, encoding="utf-8")     ##中文字体 draw.text((left, top), text, textColor, font=fontText)   #写文字 return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)

2.实现图片反色功能

 #实现图片反色功能 def PointInvert(img): height, width = img.shape    #获取图片尺寸 for i in range(height): for j in range(width): pi = img[i, j] img[i, j] = 255 - pi return img

3.边缘检测

 # canny边缘检测 edges = cv2.Canny(th, 30, 70) res=PointInvert(edges)              #颜色反转 #显示图片 cv2.imshow('original', th)            #显示二值化后的图,主题为白色,背景为黑色 更加容易找出轮廓 key = cv2.waitKey(0) if key==27: #按esc键时,关闭所有窗口 print(key) cv2.destroyAllWindows()

4.轮廓操作

 contours, hierarchy = cv2.findContours(th, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)    #得到轮廓 cnt = contours[0]        #取出轮廓 x, y, w, h = cv2.boundingRect(cnt)     #用一个矩形将轮廓包围 img_gray = cv2.cvtColor(res, cv2.COLOR_GRAY2BGR)        #将灰度转化为彩色图片方便画图 cv2.line(img_gray, (x, y), (x + w, y), (0,0,255), 2, 5)     #上边缘 cv2.line(img_gray, (x, y+h), (x + w, y+h), (0, 0, 255), 2, 5)  #下边缘 img1[80:230, 90:230] = img_gray     #用带有上下轮廓的图替换掉原图的对应部分

5.显示图片

 res1=ImgText_CN(img1, '宽度%d'%h, 25, 25, textColor=(0, 255, 0), textSize=30)  #绘制文字 #显示图片 cv2.imshow('original', res1) key = cv2.waitKey(0) if key==27: #按esc键时,关闭所有窗口 print(key) cv2.destroyAllWindows()

6.完整代码

 import cv2 import numpy as np from PIL import Image, ImageDraw, ImageFont #用于给图片添加中文字符 def ImgText_CN(img, text, left, top, textColor=(0, 255, 0), textSize=20): if (isinstance(img, np.ndarray)): #判断是否为OpenCV图片类型 img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) draw = ImageDraw.Draw(img) fontText = ImageFont.truetype(r'C:\Windows\Fonts\simsun.ttc', textSize, encoding="utf-8")     ##中文字体 draw.text((left, top), text, textColor, font=fontText)   #写文字 return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR) #实现图片反色功能 def PointInvert(img): height, width = img.shape    #获取图片尺寸 for i in range(height): for j in range(width): pi = img[i, j] img[i, j] = 255 - pi return img img=cv2.imread("gongjian1.bmp",0)        #加载彩色图 img1=cv2.imread("gongjian1.bmp",1)        #加载灰度图 recimg = img[80:230, 90:230]          #截取需要的部分 img2 = img1[80:230, 90:230]           #截取需要的部分 ret, th = cv2.threshold(recimg, 90, 255, cv2.THRESH_BI<strong style="color:transparent">来源gao@daima#com搞(%代@#码@网</strong>NARY)     #阈值操作二值化 # canny边缘检测 edges = cv2.Canny(th, 30, 70) res=PointInvert(edges)              #颜色反转 #显示图片 cv2.imshow('original', th)            #显示二值化后的图,主题为白色,背景为黑色 更加容易找出轮廓 key = cv2.waitKey(0) if key==27: #按esc键时,关闭所有窗口 print(key) cv2.destroyAllWindows() contours, hierarchy = cv2.findContours(th, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)    #得到轮廓 cnt = contours[0]        #取出轮廓 x, y, w, h = cv2.boundingRect(cnt)     #用一个矩形将轮廓包围 img_gray = cv2.cvtColor(res, cv2.COLOR_GRAY2BGR)        #将灰度转化为彩色图片方便画图 cv2.line(img_gray, (x, y), (x + w, y), (0,0,255), 2, 5)     #上边缘 cv2.line(img_gray, (x, y+h), (x + w, y+h), (0, 0, 255), 2, 5)  #下边缘 img1[80:230, 90:230] = img_gray                 #用带有上下轮廓的图替换掉原图的对应部分 res1=ImgText_CN(img1, '宽度%d'%h, 25, 25, textColor=(0, 255, 0), textSize=30)  #绘制文字 #显示图片 cv2.imshow('original', res1) key = cv2.waitKey(0) if key==27: #按esc键时,关闭所有窗口 print(key) cv2.destroyAllWindows()

三、 运行结果(效果)

四、 问题及解决方法

红色轮廓没有显示,解决方案:将灰度图转化为彩色图

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持gaodaima搞代码网

以上就是Python OpenCV实现测量图片物体宽度的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Python OpenCV实现测量图片物体宽度

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址