我使用 opencv python 形式将模拟时钟转换为小时和分钟的数字数据,但我也需要它显示几秒钟

如何解决我使用 opencv python 形式将模拟时钟转换为小时和分钟的数字数据,但我也需要它显示几秒钟

我使用 opencv 读取图像,将其转换为灰度,并使用 canny、kernel、thesh、erode 等找到边缘,我使用 HooughLineP() 检测到图像中的所有线条,并且检测到时针和分针,但我还需要找到秒针,这是我使用的代码

import cv2
import math
import numpy as np
from matplotlib import pyplot as plt
from math import sqrt
from math import acos,degrees


kernel = np.ones((5,5),np.uint8)
img1 = cv2.imread('input1.jpg')
img = cv2.imread('input1.jpg',0)
gray = cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY)

ret,thresh = cv2.threshold(gray,50,255,cv2.THRESH_BINARY)

# Create mask
height,width = img.shape
#height=height-10
#width=width-10
mask = np.zeros((height,width),np.uint8)

edges = cv2.Canny(thresh,100,200)

#cv2.imshow('detected ',gray)
cimg=cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
circles = cv2.HoughCircles(gray,cv2.HOUGH_GRADIENT,1.2,100)
#circles = cv2.HoughCircles(edges,1000,param1 = 50,param2 = 30,minRadius = 20,maxRadius = 0)
for i in circles[0,:]:
    i[2]=i[2]+4
    # Draw on mask
    cv2.circle(mask,(i[0],i[1]),i[2],(255,255),thickness=-1)

# copy that image using that mask
masked_data = cv2.bitwise_and(img1,img1,mask=mask)

# Apply Threshold
_,thresh = cv2.threshold(mask,1,cv2.THRESH_BINARY)
# Find Contour
contours = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
x,y,w,h = cv2.boundingRect(contours[0])

# Crop masked_data
crop = masked_data[y+30:y+h-30,x+30:x+w-30]
i=crop
height,width,channels = i.shape
print (width,height,channels)
#########################################################################

ret,mask = cv2.threshold(i,10,cv2.THRESH_BINARY)
edges = cv2.Canny(i,200)
kernel = np.ones((11,11),np.uint8)
kernel2 = np.ones((13,13),np.uint8)
edges = cv2.dilate(edges,kernel,iterations = 1)
edges = cv2.erode(edges,kernel2,iterations = 1)
minLineLength = 1000
maxLineGap = 10
lines = cv2.houghlinesp(edges,np.pi/180,15,minLineLength,maxLineGap)
h=[]
xmax1=0
xmax2=0
ymax1=0
ymax2=0
xs1=0
xs2=0
ys1=0
ys2=0

for line in lines:
    x1,y1,x2,y2 = line[0]
    #cv2.line(i,(x1,y1),(x2,y2),(0,0),1)
    dx=x2-x1
    if(dx<0):
        dx=dx*-1
    dy=y2-y1
    if(dy<0):
        dy=dy*-1
        
    hypo=sqrt(dx**2 + dy**2)
    
            
    #print("dx=",dx,"  dy=",dy)
    h.append(hypo)

#print(h)
print(len(h))
a=len(h)
h.sort(reverse=True)
#print(h)
m=0
k=0

for f in range(a):
    for line in lines:
        x1,y2 = line[0]
        #cv2.line(i,3)
        dx=x2-x1
        if(dx<0):
            dx=dx*-1
        dy=y2-y1
        if(dy<0):
            dy=dy*-1

        hypo2=sqrt(dx**2 + dy**2)


        if(hypo2==h[0]):
            m=hypo2
            xmax1=x1
            xmax2=x2
            ymax1=y1
            ymax2=y2
            cv2.line(crop,(xmax1,ymax1),(xmax2,ymax2),3)
            #print("xmax1=",xmax1," ymax1=",ymax1," xmax2=",xmax2," ymax2=",ymax2)

        if(m==h[0]): 
            if(hypo2==h[f]):
                if((sqrt((xmax2-x2)**2 + (ymax2-y2)**2))>20):
                    if((sqrt((xmax1-x1)**2 + (ymax1-y1)**2))>20):
                        xs1=x1
                        xs2=x2
                        ys1=y1
                        ys2=y2
                        cv2.line(crop,(xs1,ys1),(xs2,ys2),3)
                        print("xs1=",xs1," ys1=",ys1," xs2=",xs2," ys2=",ys2)
                        k=1
                        break
    if(k==1):                
        break           

print("xmax1=",ymax2)

我在上面的代码行中将分针和时针分开,但我也需要分开秒针,请任何人帮助我!

Click here for the sample input image

解决方法

基于这篇文章:How to detect lines in OpenCV? 我已经适应了你的图像和你的裁剪方法,它给出了给定图像的有效输出:

import cv2
import numpy as np
from matplotlib import pyplot as plt


kernel = np.ones((5,5),np.uint8)
img1 = cv2.imread('clock.jpg')
img = cv2.imread('clock.jpg',0)
gray = cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY)

ret,thresh = cv2.threshold(gray,50,255,cv2.THRESH_BINARY)

# Create mask
height,width = img.shape
mask = np.zeros((height,width),np.uint8)
edges = cv2.Canny(thresh,100,200)

#cv2.imshow('detected ',gray)
cimg=cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
circles = cv2.HoughCircles(gray,cv2.HOUGH_GRADIENT,1.2,100)
for i in circles[0,:]:
    i[2]=i[2]+4
    # Draw on mask
    cv2.circle(mask,(i[0],i[1]),i[2],(255,255),thickness=-1)

# Copy that image using that mask
masked_data = cv2.bitwise_and(img1,img1,mask=mask)

# Apply Threshold
_,thresh = cv2.threshold(mask,1,cv2.THRESH_BINARY)
# Find Contour
contours,hierarchy = 
cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
x,y,w,h = cv2.boundingRect(contours[0])

# Crop masked_data
crop = masked_data[y+30:y+h-30,x+30:x+w-30]


################################
kernel_size = 5
blur_crop = cv2.GaussianBlur(crop,(kernel_size,kernel_size),0)
low_threshold = 50
high_threshold = 150
edges = cv2.Canny(blur_crop,low_threshold,high_threshold)

rho = 1                     # distance resolution in pixels
theta = np.pi / 180         # angular resolution in radians
threshold = 15              # minimum number of votes 
min_line_length = 100       # minimum number of pixels making up a line
max_line_gap = 10           # maximum gap in pixels between connectable 
line segments
line_image = np.copy(crop) * 0 

# Run Hough on edge detected image
# Output "lines" is an array containing endpoints of detected line
lines = cv2.HoughLinesP(edges,rho,theta,threshold,np.array([]),min_line_length,max_line_gap)

for line in lines:
    for x1,y1,x2,y2 in line:
        cv2.line(line_image,(x1,y1),(x2,y2),0),5)

# Draw the lines on the  image
lines_edges = cv2.addWeighted(crop,0.8,line_image,0)

cv2.imshow('line_image',line_image)
cv2.imshow('crop',crop)

通过对 Hough 检测进行一些参数调整,您应该能够将结果减少到 3 条漂亮的线。 enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?