栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python OpenCV按照色彩面积识别

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python OpenCV按照色彩面积识别

载请注明出处,联系我: 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()
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/649914.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号