PythonOpenCV实现测量图⽚物体宽度⼀、题⽬描述
测量所给图⽚的⾼度,即上下边缘间的距离。
思路:
将图⽚进⾏阈值操作得到⼆值化图⽚。
截取只包含上下边框的部分,以便于后续的轮廓提取
轮廓检测
得到结果
⼆、实现过程
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 = uetype(r'C:\Windows\', textSize, encoding="utf-8")    ##中⽂字体
<((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边缘检测consortium
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)    #得到轮廓
python基础代码大全黑客
cnt = contours[0]        #取出轮廓
x, y, w, h = cv2.boundingRect(cnt)    #⽤⼀个矩形将轮廓包围
img_gray = cv2.cvtColor(res, cv2.COLOR_GRAY2BGR)        #将灰度转化为彩⾊图⽚⽅便画图
为什么int类型的最大值是
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 = uetype(r'C:\Windows\', textSize, encoding="utf-8")    ##中⽂字体
<((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_BINARY)    #阈值操作⼆值化
# canny边缘检测
edges = cv2.Canny(th, 30, 70)
res=PointInvert(edges)              #颜⾊反转
特斯拉刹车失灵事件的开始#显⽰图⽚
cv2.imshow('original', th)            #显⽰⼆值化后的图,主题为⽩⾊,背景为⿊⾊更加容易出轮廓
learn是动词还是名词
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()
三、运⾏结果(效果)
四、问题及解决⽅法
红⾊轮廓没有显⽰,解决⽅案:将灰度图转化为彩⾊图
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。