import cv2
import numpy as np
import math
import matplotlib.pyplot as plt
"""
函数的格式为:kmeans(data, K, bestLabels, criteria, attempts, flags)
(1)data: 分类数据,最好是np.float32的数据,每个特征放一列。之所以是np.float32原因是这种数据类型运算速度快,同样的数据下如果是uint型数据将会慢死你。
(2) K: 分类数,opencv2的kmeans分类是需要已知分类数的。
(3) bestLabels:预设的分类标签:没有的话 None
(4) criteria:迭代停止的模式选择,这是一个含有三个元素的元组型数。格式为(type,max_iter,epsilon)
其中,type又有两种选择:
—–cv2.TERM_CRITERIA_EPS :精确度(误差)满足epsilon停止。
—- cv2.TERM_CRITERIA_MAX_ITER:迭代次数超过max_iter停止。
—-cv2.TERM_CRITERIA_EPS+cv2.TERM_CRITERIA_MAX_ITER,两者合体,任意一个满足结束。
(5)attempts:重复试验kmeans算法次数,将会返回最好的一次结果
(6)flags:初始类中心选择,两种方法
cv2.KMEANS_PP_CENTERS ; cv2.KMEANS_RANDOM_CENTERS
"""
def panelAbstract(srcImage):imgHeight,imgWidth = srcImage.shape[:2]imgHeight = int(imgHeight);imgWidth = int(imgWidth)# 均值聚类提取前景:二维转一维imgVec = np.float32(srcImage.reshape((-1,3)))criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER,10,1.0)flags = cv2.KMEANS_RANDOM_CENTERSret,label,clusCenter = cv2.kmeans(imgVec,2,None,criteria,10,flags)clusCenter = np.uint8(clusCenter)clusResult = clusCenter[label.flatten()]imgres = clusResult.reshape((srcImage.shape))imgres = cv2.cvtColor(imgres,cv2.COLOR_BGR2GRAY)bwThresh = int((np.max(imgres)+np.min(imgres))/2)_,thresh = cv2.threshold(imgres,bwThresh,255,cv2.THRESH_BINARY_INV)threshRotate = cv2.merge([thresh,thresh,thresh])# 确定前景外接矩形contours, hierarchy = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)minvalx = np.max([imgHeight,imgWidth]);maxvalx = 0minvaly = np.max([imgHeight,imgWidth]);maxvaly = 0maxconArea = 0;maxAreaPos = -1for i in range(len(contours)):if maxconArea < cv2.contourArea(contours[i]):maxconArea = cv2.contourArea(contours[i])maxAreaPos = iobjCont = contours[maxAreaPos]# 旋转校正前景rect = cv2.minAreaRect(objCont)for j in range(len(objCont)):minvaly = np.min([minvaly,objCont[j][0][0]])maxvaly = np.max([maxvaly,objCont[j][0][0]])minvalx = np.min([minvalx,objCont[j][0][1]])maxvalx = np.max([maxvalx,objCont[j][0][1]])if rect[2] <=-45:rotAgl = 90 +rect[2]else:rotAgl = rect[2]if rotAgl == 0:panelImg = srcImage[minvalx:maxvalx,minvaly:maxvaly,:]else:rotCtr = rect[0]rotCtr = (int(rotCtr[0]),int(rotCtr[1]))rotMdl = cv2.getRotationMatrix2D(rotCtr,rotAgl,1)imgHeight,imgWidth = srcImage.shape[:2]#图像的旋转dstHeight = math.sqrt(imgWidth *imgWidth + imgHeight*imgHeight)dstRotimg = cv2.warpAffine(threshRotate,rotMdl,(int(dstHeight),int(dstHeight)))dstImage = cv2.warpAffine(srcImage,rotMdl,(int(dstHeight),int(dstHeight)))dstRotimg = cv2.cvtColor(dstRotimg,cv2.COLOR_BGR2GRAY)_,dstRotBW = cv2.threshold(dstRotimg,127,255,0)contours, hierarchy = cv2.findContours(dstRotBW,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)maxcntArea = 0;maxAreaPos = -1for i in range(len(contours)):if maxcntArea < cv2.contourArea(contours[i]):maxcntArea = cv2.contourArea(contours[i])maxAreaPos = ix,y,w,h = cv2.boundingRect(contours[maxAreaPos])#提取前景:panelpanelImg = dstImage[int(y):int(y+h),int(x):int(x+w),:]panel_img = cv2.imwrite('image2/panel_img.png', panelImg)  # 将画上矩形的图形保存到当前目录# cv2.imshow("img", img)# cv2.imshow("mask", mask)# cv2.imshow("target", target)cv2.imshow("panel_img", panel_img)return panelImgdef getTaeget(targetImg):kernel_4 = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 2))HSV = cv2.cvtColor(targetImg, cv2.COLOR_BGR2HSV)  # 把BGR图像转换为HSV格式Lower = np.array([0, 3, 5])  # 要识别颜色-红色的下限    ## 0 5 5 10 255 255Upper = np.array([10, 255, 255])  # 要识别的颜色-红色的上限# mask是把HSV图片中在颜色范围内的区域变成白色,其他区域变成黑色mask = cv2.inRange(HSV, Lower, Upper)erosion = cv2.erode(mask, kernel_4, iterations=1)erosion = cv2.erode(erosion, kernel_4, iterations=1)dilation = cv2.dilate(erosion, kernel_4, iterations=1)dilation = cv2.dilate(dilation, kernel_4, iterations=1)# target是把原图中的非目标颜色区域去掉剩下的图像target = cv2.bitwise_and(targetImg, targetImg, mask=dilation)# 将滤波后的图像变成二值图像放在binary中ret, binary = cv2.threshold(dilation, 127, 255, cv2.THRESH_BINARY)# 在binary中发现轮廓,轮廓按照面积从小到大排列contours, hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)w_ = []for i in contours:  # 遍历所有的轮廓x1, y1, w, h = cv2.boundingRect(i)  # 将轮廓分解为识别对象的左上角坐标和宽、高w_.append(w)i = w_.index(max(w_))x, y, w, h = cv2.boundingRect(contours[i])# cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255,), 3)cropped = targetImg[y - 480:y + 380, x - 190:x + 280]  # 裁剪坐标为[y0:y1, x0:x1]# cv_cut_img_circle = cv2.imwrite("image/cv_cut_img_circle.png", cropped)# print(x, y, w, h)target_img = cv2.imwrite('image2/target_img.png', cropped)  # 将画上矩形的图形保存到当前目录# cv2.imshow("img", img)# cv2.imshow("mask", mask)# cv2.imshow("target", target)cv2.imshow("target_img", target_img)# cv2.waitKey(600)return croppeddef getGuage(cicleImg):gaus = cv2.GaussianBlur(cicleImg, (3, 3), 0)gray_img = cv2.cvtColor(gaus, cv2.COLOR_BGR2GRAY)gradx = cv2.Sobel(gray_img, cv2.CV_16SC1, 1, 0)grady = cv2.Sobel(gray_img, cv2.CV_16SC1, 0, 1)canny_img = cv2.Canny(gradx, grady, 50, 200)  # 黑白的边缘 高低阈值比值为2:1或3:1最佳(50:150 = 1:3)ret_2, binary_2 = cv2.threshold(canny_img, 127, 255, cv2.THRESH_BINARY)contours_2, hierarchy_2 = cv2.findContours(binary_2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)h_2_ = []for j in contours_2:  # 遍历所有的轮廓x1_2, y1_2, w_2, h_2 = cv2.boundingRect(j)  # 将轮廓分解为识别对象的左上角坐标和宽、高h_2_.append(h_2)j = h_2_.index(max(h_2_))# print(max(h_))# h_2_ = (float(max(h_2_))) * 0.878# print(max(h_))# print("h_2_=", h_2_)x_2, y_2, w_2, h_2 = cv2.boundingRect(contours_2[j])print("x_2, y_2, w_2, h_2=",x_2, y_2, w_2, h_2)print("h_2 = ",h_2)cv2.rectangle(cicleImg, (x_2, y_2), (x_2 + w_2, y_2 + h_2), (0, 255,), 3)# cropped = cicleImg[(y_2 - 228):(y_2 + 118), (x_2 - 112):(x_2 - 80)]  # 裁剪坐标为[y0:y1, x0:x1]cropped = cicleImg[(y_2):(y_2 + h_2), (x_2):(x_2 + w_2)]cv_cut_guage = cv2.imwrite("image2/cv_cut_guage.png", cropped)cv2.imshow("cicleImg", cicleImg)# cv2.imshow("mask", mask)# cv2.imshow("target", target)cv2.imshow("canny_img", canny_img)cv2.imshow("cv_cut_guage", cv_cut_guage)return croppedif __name__=="__main__":srcImage = cv2.imread('image_guage/DSC_1394.JPG')#####040  400  145  235(error) 1394 1361(error) 035(error)# srcImage = cv2.imread('image2/1576638923.jpg')panelImg = panelAbstract(srcImage)targetImg = getTaeget(panelImg)cut_guage = getGuage(targetImg)gray_img = cv2.cvtColor(cut_guage, cv2.COLOR_BGR2GRAY)gaus = cv2.GaussianBlur(gray_img, (3, 3), 0)gradx = cv2.Sobel(gray_img, cv2.CV_16SC1, 1, 0)grady = cv2.Sobel(gray_img, cv2.CV_16SC1, 0, 1)canny_img = cv2.Canny(gradx, grady, 50, 300)  # 黑白的边缘 高低阈值比值为2:1或3:1最佳(50:150 = 1:3)ret_2, binary_2 = cv2.threshold(canny_img, 127, 255, cv2.THRESH_BINARY)contours_2, hierarchy_2 = cv2.findContours(binary_2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)#h_2_ = []for j in contours_2:  # 遍历所有的轮廓x1_2, y1_2, w_2, h_2 = cv2.boundingRect(j)  # 将轮廓分解为识别对象的左上角坐标和宽、高h_2_.append(h_2)j = h_2_.index(max(h_2_))# print(max(h_))h_2_ = (float(max(h_2_))) * 0.878# print(max(h_2_))print("h_2_=", h_2_)# x_2, y_2, w_2, h_2 = cv2.boundingRect(contours_2[j])# print(x, y, w, h)# cropped = cut_guage[(y_2 - 228):(y_2 + 115), (x_2 - 108):(x_2 - 80)]  # 裁剪坐标为[y0:y1, x0:x1]# cv_cut_guage = cv2.imwrite("image/cv_cut_guage4.png", cropped)kernel_4 = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 1))HSV = cv2.cvtColor(cut_guage, cv2.COLOR_BGR2HSV)  # 把BGR图像转换为HSV格式Lower = np.array([0, 10, 10])  # 要识别颜色-红色的下限    ## 0 5 5 10 255 255Upper = np.array([30, 255, 255])  # 要识别的颜色-红色的上限mask = cv2.inRange(HSV, Lower, Upper)# mask是把HSV图片中在颜色范围内的区域变成白色,其他区域变成黑色# erosion = cv2.erode(mask, kernel_4, iterations=1)# erosion = cv2.erode(erosion, kernel_4, iterations=1)dilation = cv2.dilate(mask, kernel_4, iterations=1)dilation = cv2.dilate(dilation, kernel_4, iterations=1)# target是把原图中的非目标颜色区域去掉剩下的图像# target = cv2.bitwise_and(img, img, mask=dilation)# 将滤波后的图像变成二值图像放在binary中ret_3, binary_3 = cv2.threshold(dilation, 127, 255, cv2.THRESH_BINARY)# 在binary中发现轮廓,轮廓按照面积从小到大排列contours_3, hierarchy_3 = cv2.findContours(binary_3, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)w_3_ = []for j in contours_3:  # 遍历所有的轮廓x1_3, y1_3, w_3, h_3 = cv2.boundingRect(j)  # 将轮廓分解为识别对象的左上角坐标和宽、高w_3_.append(w_3)j = w_3_.index(max(w_3_))x_3, y_3, w_3, h_3 = cv2.boundingRect(contours_3[j])# cv2.rectangle(cut_guage, (x_3, y_3), (x_3 + w_3/2, y_3 + h_3/2), (0, 255,), 3)cv2.rectangle(cut_guage, (x_3, y_3), (x_3+ w_3, y_3 + h_3), (255, 0,0), 3)# srcImage = cv2.imwrite("image2/srcImage.png", srcImage)youwei = y_3+h_3/2print("youwei=",youwei)result = (1-(youwei/h_2_))*100print("************************************* ")print("result = ",result)cv2.namedWindow('srcImage', cv2.WINDOW_NORMAL)  # 窗口大小可以改变cv2.imshow("srcImage", srcImage)cv2.imshow("cut_guage", cut_guage)cv2.waitKey(0)cv2.destroyAllWindows()

                            

