def patternDetection(cnts): #Initialize classes sd = ShapeDetector() cl = ColorLabeler() contours = [] for c in cnts: shape = sd.detect(c, 3) if shape != "": color = cl.label(lab, c) x,y,w,h = cv2.boundingRect(c) cX = x+w/2 cY = y+h/2 contours.append([cX, cY, shape, color]) row = [] pattern = [] rowMin = contours[0][1]-10 rowMax = contours[0][1]+10 for c in contours: cX, cY, shape, color = c if rowMin <= cY and cY <= rowMax: row.append([cX, cY, shape+' '+color]) else: row.sort() pattern.append(row) row = [[cX, cY, shape+' '+color]] rowMin = cY-10 rowMax = cY+10 return pattern
def l23shape(self): # 读取图片 image = cv2.imread(self.imgName) cv2.imshow("images", image) # 进行裁剪操作 resized = imutils.resize(image, width=300) ratio = image.shape[0] / float(resized.shape[0]) # 进行高斯模糊操作 blurred = cv2.GaussianBlur(resized, (5, 5), 0) # 进行图片灰度化 gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY) # 进行颜色空间的变换 lab = cv2.cvtColor(blurred, cv2.COLOR_BGR2LAB) # 进行阈值分割 thresh = cv2.threshold(gray, 60, 255, cv2.THRESH_BINARY)[1] cv2.imshow("Thresh", thresh) # 在二值图片中寻找轮廓 cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) # 初始化形状检测器和颜色标签 sd = ShapeDetector() cl = ColorLabeler() # 遍历每一个轮廓 for c in cnts: # 计算每一个轮廓的中心点 M = cv2.moments(c) cX = int((M["m10"] / M["m00"]) * ratio) cY = int((M["m01"] / M["m00"]) * ratio) # 进行颜色检测和形状检测 shape = sd.detect(c) color = cl.label(lab, c) # 进行坐标变换 c = c.astype("float") c *= ratio c = c.astype("int") text = "{} {}".format(color, shape) # 绘制轮廓并显示结果 cv2.drawContours(image, [c], -1, (0, 255, 0), 2) cv2.putText(image, text, (cX, cY), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2) cv2.imshow("Image", image) cv2.waitKey(0) show = cv2.resize(image, (640, 480)) # 把读到的帧的大小重新设置为 640x480 show = cv2.cvtColor(show, cv2.COLOR_BGR2RGB) # 视频色彩转换回RGB,这样才是现实的颜色 self.showImage = QtGui.QImage( show.data, show.shape[1], show.shape[0], QtGui.QImage.Format_RGB888) # 把读取到的视频数据变成QImage形式 self.lImg1.setPixmap(QtGui.QPixmap.fromImage( self.showImage)) # 往显示视频的Label里 显示QImage
def __init__(self): self.pub = rospy.Publisher('pub_with_sub', String, queue_size=10) # self.bridge = CvBridge() # self.img = rospy.Subscriber("color_dect",Image,self.callback) image = cv2.imread("talker_screenshot_25.08.2021.png") # if ret == True: cv2.imshow("talker", image) cv2.waitKey(1) resized = imutils.resize(image, width=300) blurred = cv2.GaussianBlur(resized, (5, 5), 0) gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY) cv2.imshow("gray is ", gray) cv2.waitKey(1) lab = cv2.cvtColor(blurred, cv2.COLOR_BGR2LAB) # thresh = cv2.threshold(gray,20,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)[1] # thresh = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\ # cv2.THRESH_BINARY_INV,11,2)[1] thresh = cv2.threshold(gray, 100, 255, cv2.THRESH_BINARY_INV)[1] cv2.imshow("ROS_Thresh", thresh) cv2.waitKey(1) cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) cv2.drawContours(resized, cnts, -1, (0, 255, 0), 2) cl = ColorLabeler() # --------------------------------------------------------# # for c in cnts: # area = [cv2.contourArea(c)] # max_area = max(area) # if max_area > 2500: # color = cl.label(lab, c) # cv2.drawContours(image, [c], -1, (0, 255, 0), 2) # -------------------------------------------------------# areas = [] for c in cnts: area = [cv2.contourArea(c)] areas.append(area) max_area = max(areas) max_idx = np.argmax(areas) print("max_area", max_area) # if max_area > 2500: color = cl.label(lab, cnts, max_idx) # print cnts[max_idx] rospy.loginfo("The s is %s", color) self.pub.publish(color) return color
def findcolors(nc, im): cont = 0 image = cv2.imread(im) blurred = cv2.GaussianBlur(image, (5, 5), 0) gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY) lab = cv2.cvtColor(blurred, cv2.COLOR_BGR2LAB) thresh = cv2.threshold(gray, 60, 255, cv2.THRESH_BINARY)[1] cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if imutils.is_cv2() else cnts[1] cl = ColorLabeler() sd = ShapeDetector() for c in cnts: if cv2.contourArea(c) < 800: continue else: M = cv2.moments(c) cX = int((M["m10"] / M["m00"])) cY = int((M["m01"] / M["m00"])) shape = sd.detect(c) color = cl.label(lab, c) print(shape) print(color) if (shape == "square" or shape == "rectangle"): if (color == nc): text = "{}".format(color) cv2.drawContours(image, [c], -1, (0, 255, 0), 2) cv2.putText(image, text, (cX, cY), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2) cont = cont + 1 cv2.imshow("Image", image) cv2.waitKey(0) return cont
def alltheCV(image): #resized = imutils.resize(image, width=300) #ratio = image.shape[0] / float(resized.shape[0]) #height, width = image.shape[:2] print("runningCV") #Convert white background to black gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) (thresh, baw) = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU) wab = cv2.bitwise_not(baw) wabrgb = cv2.cvtColor(wab, cv2.COLOR_GRAY2RGB) noBackground = cv2.bitwise_and(image, wabrgb) #Blur and threshold the image for curve detection blurred = cv2.GaussianBlur(noBackground, (5, 5), 0) lab = cv2.cvtColor(blurred, cv2.COLOR_BGR2LAB) gray = cv2.cvtColor(noBackground, cv2.COLOR_BGR2GRAY) thresh = cv2.threshold(gray, 30, 255, cv2.THRESH_BINARY)[1] #Detect contours cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] #Initialize classes sd = ShapeDetector() cl = ColorLabeler() #Finding centers using bounding rectangle centers = [] for c in cnts: shape, perimeter = sd.detect(c, 1) color = cl.label(lab, c) x, y, w, h = cv2.boundingRect(c) cX = x + w / 2 cY = y + h / 2 centers.append([cX, cY, perimeter, shape, color]) return centers
def findcolors(nc,im): cont=0 image = cv2.imread(im) blurred = cv2.GaussianBlur(image, (5, 5), 0) gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY) lab = cv2.cvtColor(blurred, cv2.COLOR_BGR2LAB) thresh = cv2.threshold(gray, 60, 255, cv2.THRESH_BINARY)[1] cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if imutils.is_cv2() else cnts[1] cl = ColorLabeler() sd = ShapeDetector() for c in cnts: if cv2.contourArea(c)< 800: continue else: M = cv2.moments(c) cX = int((M["m10"] / M["m00"])) cY = int((M["m01"] / M["m00"])) shape = sd.detect(c) color = cl.label(lab, c) print(shape) print(color) if (shape=="square" or shape=="rectangle"): if (color==nc): text = "{}".format(color) cv2.drawContours(image, [c], -1, (0, 255, 0), 2) cv2.putText(image, text, (cX, cY), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2) cont=cont+1 cv2.imshow("Image", image) cv2.waitKey(0) return cont
wab = cv2.bitwise_not(baw) wabrgb = cv2.cvtColor(wab, cv2.COLOR_GRAY2RGB) noBackground = cv2.bitwise_and(resized, wabrgb) lab = cv2.cvtColor(noBackground, cv2.COLOR_BGR2LAB) blurred_3 = cv2.GaussianBlur(noBackground, (1, 1), 0) gray_3 = cv2.cvtColor(blurred_3, cv2.COLOR_BGR2GRAY) thresh_3 = cv2.threshold(gray_3, 30, 255, cv2.THRESH_BINARY)[1] cnts = cv2.findContours(thresh_3.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if imutils.is_cv2() else cnts[1] #Initialize classes sd = ShapeDetector() cl = ColorLabeler() contours = [] for c in cnts: shape = sd.detect(c, 3) if shape != "": color = cl.label(lab, c) x, y, w, h = cv2.boundingRect(c) cX = x + w / 2 cY = y + h / 2 contours.append([cX, cY, shape, color]) # cv2.imshow("Image", img) # cv2.circle(img, (cX, cY), 3, (255, 255, 255), -1) # cv2.waitKey(0) # cv2.imshow("Image", img) # cv2.circle(img, (cX, cY), 3, (255, 255, 255), -1)
from colorlabeler import ColorLabeler lowerBound = np.array([136, 87, 111]) upperBound = np.array([180, 255, 255]) kernelOpen = np.ones((5, 5)) kernelClose = np.ones((20, 20)) contours = {} approx = [] scale = 1 #camera cap = cv2.VideoCapture(0) cl = ColorLabeler() #calculate angle def angle(pt1, pt2, pt0): 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] return float((dx1 * dx2 + dy1 * dy2)) / math.sqrt( float((dx1 * dx1 + dy1 * dy1)) * (dx2 * dx2 + dy2 * dy2) + 1e-10) while (cap.isOpened()): ret, frame = cap.read()
from imageai.Detection import ObjectDetection from mrcnn import utils import numpy as np import cv2 from sklearn.cluster import KMeans import matplotlib.pylab as plt import matplotlib.patches as patches from colorlabeler import ColorLabeler from maskrcnn import * import warnings warnings.filterwarnings("ignore") cl = ColorLabeler() lst_intensities = [] def preprocess(rgb): blurred = cv2.GaussianBlur(rgb, (5, 5), 0) gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY) # print ('gray_shape', gray.shape) binary = cv2.threshold(gray, 60, 255, cv2.THRESH_BINARY)[1] # binary = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, \ # cv2.THRESH_BINARY, 11, 2) # binary = binary - binary.max() #binary[binary==255] = 1 return binary
# blur the resized image slightly, then convert it to both # graysale and the L*a*b* color spaces blurred = cv2.GaussianBlur(resized, (5, 5), 0) gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY) lab = cv2.cvtColor(blurred, cv2.COLOR_BGR2LAB) thresh = cv2.threshold(gray, 60, 255, cv2.THRESH_BINARY)[1] # find contours in the thresholded image cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if imutils.is_cv2() else cnts[1] # init sd = ShapeDetector() cl = ColorLabeler() # loop over cnts for c in cnts: # compute the center of the contour M = cv2.moments(c) try: cX = int((M['m10'] / M['m00']) * ratio) cY = int((M['m01'] / M['m00']) * ratio) except: cX = 0 cY = 0
from colorlabeler import ColorLabeler import argparse import imutils import cv2 ap = argparse.ArgumentParser() ap.add_argument('-i', '--image', required=True, help='/path/to/img') args = vars(ap.parse_args()) image = cv2.imread(args['image']) resized = imutils.resize(image, width=300) ratio = image.shape[0] / float(resized.shape[0]) blurred = cv2.GaussianBlur(resized, (5,5), 0) gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY) lab = cv2.cvtColor(blurred, cv2.COLOR_BGR2LAB) thresh = cv2.threshold(gray, 60, 255, cv2.THRESH_BINARY) contours = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) contours = contours[0] if imutils.is_cv2() else contours[1] # Initialize shape and color detectors sd = ShapeDetector() cl = ColorLabeler() for c in contours: M = cv2.moments(c) cX = int((M['m10'] / (M['m00'] + 1e-7)
rows, cols = (len(cnts), 2) tmp = [[0]*cols]*rows for c in cnts: mask = np.zeros(resized.shape[:2], dtype="uint8") cv2.drawContours(mask, [c[0]], -1, 255, -1) mean = cv2.mean(resized, mask=mask)[:3] tmp[i] = [c,mean] i += 1 cnts = tmp #if it is the first time if cl == None: cl = ColorLabeler(cnts,nbrClasses) continue #colors classification #cnts = cl.getCenters(cnts) #every contour with its label i = 0 labels = cl.getLabel(cnts,supervised = supervised) rows, cols = (len(cnts), 3) tmp = [[0]*cols]*rows for c in cnts: tmp[i] = [c[0],c[1],labels[i]] i += 1
blurred = cv2.GaussianBlur(resized, (5, 5), 0) gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY) lab = cv2.cvtColor(blurred, cv2.COLOR_BGR2LAB) thresh = cv2.threshold(gray, 170, 255, cv2.THRESH_BINARY)[1] # thresh = cv2.Canny(gray, 50, 100) # thresh = cv2.dilate(thresh, None, iterations=2) # thresh = cv2.erode(thresh, None, iterations=2) cv2.imshow("Thresh", thresh) cv2.waitKey(0) cnts = cv2.findContours(thresh.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE) cnts = imutils.grab_contours(cnts) (cnts, _) = contours.sort_contours(cnts) sd = ShapeDetector() cl = ColorLabeler() # for c in cnts: if 1: if len(cnts) > 1: c = cnts[1] else: c = cnts[0] M = cv2.moments(c) cX = int((M["m10"] / M["m00"]) * ratio) cY = int((M["m01"] / M["m00"]) * ratio) shape, approx = sd.detect(c) color, color_value = cl.label(lab, c) # mean = cl2.get_mean(lab, c) # color = cl2.getColorName(mean)