基於大小相似,擺放規整的水表圖片的 數字表盤的識別
識別效果:
# -*- coding: utf-8 -*- """ Created on Tue Sep 17 19:00:45 2019 @author: xxr """ from cv2 import cv2 #因為cv2里面還有cv2 所以要這樣干! import numpy as np #讀取原始圖片 image= cv2.imread('shuibiao1.jpg') #圖片的縮小,記住比例 的縮放 r = 500.0 / image.shape[1] dim = (500, int(image.shape[0] * r)) image = cv2.resize(image, dim, interpolation = cv2.INTER_AREA) #圖像灰度化處理 grayImage = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) #基於Canny的邊沿檢測(生成的也是二值圖) canny=cv2.Canny(grayImage,30,180) cv2.imshow('canny', canny) # 運用OpenCV findContours函數檢測圖像所有輪廓 contours, hierarchy = cv2.findContours(canny,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_SIMPLE) # 對於檢測出的輪廓,contourArea限制輪廓所包圍的面積的大小,boundingRect識別出正矩形,通過矩形的寬度和高度篩選出想要的圖片區域。 # count=0 # minx=10000 # miny=10000 # height=0 # weight=0 for cnt in contours: if cv2.contourArea(cnt)>800: #篩選出面積大於30的輪廓 [x,y,w,h] = cv2.boundingRect(cnt) #x,y是左上角的坐標,h,w是高和寬 if h > 28 and h < 50: # 根據期望獲取區域,即數字區域的實際高度預估28至50之間 # if(minx>x): # minx=x # if(miny>y): # miny=y # count+=1 # height+=h # weight+=w cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),2) # #截取特定區域 # imagehh = image[miny:miny+height,minx:minx+weight] cv2.imshow("hh",image) # print(str(count)+"jjjjjjjj") cv2.waitKey(0) cv2.destroyAllWindows()
對水表圖片進行的hough(霍夫)直線檢測
1 # -*- coding: utf-8 -*- 2 """ 3 Created on Tue Sep 17 19:00:45 2019 4 5 @author: xxr 6 """ 7 from cv2 import cv2 #因為cv2里面還有cv2 所以要這樣干! 8 import numpy as np 9 10 #讀取原始圖片 11 image= cv2.imread('shuibiao.jpg') 12 13 #讀入一張白色的圖 14 image2=cv2.imread('white.png') 15 image3=image2 16 17 #圖片的縮小,記住比例 的縮放 18 r = 500.0 / image.shape[1] 19 dim = (500, int(image.shape[0] * r)) 20 image = cv2.resize(image, dim, interpolation = cv2.INTER_AREA) 21 image2=cv2.resize(image2, dim, interpolation = cv2.INTER_AREA) 22 23 #圖像灰度化處理 24 grayImage = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) 25 #基於Canny的邊沿檢測(生成的也是二值圖) 26 canny=cv2.Canny(grayImage,30,180) 27 #cv2.imshow("canny_image",canny) 28 #再canny處理圖像以后 用hough直線檢測 29 #統計概率霍夫線變換 30 31 32 # 步長 閾值 最小直線長度 最大構成直線的點的間隔 33 lines = cv2.HoughLinesP(canny, 1, np.pi / 180, 60, minLineLength=30, maxLineGap=8) 34 for line in lines: 35 x1, y1, x2, y2 = line[0] 36 cv2.line(image2, (x1, y1), (x2, y2), (0, 0, 255), 1) 37 38 39 40 #這里進行矩形的輪廓檢測 !!!應該找 白色的矩形框 41 image2 = cv2.cvtColor(image2,cv2.COLOR_BGR2GRAY) 42 #黑白顏色顛倒 43 height, width = image2.shape 44 dst = np.zeros((height,width,1), np.uint8) 45 for i in range(0, height): 46 for j in range(0, width): 47 grayPixel = image2[i, j] 48 dst[i, j] = 255-grayPixel 49 50 51 #高斯濾波(使圖像模糊,平滑) 52 #dst=cv2.GaussianBlur(dst,(7,7),0) 53 54 cv2.imshow('dst', dst) 55 contours, hierarchy = cv2.findContours(dst,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_SIMPLE) 56 57 # #繪制直線 58 # max=0 59 # max_i=0 60 # print(len(contours[2])) 61 # for i in range(len(contours)): 62 # if(5==len(contours[i])): 63 # max_i=i 64 # cv2.drawContours(image3,contours,-1,(0,255,255),3) 65 # cv2.imshow("draw_img0", image3) 66 67 # #繪制矩形 68 # for i in range(0,len(contours)): 69 # x, y, w, h = cv2.boundingRect(contours[i]) 70 # cv2.rectangle(image3, (x,y), (x+w,y+h), (153,153,0), 5) 71 #cv2.imshow("draw_img0", image3) 72 73 #打印矩形 74 # for i in range(0,len(contours)): 75 # x, y, w, h = cv2.boundingRect(contours[i]) 76 # print(contours[0]) 77 # cv2.rectangle(image3, (x,y), (x+w,y+h), (255,153,0), 5) 78 79 #標准霍夫線變換(但在這里不太實用) 80 # lines = cv2.HoughLines(canny, 1, np.pi/180, 150) 81 # for line in lines: 82 # rho, theta = line[0] #line[0]存儲的是點到直線的極徑和極角,其中極角是弧度表示的。 83 # a = np.cos(theta) #theta是弧度 84 # b = np.sin(theta) 85 # x0 = a * rho #代表x = r * cos(theta) 86 # y0 = b * rho #代表y = r * sin(theta) 87 # x1 = int(x0 + 1000 * (-b)) #計算直線起點橫坐標 88 # y1 = int(y0 + 1000 * a) #計算起始起點縱坐標 89 # x2 = int(x0 - 1000 * (-b)) #計算直線終點橫坐標 90 # y2 = int(y0 - 1000 * a) #計算直線終點縱坐標 注:這里的數值1000給出了畫出的線段長度范圍大小,數值越小,畫出的線段越短,數值越大,畫出的線段越長 91 # cv2.line(image, (x1, y1), (x2, y2), (0, 0, 255), 2) #點的坐標必須是元組,不能是列表。 92 # cv2.imshow("image-lines", image) 93 94 # 二值化圖片 95 # #自適應閾值化能夠根據圖像不同區域亮度分布,改變閾值 96 # #threshold_pic = cv2.adaptiveThreshold(grayImage, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY, 25, 10) 97 # ret,threshold_pic=cv2.threshold(grayImage,127, 255, cv2.THRESH_BINARY) 98 # cv2.imshow("threshold_image",threshold_pic) 99 #等待顯示(不添加這兩行將會報錯) 100 cv2.waitKey(0) 101 cv2.destroyAllWindows()
# -*- coding: utf-8 -*-
"""
Created on Tue Sep 17 19:00:45 2019
@author: xxr
"""
from cv2
import cv2
#因為cv2里面還有cv2 所以要這樣干!
import numpy
as np
#讀取原始圖片
image= cv2.imread(
'shuibiao.jpg')
#讀入一張白色的圖
image2=cv2.imread(
'white.png')
image3=image2
#圖片的縮小,記住比例 的縮放
r =
500.0 / image.shape[
1]
dim = (
500,
int(image.shape[
0] * r))
image = cv2.resize(image, dim,
interpolation = cv2.INTER_AREA)
image2=cv2.resize(image2, dim,
interpolation = cv2.INTER_AREA)
#圖像灰度化處理
grayImage = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
#基於Canny的邊沿檢測(生成的也是二值圖)
canny=cv2.Canny(grayImage,
30,
180)
#cv2.imshow("canny_image",canny)
#再canny處理圖像以后 用hough直線檢測
#統計概率霍夫線變換
# 步長 閾值 最小直線長度 最大構成直線的點的間隔
lines = cv2.HoughLinesP(canny,
1, np.pi /
180,
60,
minLineLength=
30,
maxLineGap=
8)
for line
in lines:
x1, y1, x2, y2 = line[
0]
cv2.line(image2, (x1, y1), (x2, y2), (
0,
0,
255),
1)
#這里進行矩形的輪廓檢測 !!!應該找 白色的矩形框
image2 = cv2.cvtColor(image2,cv2.COLOR_BGR2GRAY)
#黑白顏色顛倒
height, width = image2.shape
dst = np.zeros((height,width,
1), np.uint8)
for i
in
range(
0, height):
for j
in
range(
0, width):
grayPixel = image2[i, j]
dst[i, j] =
255-grayPixel
#高斯濾波(使圖像模糊,平滑)
#dst=cv2.GaussianBlur(dst,(7,7),0)
cv2.imshow(
'dst', dst)
contours, hierarchy = cv2.findContours(dst,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_SIMPLE)
# #繪制直線
# max=0
# max_i=0
# print(len(contours[2]))
# for i in range(len(contours)):
# if(5==len(contours[i])):
# max_i=i
# cv2.drawContours(image3,contours,-1,(0,255,255),3)
# cv2.imshow("draw_img0", image3)
# #繪制矩形
# for i in range(0,len(contours)):
# x, y, w, h = cv2.boundingRect(contours[i])
# cv2.rectangle(image3, (x,y), (x+w,y+h), (153,153,0), 5)
#cv2.imshow("draw_img0", image3)
#打印矩形
# for i in range(0,len(contours)):
# x, y, w, h = cv2.boundingRect(contours[i])
# print(contours[0])
# cv2.rectangle(image3, (x,y), (x+w,y+h), (255,153,0), 5)
#標准霍夫線變換(但在這里不太實用)
# lines = cv2.HoughLines(canny, 1, np.pi/180, 150)
# for line in lines:
# rho, theta = line[0] #line[0]存儲的是點到直線的極徑和極角,其中極角是弧度表示的。
# a = np.cos(theta) #theta是弧度
# b = np.sin(theta)
# x0 = a * rho #代表x = r * cos(theta)
# y0 = b * rho #代表y = r * sin(theta)
# x1 = int(x0 + 1000 * (-b)) #計算直線起點橫坐標
# y1 = int(y0 + 1000 * a) #計算起始起點縱坐標
# x2 = int(x0 - 1000 * (-b)) #計算直線終點橫坐標
# y2 = int(y0 - 1000 * a) #計算直線終點縱坐標 注:這里的數值1000給出了畫出的線段長度范圍大小,數值越小,畫出的線段越短,數值越大,畫出的線段越長
# cv2.line(image, (x1, y1), (x2, y2), (0, 0, 255), 2) #點的坐標必須是元組,不能是列表。
# cv2.imshow("image-lines", image)
# 二值化圖片
# #自適應閾值化能夠根據圖像不同區域亮度分布,改變閾值
# #threshold_pic = cv2.adaptiveThreshold(grayImage, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY, 25, 10)
# ret,threshold_pic=cv2.threshold(grayImage,127, 255, cv2.THRESH_BINARY)
# cv2.imshow("threshold_image",threshold_pic)
#等待顯示(不添加這兩行將會報錯)
cv2.waitKey(
0)
cv2.destroyAllWindows()