载请注明出处,联系我: t39q@163.com
本人热衷于数据库技术及算法的研究,志同道合之士, 欢迎探讨
import cv2
import numpy as np
#img = cv2.imread('101.png')
#img = cv2.imread('7.png')
#img = cv2.imread('84.png')
#img = cv2.imread('55.png')
#img = cv2.imread('20.png')
#img = cv2.imread('8.jpg')
img = cv2.imread('cnts_test.jpg')
#img = cv2.imread('321.png')
#img = cv2.imread('48.png')
img2=img
Gauss = cv2.GaussianBlur(img, (7, 7),0)
#img_gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
#cv2.imshow('Battery', img)
#print (30)
HSV = cv2.cvtColor(Gauss, cv2.COLOR_BGR2HSV)
H, S, V = cv2.split(HSV)
#battery color
#LowerBlue = np.array([0, 100, 50])
#UpperBlue = np.array([11, 255, 255])
#LowerBlue = np.array([55, 240, 240])
#UpperBlue = np.array([60, 255, 255])
LowerBlue = np.array([13, 0, 0])
UpperBlue = np.array([30, 255, 255])
#cyan paper color
#LowerBlue = np.array([80, 85, 100])
#UpperBlue = np.array([90, 175, 255])
mask = cv2.inRange(HSV, LowerBlue, UpperBlue)
Things = cv2.bitwise_and(img, img, mask=mask)
#kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(3, 3))
erode_hsv = cv2.erode(Things, None, iterations=0)
#cv2.imshow('erode_hsv',erode_hsv)
ThingsGray=cv2.cvtColor(erode_hsv,cv2.COLOR_RGB2GRAY)
#cv2.imshow('ThingsGray',ThingsGray)
cnts = cv2.findContours(ThingsGray.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
#cv2.drawContours(img,cnts,-1,(0,0,255),3)
#isinside=cv2.pointPolygonTest(cnts[154],(500,500),False)
#print ('isinside:'+str(isinside))
for i in range(len(cnts)):
#rect = cv2.minAreaRect(cnts[i])
rect = cv2.minAreaRect(cnts[i])
(x, y), radius = cv2.minEnclosingCircle(cnts[i])
center = (int(x), int(y))
radius = int(radius)
#print (rect[1][0]*rect[1][1])
box = cv2.boxPoints(rect)
if rect[1][0]*rect[1][1]>100:
cv2.drawContours(img, [np.int0(box)], -1, (0, 255, 0), 6)
#cv2.circle(img, center, radius, (0, 0, 255), 6)
print (rect[1][0]*rect[1][1])
print (i)
print (rect[1][0],rect[1][1])
cv2.circle(img, (800,500), 10, (0, 0, 255), 6)
isinside=cv2.pointPolygonTest(cnts[i],(800,500),False)
print ('isinside:'+str(isinside))
cv2.imshow('Bursh', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
"""
#print (cnts)
#print (cv2.area(cnts))
#print (cnts[0])
if len(cnts)>0:
c = max(cnts, key=cv2.contourArea)
rect = cv2.minAreaRect(c)
box = cv2.boxPoints(rect)
if rect[1][0]*rect[1][1]>100000: #calc area to judge if have brush
print ("OK")
print (rect[1][0]*rect[1][1])
#cv2.drawContours(img, [np.int0(box)], -1, (0, 255, 0), 6)
font = cv2.FONT_HERSHEY_SIMPLEX
#print (img.shape[0]/2)
#img = cv2.putText(img, 'OK', (int(img.shape[1]/2)-80, int(img.shape[0]/2)), font, 6, (0, 255, 0), 8)
#cv2.imwrite('cnts_test.jpg',img)
cv2.imshow('Bursh', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
else:
print ("NG")
print (rect[1][0]*rect[1][1])
font = cv2.FONT_HERSHEY_SIMPLEX
#print (img.shape[0]/2)
#img = cv2.putText(img, 'NG', (int(img.shape[1]/2)-80, int(img.shape[0]/2)), font, 6, (0, 0, 255), 8)
cv2.imshow('Bursh', img2)
cv2.waitKey(0)
cv2.destroyAllWindows()
else:
print ("NG")
print (0)
font = cv2.FONT_HERSHEY_SIMPLEX
#print (img.shape[0]/2)
#img = cv2.putText(img, 'NG', (int(img.shape[1]/2)-80, int(img.shape[0]/2)), font, 6, (0, 0, 255), 8)
cv2.imshow('Bursh', img)
cv2.waitKey(0)
cv2.destroyAllWindows()