结果显示:

分步骤实现:test1file:/F:/code/python/wk_guage/image2/DSC_1394.JPG

前景背景分割:keanmes

原图如下所示:

import cv2
import numpy as np
import math
import matplotlib.pyplot as pltsrcImage = cv2.imread('image2/image2/DSC_1400_0000040.JPG')
#srcImage = cv2.imread('image2/guage_1.jpg')imgHeight, imgWidth = srcImage.shape[:2]
imgHeight = int(imgHeight);
imgWidth = int(imgWidth)
# 均值聚类提取前景:二维转一维
imgVec = np.float32(srcImage.reshape((-1, 3)))
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
flags = cv2.KMEANS_RANDOM_CENTERS
ret, label, clusCenter = cv2.kmeans(imgVec, 2, None, criteria, 10, flags)
clusCenter = np.uint8(clusCenter)
clusResult = clusCenter[label.flatten()]
imgres = clusResult.reshape((srcImage.shape))
imgres = cv2.cvtColor(imgres, cv2.COLOR_BGR2GRAY)
bwThresh = int((np.max(imgres) + np.min(imgres)) / 2)
_, thresh = cv2.threshold(imgres, bwThresh, 255, cv2.THRESH_BINARY_INV)
threshRotate = cv2.merge([thresh, thresh, thresh])
# 确定前景外接矩形
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
minvalx = np.max([imgHeight, imgWidth]);
maxvalx = 0
minvaly = np.max([imgHeight, imgWidth]);
maxvaly = 0
maxconArea = 0;
maxAreaPos = -1
for i in range(len(contours)):if maxconArea < cv2.contourArea(contours[i]):maxconArea = cv2.contourArea(contours[i])maxAreaPos = i
objCont = contours[maxAreaPos]
# 旋转校正前景
rect = cv2.minAreaRect(objCont)
for j in range(len(objCont)):minvaly = np.min([minvaly, objCont[j][0][0]])maxvaly = np.max([maxvaly, objCont[j][0][0]])minvalx = np.min([minvalx, objCont[j][0][1]])maxvalx = np.max([maxvalx, objCont[j][0][1]])
if rect[2] <= -45:rotAgl = 90 + rect[2]
else:rotAgl = rect[2]
if rotAgl == 0:panelImg = srcImage[minvalx:maxvalx, minvaly:maxvaly, :]
else:rotCtr = rect[0]rotCtr = (int(rotCtr[0]), int(rotCtr[1]))rotMdl = cv2.getRotationMatrix2D(rotCtr, rotAgl, 1)imgHeight, imgWidth = srcImage.shape[:2]# 图像的旋转dstHeight = math.sqrt(imgWidth * imgWidth + imgHeight * imgHeight)dstRotimg = cv2.warpAffine(threshRotate, rotMdl, (int(dstHeight), int(dstHeight)))dstImage = cv2.warpAffine(srcImage, rotMdl, (int(dstHeight), int(dstHeight)))dstRotimg = cv2.cvtColor(dstRotimg, cv2.COLOR_BGR2GRAY)_, dstRotBW = cv2.threshold(dstRotimg, 127, 255, 0)contours, hierarchy = cv2.findContours(dstRotBW, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)maxcntArea = 0;maxAreaPos = -1for i in range(len(contours)):if maxcntArea < cv2.contourArea(contours[i]):maxcntArea = cv2.contourArea(contours[i])maxAreaPos = ix, y, w, h = cv2.boundingRect(contours[maxAreaPos])# 提取前景:panelpanelImg = dstImage[int(y):int(y + h), int(x):int(x + w)]# cropped = targetImg[y - 150:y + 50, x - 80:x + 80]  # 裁剪坐标为[y0:y1, x0:x1]panel_img = cv2.imwrite("image2/panelImg.png", panelImg)cv2.imshow("panelImg", panel_img)

