前言
一、鼠標(biāo)點(diǎn)擊的角度測量
首先導(dǎo)入一個帶有角度的照片
然后下面的代碼注冊了一個鼠標(biāo)按下的回調(diào)函數(shù),
還有一個點(diǎn)的數(shù)列,鼠標(biāo)事件為按下的時候就記錄點(diǎn),并畫出點(diǎn),由于點(diǎn)是畫在圖像上面的,那么就要求了img是需要刷新的所以將他們放在while True里面
當(dāng)有按鍵按下的的時候就把圖片歸為原來的以及清除列表的值。
按鍵的使用可以看這個文章
簡述
cv2.waitKey(1)在有按鍵按下的時候返回按鍵的ASCII值,否則返回-1
& 0xFF的按位與操作只取cv2.waitKey(1)返回值最后八位,因?yàn)橛行┫到y(tǒng)cv2.waitKey(1)的返回值不止八位
ord(‘q’)表示q的ASCII值
總體效果:按下q鍵后break
import cv2
import math
img = cv2.imread('jiaodu.png')
pointslist = []
def mousepoints(event,x,y,flags,params):
if event == cv2.EVENT_LBUTTONDOWN:
cv2.circle(img,(x,y),5,(0,0,255),cv2.FILLED)
pointslist.append([x,y])
print(pointslist)
while True:
cv2.imshow('image',img)
cv2.setMouseCallback('image',mousepoints)
keycc = cv2.waitKey(1) & 0xFF
if keycc == ord('q'):
pointslist = []
img = cv2.imread('jiaodu.png')
if keycc == ord('c'):
exit()
當(dāng)在圖像上點(diǎn)擊的時候有紅點(diǎn),當(dāng)按下q的時候清除,當(dāng)按下c的時候退出
接著我們要做的就是每點(diǎn)擊一下就會出現(xiàn)三角的邊,我們用size來做判斷,當(dāng)取余后值為2的時候就說明要畫一個直線,當(dāng)按下第三個點(diǎn)的時候要是第二條直線,那么就elif size % 3 == 0 and size != 0:
就可以得到啦
還是用了tuple()
函數(shù)因?yàn)?cv2.line 需要點(diǎn)的坐標(biāo)作為元組,所以我們使用 tuple 函數(shù)將列表轉(zhuǎn)換為元組。
使用到了畫線函數(shù)cv2.line
可以參考這篇文章
簡述:
cv2.line(img, pt1, pt2, color[, thickness[, lineType[, shift]]]) → img
img,背景圖
pt1,直線起點(diǎn)坐標(biāo)
pt2,直線終點(diǎn)坐標(biāo)
color,當(dāng)前繪畫的顏色。如在BGR模式下,傳遞(255,0,0)表示藍(lán)色畫筆?;叶葓D下,只需要傳遞亮度值即可。
thickness,畫筆的粗細(xì),線寬。若是-1表示畫封閉圖像,如填充的圓。默認(rèn)值是1.
lineType,線條的類型,
如8-connected類型、anti-aliased線條(反鋸齒),默認(rèn)情況下是8-connected樣式ide,cv2.LINE_AA表示反鋸齒線條,在曲線的時候視覺效果更佳。
def mousepoints(event,x,y,flags,params):
if event == cv2.EVENT_LBUTTONDOWN:
cv2.circle(img,(x,y),5,(0,0,255),cv2.FILLED)
pointslist.append([x,y])
size = len(pointslist)
if size % 3 == 2:
cv2.line(img, tuple(pointslist[-2]), tuple(pointslist[-1]), (255, 0, 0), 2)
# 當(dāng)添加第三個點(diǎn)時,使用第一個點(diǎn)和第三個點(diǎn)繪制線
elif size % 3 == 0 and size != 0:
cv2.line(img, tuple(pointslist[-3]), tuple(pointslist[-1]), (255, 0, 0), 2)
print(pointslist)
現(xiàn)在我們還需要計(jì)算出,直接使用math中的函數(shù),不過,我們需要由梯度來求,具體公式百度,最終的代碼:
import cv2
import math
img = cv2.imread('jiaodu.png')
pointslist = []
def mousepoints(event,x,y,flags,params):
if event == cv2.EVENT_LBUTTONDOWN:
cv2.circle(img,(x,y),5,(0,0,255),cv2.FILLED)
pointslist.append([x,y])
size = len(pointslist)
if size % 3 == 2:
cv2.line(img, tuple(pointslist[-2]), tuple(pointslist[-1]), (255, 0, 0), 2)
# 當(dāng)添加第三個點(diǎn)時,使用第一個點(diǎn)和第三個點(diǎn)繪制線
elif size % 3 == 0 and size != 0:
cv2.line(img, tuple(pointslist[-3]), tuple(pointslist[-1]), (255, 0, 0), 2)
print(pointslist)
def gradient(pt1,pt2):
return (pt2[1]-pt1[1])/(pt2[0]-pt1[0])
def getAngle(pointslist):
pt1,pt2,pt3 = pointslist[-3:]
m1 = gradient(pt1,pt2)
m2 = gradient(pt1,pt3)
angr = math.atan((m2 - m1)/(1+(m1 * m2)))
angd = round(math.degrees(angr))
cv2.putText(img,str(angd),(pt1[0]-40,pt1[1]),cv2.FONT_HERSHEY_SIMPLEX,1,(255,0,0),1)
print(angd)
while True:
if len(pointslist) % 3 == 0 and len(pointslist) != 0:
getAngle(pointslist)
cv2.imshow('image',img)
cv2.setMouseCallback('image',mousepoints)
keycc = cv2.waitKey(1) & 0xFF
if keycc == ord('q') or keycc == ord('Q') :
pointslist = []
img = cv2.imread('jiaodu.png')
if keycc == ord('c') or keycc == ord('C') :
exit()
二、二維碼條形碼識別
需要安裝庫numpy
和pyzbar
numpy是anaconda自帶的,我們在終端輸入 pip install pyzbar
就可以進(jìn)行下載了。
我們首先有一張有二維碼的圖片
import cv2
import numpy as np
from pyzbar.pyzbar import decode
img = cv2.imread('qr.png')
code = decode(img)
print(code)
我們可以讀取到圖片中二維碼的信息
[Decoded(data=b’111111’, type=‘QRCODE’, rect=Rect(left=182, top=381,
width=163, height=164), polygon=[Point(x=182, y=381), Point(x=182, y=545), Point(x=345, y=545), Point(x=345, y=381)], quality=1, orie ntation=‘UP’)]
其中data就是我們要的數(shù)據(jù),而polygon是邊框點(diǎn)
我們只打印出二維碼的數(shù)據(jù)信息
import cv2
import numpy as np
from pyzbar.pyzbar import decode
img = cv2.imread('qr.png')
for barcode in decode(img):
print(barcode.data)
其中的b是編碼
import cv2
import numpy as np
from pyzbar.pyzbar import decode
img = cv2.imread('qr.png')
for barcode in decode(img):
mydata = barcode.data.decode('utf-8')
print(mydata)
這樣就可以只得到數(shù)據(jù)信息。
現(xiàn)在我們調(diào)用攝像頭cv2.VideoCapture(0)
還需要在二維碼周圍畫框cv2.polylines
,在周圍顯示 cv2.putText
二維碼的數(shù)據(jù)。文章來源:http://www.zghlxwxcb.cn/news/detail-572730.html
NumPy 和 OpenCV 的用法。
np.array():
NumPy 的 array 函數(shù)用于創(chuàng)建一個數(shù)組。在這里,barcode.polygon
被轉(zhuǎn)換成一個 NumPy 數(shù)組。[barcode.polygon]:
這個語法是創(chuàng)建一個包含barcode.polygon
的列表。這在創(chuàng)建二維或者更高維度的數(shù)組時是常見的做法。np.int32:
這是一個參數(shù),指定了新創(chuàng)建的數(shù)組的數(shù)據(jù)類型應(yīng)該是 32 位整數(shù)。在圖像處理中,像素的坐標(biāo)通常是整數(shù)。pts:
這是一個變量,用于保存新創(chuàng)建的數(shù)組。
所以,pts = np.array([barcode.polygon], np.int32)
這句話的作用是創(chuàng)建一個新的NumPy
數(shù)組,數(shù)組的元素來自barcode.polygon
,并且這個數(shù)組的數(shù)據(jù)類型是 32 位整數(shù)。這通常用于處理圖像,比如 OpenCV 的多邊形(如輪廓等)。文章來源地址http://www.zghlxwxcb.cn/news/detail-572730.html
import cv2
import numpy as np
from pyzbar.pyzbar import decode
cap = cv2.VideoCapture(0)
while True:
success,img = cap.read()
# img = cv2.imread('qr.png')
for barcode in decode(img):
mydata = barcode.data.decode('utf-8')
print(mydata)
pts = np.array([barcode.polygon],np.int32)
pts = pts.reshape((-1,1,2))
cv2.polylines(img,[pts],True,(255,0,255),5)
pts2 = barcode.rect
cv2.putText(img,mydata,(pts2[0],pts2[1]),cv2.FONT_HERSHEY_SIMPLEX,0.9,(255,0,255),2)
cv2.imshow('result',img)
keycc = cv2.waitKey(1)
if keycc == ord('c') or keycc == ord('C') :
exit()
到了這里,關(guān)于opencv實(shí)戰(zhàn)--角度測量和二維碼條形碼識別的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!