import cv2 as cv
import numpy as np
import math
def findColor(src, min, max):
'''
描述:用于判斷顏色識別基于HSV模式
:param src:
:param min:
:param max:
:return:
'''
src_image = cv.GaussianBlur(src, (9, 9), 0)
hsv_image = cv.cvtColor(src_image, cv.COLOR_BGR2HSV)
thresholded = cv.inRange(hsv_image, (min, 90, 90), (max, 255, 255))
return thresholded
def angle(pt1, pt2, pt0):
'''
描述:用于求證正方形
:param pt1:
:param pt2:
:param pt0:
:return:
'''
dx1 = pt1[0][0] - pt0[0][0]
dy1 = pt1[0][1] - pt0[0][1]
dx2 = pt2[0][0] - pt0[0][0]
dy2 = pt2[0][1] - pt0[0][1]
# 邊長平方的比
ratio = (dx1 * dx1 + dy1 * dy1) / (dx2 * dx2 + dy2 * dy2)
a = (dx1 * dx2 + dy1 * dy2) / math.sqrt((dx1 * dx1 + dy1 * dy1) * (dx2 * dx2 + dy2 * dy2) + 1e-10)
# 根據邊長平方的比過小或過大提前淘汰這個四邊形,如果淘汰過多,調整此比例數字
if ratio < 0.8 or 1.2 < ratio:
# 根據邊長平方的比過小或過大提前淘汰這個四邊形
return 1.0
return a
def Graphicdetection(srcImage, CLImage, outImage, str):
'''
描述:用于識別和統計形狀數量
:param srcImage:
:param CLImage:
:param outImage:
:param str:
:return:
'''
bjImage = srcImage.copy()
# 模糊降噪
cv.GaussianBlur(CLImage, (9, 9), 0, CLImage, 0)
# 邊緣檢測
outImage = cv.Canny(CLImage, 10, 100)
# 定義一個正方形計數器
zfcount = 0
# 定義一個三角形計數器
sjcount = 0
# 查找輪廓
cloneImage,contours, hierarchy = cv.findContours(outImage, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_NONE)
for i in range(len(contours)):
# 計算輪廓矩
mu = cv.moments(contours[i], False)
# 計算輪廓的質心
cx = mu['m10'] / mu['m00']
cy = mu['m01'] / mu['m00']
# 畫出中心點
cv.circle(bjImage, (np.int(cx), np.int(cy)), 5, (0, 0, 0), -1, 8, 0)
# 繪出多邊形
approx = cv.approxPolyDP(contours[i], cv.arcLength(contours[i], True) * 0.02, True)
if len(approx) == 4 & np.int(math.fabs(cv.contourArea(approx))) > 1000 & cv.isContourConvex(approx):
MaxCosine = 0
for j in range(2, 5):
# 正方形
cosine = math.fabs(angle(approx[j % 4], approx[j - 2], approx[j - 1]))
MaxCosine = np.maximum(MaxCosine, cosine)
if MaxCosine < 0.3:
jx = approx
# 繪制出正方形邊框
cv.line(bjImage, (jx[0][0][0], jx[0][0][1]), (jx[1][0][0], jx[1][0][1]), (0, 0, 0), 3)
cv.line(bjImage, (jx[1][0][0], jx[1][0][1]), (jx[2][0][0], jx[2][0][1]), (0, 0, 0), 3)
cv.line(bjImage, (jx[2][0][0], jx[2][0][1]), (jx[3][0][0], jx[3][0][1]), (0, 0, 0), 3)
cv.line(bjImage, (jx[3][0][0], jx[3][0][1]), (jx[0][0][0], jx[0][0][1]), (0, 0, 0), 3)
tam = "Square " + str
cv.putText(bjImage, tam, (np.int(cx), np.int(cy) - 10), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0))
zfcount += 1
# 三邊形
if len(approx) == 3:
sjx = approx
# 繪制出三角形邊框
cv.line(bjImage, (sjx[0][0][0], sjx[0][0][1]), (sjx[1][0][0], sjx[1][0][1]), (0, 0, 0), 3)
cv.line(bjImage, (sjx[1][0][0], sjx[1][0][1]), (sjx[2][0][0], sjx[2][0][1]), (0, 0, 0), 3)
cv.line(bjImage, (sjx[2][0][0], sjx[2][0][1]), (sjx[0][0][0], sjx[0][0][1]), (0, 0, 0), 3)
tam = "Triangle " + str
cv.putText(bjImage, tam, (np.int(cx), np.int(cy) - 10), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0))
sjcount += 1
# 霍夫檢測圓
circles = None
# 霍夫圓檢測
circles = cv.HoughCircles(CLImage, cv.HOUGH_GRADIENT, 1.5, 10, circles, 200, 100, 0, 0)
circles = np.uint16(np.around(circles))
for i in circles[0, :]:
radius = (i[2]);
tam = "Round " + str
cv.putText(bjImage, tam, (i[0], i[1] - 10), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0))
cv.circle(bjImage, (i[0], i[1]), 3, (0, 0, 0), -1, 8, 0)
# 繪制圓輪廓
cv.circle(bjImage, (i[0], i[1]), radius, (0, 0, 0), 3, 8, 0)
if str == "R":
str = "紅色"
print("%s 正方形 有 %d 個" % (str, zfcount))
print("%s 三角形 有 %d 個" % (str, sjcount))
print("%s 圓 有 %d 個" % (str, len(circles)))
if str == "G":
str = "綠色"
print("%s 正方形 有 %d 個" % (str, zfcount))
print("%s 三角形 有 %d 個" % (str, sjcount))
print("%s 圓 有 %d 個" % (str, len(circles)))
if str == "Y":
str = "黃色"
print("%s 正方形 有 %d 個" % (str, zfcount))
print("%s 三角形 有 %d 個" % (str, sjcount))
print("%s 圓 有 %d 個" % (str, len(circles)))
return bjImage
src = cv.imread("1.jpg")
dst = src.copy()
Rdst = findColor(src, 0, 25)
Gdst = findColor(src, 60, 80)
Ydst = findColor(src, 30, 50)[code=python]
RdstImage = Graphicdetection(src, Rdst, dst, "R")
GdstImage = Graphicdetection(src, Gdst, dst, "G")
YdstImage = Graphicdetection(src, Ydst, dst, "Y")
cv.imshow("Rdst", RdstImage)
cv.imshow("Gdst", GdstImage)
cv.imshow("Ydst", YdstImage)
cv.waitKey(0)
cv.destroyAllWindows()[/code]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/195592.html
標籤:OpenCV
上一篇:qtcreator5.2.0 QGraphicsView圖片顯示不成功
下一篇:測驗用例