test2:从分割所得图像中,根据颜色特识别最大的颜色矩形,(这个表里面一定是某个液位标志)。

panelImg如下图所示:

import cv2
import numpy as np
import math
import matplotlib.pyplot as plt# targetImg = cv2.imread('image/1.jpg')
targetImg = cv2.imread('image/1.jpg')
kernel_4 = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 2))
HSV = cv2.cvtColor(targetImg, cv2.COLOR_BGR2HSV)  # 把BGR图像转换为HSV格式
Lower = np.array([0, 3, 5])  # 要识别颜色-红色的下限    ## 0 5 5 10 255 255
Upper = np.array([10, 255, 255])  # 要识别的颜色-红色的上限# mask是把HSV图片中在颜色范围内的区域变成白色,其他区域变成黑色
mask = cv2.inRange(HSV, Lower, Upper)
erosion = cv2.erode(mask, kernel_4, iterations=1)
erosion = cv2.erode(erosion, kernel_4, iterations=1)
dilation = cv2.dilate(erosion, kernel_4, iterations=1)
dilation = cv2.dilate(dilation, kernel_4, iterations=1)# target是把原图中的非目标颜色区域去掉剩下的图像
target = cv2.bitwise_and(targetImg, targetImg, mask=dilation)# 将滤波后的图像变成二值图像放在binary中
ret, binary = cv2.threshold(dilation, 127, 255, cv2.THRESH_BINARY)# 在binary中发现轮廓,轮廓按照面积从小到大排列
contours, hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)# w_ = []
#
# for i in contours:  # 遍历所有的轮廓
#         x1, y1, w, h = cv2.boundingRect(i)  # 将轮廓分解为识别对象的左上角坐标和宽、高
#         w_.append(w)
#         i = w_.index(max(w_))
# x, y, w, h = cv2.boundingRect(contours[i])# cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255,), 3)# cropped = targetImg[y - 150:y + 50, x - 80:x + 80]  # 裁剪坐标为[y0:y1, x0:x1]
# cv_cut_img_circle = cv2.imwrite("image2/cv_cut_img_circle.png", cropped)# print(x, y, w, h)
# cv2.imwrite('image/cv_cut_img_circle.png', cv_cut_img)  # 将画上矩形的图形保存到当前目录
cv2.drawContours(targetImg, contours, -1, (0, 0, 255), 3)
cv2.imshow("img", targetImg)
cv2.imshow("mask", mask)
cv2.imshow("target", target)
cv2.imshow("erosion", erosion)
cv2.imshow("dilation", dilation)
cv2.waitKey()
# cv2.imshow("cv_cut_img_circle", cv_cut_img_circle)
# return cropped

