如何使用Python和OpenCV读取此模拟量表?

发布于 2025-02-14 01:01:46 字数 2193 浏览 3 评论 0 原文

我想使用python和openCV读取此模拟量表,这是我的起点图像:

“

实际上,有了一些模糊和轮廓检测,我能够将其清除一点并绘制一些重要部分:

“

这是我的实际代码:

import os
import glob
import cv2                       # to run code even if version changes
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
img = cv2.imread('/home/administrator/Downloads/read_analogue_gauge_v1_1/images/gaugemio.jpg')
def rescaleFrame(frame, scale=0.5):
        width = int(frame.shape[1]*scale)
        height = int(frame.shape[0]*scale)
        dimensions = (width, height)
        return cv2.resize(frame, dimensions, interpolation=cv2.INTER_AREA)
resized_image = rescaleFrame(img)
img = resized_image
img_hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
img_hsv = cv2.bitwise_not(img_hsv)  # Convert to HSV space
lower_black, upper_black = np.array([0, 0, 0]), np.array([180, 255, 65])
mask = cv2.inRange(img_hsv, lower_black, upper_black)

blank = np.zeros((747, 714), dtype='uint8')
blank2 = np.zeros((747, 714), dtype='uint8')
blurred = cv2.blur(~mask, (9, 9))

circles = cv2.HoughCircles(blurred, cv2.HOUGH_GRADIENT,1, 400, param1=50, param2=100, minRadius=200, maxRadius=0)
detected_circles = np.uint16(np.around(circles))
dimension = img.shape

for(x, y, r) in detected_circles[0, :]:
    circle = cv2.circle(blank, (x,y),r, 255, -1 )
    cv2.circle(img, (x,y),r, (0,255,0),3)
    cv2.circle(img, (x,y),2, (0,0,255),3)
    cv2.circle(img, (x,y),30, (0,0,255),3)
    circ_col = x
    circ_row = y
bitwise_and = cv2.bitwise_and(blank, mask)
canny2 = cv2.Canny(bitwise_and, 50, 150, apertureSize=3)
blurred2 = cv2.blur(bitwise_and, (15, 15))
contours, hierarchy = cv2.findContours(blurred2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img, contours, -1, (0, 255, 0), 2)
cv2.imshow('hi',resized_image)

cv2.waitKey(0)

有技巧吗?

I want to read this analog gauge using Python and OpenCV, here is my starting point image:

1

Actually, with some blur and contour detection I was able to clear it a little and draw some important parts:

2

Here is my actual code:

import os
import glob
import cv2                       # to run code even if version changes
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
img = cv2.imread('/home/administrator/Downloads/read_analogue_gauge_v1_1/images/gaugemio.jpg')
def rescaleFrame(frame, scale=0.5):
        width = int(frame.shape[1]*scale)
        height = int(frame.shape[0]*scale)
        dimensions = (width, height)
        return cv2.resize(frame, dimensions, interpolation=cv2.INTER_AREA)
resized_image = rescaleFrame(img)
img = resized_image
img_hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
img_hsv = cv2.bitwise_not(img_hsv)  # Convert to HSV space
lower_black, upper_black = np.array([0, 0, 0]), np.array([180, 255, 65])
mask = cv2.inRange(img_hsv, lower_black, upper_black)

blank = np.zeros((747, 714), dtype='uint8')
blank2 = np.zeros((747, 714), dtype='uint8')
blurred = cv2.blur(~mask, (9, 9))

circles = cv2.HoughCircles(blurred, cv2.HOUGH_GRADIENT,1, 400, param1=50, param2=100, minRadius=200, maxRadius=0)
detected_circles = np.uint16(np.around(circles))
dimension = img.shape

for(x, y, r) in detected_circles[0, :]:
    circle = cv2.circle(blank, (x,y),r, 255, -1 )
    cv2.circle(img, (x,y),r, (0,255,0),3)
    cv2.circle(img, (x,y),2, (0,0,255),3)
    cv2.circle(img, (x,y),30, (0,0,255),3)
    circ_col = x
    circ_row = y
bitwise_and = cv2.bitwise_and(blank, mask)
canny2 = cv2.Canny(bitwise_and, 50, 150, apertureSize=3)
blurred2 = cv2.blur(bitwise_and, (15, 15))
contours, hierarchy = cv2.findContours(blurred2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img, contours, -1, (0, 255, 0), 2)
cv2.imshow('hi',resized_image)

cv2.waitKey(0)

Any tips?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文