test3:在框出的最大矩形中,(先验知识,出的最大矩形,一定是液位表的位置),识别其中为一个带有红色标记的矩形,浮标

进一步缩小面积:

import cv2
import numpy as np
import math
import matplotlib.pyplot as pltcicleImg = cv2.imread('image/cv_cut_img_circle1.png')
gaus = cv2.GaussianBlur(cicleImg, (3, 3), 0)
gray_img = cv2.cvtColor(gaus, cv2.COLOR_BGR2GRAY)gradx = cv2.Sobel(gray_img, cv2.CV_16SC1, 1, 0)
grady = cv2.Sobel(gray_img, cv2.CV_16SC1, 0, 1)
canny_img = cv2.Canny(gradx, grady, 100, 200)  # 黑白的边缘 高低阈值比值为2:1或3:1最佳(50:150 = 1:3)ret_2, binary_2 = cv2.threshold(canny_img, 127, 255, cv2.THRESH_BINARY)
contours_2, hierarchy_2 = cv2.findContours(binary_2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)h_2_ = []
for j in contours_2:  # 遍历所有的轮廓x1_2, y1_2, w_2, h_2 = cv2.boundingRect(j)  # 将轮廓分解为识别对象的左上角坐标和宽、高h_2_.append(h_2)j = h_2_.index(max(h_2_))# print(max(h_))# h_2_ = (float(max(h_2_))) * 0.878# print(max(h_))# print("h_2_=", h_2_)
x_2, y_2, w_2, h_2 = cv2.boundingRect(contours_2[j])
cv2.rectangle(cicleImg, (x_2, y_2), (x_2+w_2, y_2+h_2), (0, 255,), 3)
print(x_2, y_2, w_2, h_2)
print(h_2)
cropped = cicleImg[(y_2 - 228):(y_2 + 118), (x_2 - 112):(x_2 - 80)]  # 裁剪坐标为[y0:y1, x0:x1]
cv_cut_guage = cv2.imwrite("image2/cv_cut_guage5.png", cropped)
cv2.imshow("cicleImg", cicleImg)
cv2.imshow("canny_demo", canny_img)
cv2.imshow("cv_cut_guage", cv_cut_guage)# return cropped
cv2.waitKey(0)
cv2.destroyAllWindows()

test4:在框出的最大矩形中,(先验知识,出的最大矩形,一定是液位表的位置),识别其中为一个带有红色标记的矩形,浮标,如下图所示image/cv_cut_guage.png

import cv2
import numpy as np
import math
import matplotlib.pyplot as pltcut_guage = cv2.imread('image2/cv_cut_guage.png')
# srcImage = cv2.imread('image/guage_2.png')
gray_img = cv2.cvtColor(cut_guage, cv2.COLOR_BGR2GRAY)
gaus = cv2.GaussianBlur(gray_img, (3, 3), 0)
gradx = cv2.Sobel(gray_img, cv2.CV_16SC1, 1, 0)
grady = cv2.Sobel(gray_img, cv2.CV_16SC1, 0, 1)
canny_img = cv2.Canny(gradx, grady, 100, 300)  # 黑白的边缘 高低阈值比值为2:1或3:1最佳(50:150 = 1:3)ret_2, binary_2 = cv2.threshold(canny_img, 127, 255, cv2.THRESH_BINARY)
contours_2, hierarchy_2 = cv2.findContours(binary_2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)h_2_ = []
for j in contours_2:  # 遍历所有的轮廓x1_2, y1_2, w_2, h_2 = cv2.boundingRect(j)  # 将轮廓分解为识别对象的左上角坐标和宽、高h_2_.append(h_2)j = h_2_.index(max(h_2_))# h_2_ = (float(max(h_2_))) * 0.878print("h_2_=", h_2_)kernel_4 = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 2))
HSV = cv2.cvtColor(cut_guage, cv2.COLOR_BGR2HSV)
Lower = np.array([0, 5, 5])  # 要识别颜色-红色的下限    ## 0 5 5 10 255 255
Upper = np.array([20, 255, 255])  # 要识别的颜色-红色的上限mask = cv2.inRange(HSV, Lower, Upper)
dilation = cv2.dilate(mask, kernel_4, iterations=1)
dilation = cv2.dilate(dilation, kernel_4, iterations=1)
ret_3, binary_3 = cv2.threshold(dilation, 127, 255, cv2.THRESH_BINARY)
contours_3, hierarchy_3 = cv2.findContours(binary_3, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)w_3_ = []
for j in contours_3:  # 遍历所有的轮廓x1_3, y1_3, w_3, h_3 = cv2.boundingRect(j)  # 将轮廓分解为识别对象的左上角坐标和宽、高w_3_.append(w_3)
j = w_3_.index(max(w_3_))x_3, y_3, w_3, h_3 = cv2.boundingRect(contours_3[j])youwei = y_3 + h_3 / 2
print("youwei=", youwei)result = (1 - (youwei / h_2_)) * 100
print(result)# cv2.imshow("canny_img", canny_img)
cv2.waitKey(0)
# cv2.destroyAllWindows()

python-opencv 定位识别读表相关推荐

  1. python opencv人脸识别考勤系统的完整源码

    这篇文章主要介绍了python opencv人脸识别考勤系统的完整源码,本文给大家介绍的非常详细,希望对大家的学习或工作具有一定的参考借鉴价值. 代码如下: import wx import wx.g ...

  2. Python+OpenCV人脸识别签到考勤系统(新手入门)

    Python+OpenCV人脸识别签到考勤系统(新手入门) 前言 项目效果图 项目需要的环境 编译器 辅助开发QT-designer 项目配置 代码部分 核心代码 项目目录结构 后记 正式版改进 项目 ...

  3. 基于python opencv人脸识别的签到系统

    基于python opencv人脸识别的签到系统 前言 先看下效果 实现的功能 开始准备 页面的构建 功能实现 代码部分 总结 前言 一个基于opencv人脸识别和TensorFlow进行模型训练的人 ...

  4. python opencv 文字识别_文本识别 使用 Tesseract 进行 OpenCV OCR 和 文本识别

    文本识别 用 Tesseract 进行 OpenCV OCR 和 文本识 在本教程中,您将学习如何应用OpenCV OCR(光学字符识别).我们将使用OpenCV,Python和Tesseract 执 ...

  5. Python+OpenCV人脸识别技术详解

    总在科幻电影里看到人脸识别,现在我们也可以编程来实现啦.哈哈~~ OpenCV是Intel®开源计算机视觉库.它由一系列 C 函数和少量 C++ 类构成,实现了图像处理和计算机视觉方面的很多通用算法. ...

  6. Python OpenCV 物体识别

    基于Python OpenCV 隐马尔可夫模型 物体识别,汽车飞机摩托车. 网上自己收集的图片进行识别的,准确率可以达到60% 其中通过star 和sift 特征检测. SIFT算法 来源: 尺度不变 ...

  7. Python+opencv 人脸识别

    python+opencv人脸检测+识别示例及原理解析 一.开发环境搭建 二.图片人脸检测 2.1 文件准备与编程 2.2 注意事项 三.视频人脸识别 3.1文件准备与编程 3.2 注意事项 四.人脸 ...

  8. 基于python opencv人脸识别的员工考勤系统

    WorkAttendanceSystem 一个基于opencv人脸识别的员工考勤系统,作者某双一流A类大学里的一流学生,写于2018/09/,python课设期间. 源代码详细解释请关注微信公众号: ...

  9. 【Python+OpenCV】识别颜色方块并提取轮廓

    前一阵在做机械臂下井字棋的综合设计,在网上直接购买了一套机械臂装置(包括机械臂,摄像头,树莓派,花费1600元),机械臂不是很高级.精度很低.源码里提供识别红绿蓝三种颜色方块的识别和抓取. 经过多次尝 ...

最新文章

  1. 技术图文:进一步完善自动化交易系统 - 02
  2. 视口和窗口的理解方法
  3. linux awk 分组统计
  4. 如何多次读取request请求里的数据
  5. word-break:break-all和word-wrap:break-word的区别
  6. android5.1 显示方向,Android5.1 Settings.apk定制显示选项
  7. linux判断网站被采集,网站被采集的几个处理方法(非技术)
  8. 华为首秀 AI 全栈软件平台!
  9. 软件工程综合实践第二次作业——结对编程
  10. 单片机c语言仿真,单片机c语言教程:C51表达式语句及仿真器
  11. 【爬虫】利用Python爬虫爬取小麦苗itpub博客的所有文章的连接地址(1)
  12. Linux操作系统原理与应用(陈莉君)——学习笔记
  13. Mac 右键展示Copy path
  14. 计算机操作系统——程序执行的流程
  15. URI、URL、URN介绍
  16. 中兴网络设备交换机路由器查看MC-LAG状态检查命令方法
  17. 【1字=16bits的原因,switch汇编详解,跳到中间 jump to middle,guarded-do门卫】
  18. n子棋,你能下赢电脑吗,来玩玩吧
  19. 【第三方API】顺丰API调用总结-java
  20. tomcat配置监控界面

热门文章

  1. oracle主目录修改,ORACLE主目录权限被修改,恢复ORACLE_HOME或GI_HOME权限、属主
  2. 瑞尔森大学的计算机科学专业,瑞尔森大学有哪些专业处于世界顶尖水平?
  3. mybatis可以用oracle,使用MyBatis调用oracle函数(基于注释)
  4. OpenCV中的光流及视频特征点追踪
  5. 深入浅出神经网络原理
  6. MXNET源码中TShape值的获取和打印
  7. 学习《Linux设备模型浅析之设备篇》笔记(深挖一)
  8. JS中编写函数去除HTML标签,js函数获取html中className所在的内容并去除标签
  9. Android替换view父节点,android – issue:指定的子节点已经有父节点.您必须首先在孩子的父母上调用removeView()...
  10. ORB特征原理(浅显易懂)