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
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) # cv2.waitKey(0) # print(contours) # img = cv2.imread('../cropping/test6.jpg') # num_rows, num_cols = img.shape[:2]
True) #Skip small or non-convex objects if (abs(cv2.contourArea(contours[i])) < 100): continue #triangle if (len(approx) == 3): x, y, w, h = cv2.boundingRect(contours[i]) cv2.putText(resized, 'TRI', (x, y), cv2.FONT_HERSHEY_SIMPLEX, scale, (255, 255, 255), 2, cv2.LINE_AA) cv2.rectangle(resized, (x, y), (x + w, y + h), (0, 0, 255), 2) M = cv2.moments(contours[i]) cx = int(M['m10'] / M['m00']) cy = int(M['m01'] / M['m00']) color = cl.label(mask, contours[i]) contours[i] = contours[i].astype("float") contours[i] *= ratio contours[i] = contours[i].astype("int") cv2.drawContours(resized, [contours[i]], -1, (0, 255, 0), 2) cv2.putText(image, color, (cx, cy), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2) elif (len(approx) >= 4 and len(approx) <= 6): #nb vertices of a polygonal curve vtc = len(approx) #get cos of all corners cos = [] for j in range(2, vtc + 1): cos.append( angle(approx[j % vtc], approx[j - 2], approx[j - 1]))
cv2.waitKey(0) cv2.destroyAllWindows() cv2.rectangle(img, (array[0], array[1]), (array[2], array[3]), (0, 0, 255), 3) cv2.imshow('Detection result', img) cv2.waitKey(0) cv2.destroyAllWindows() list_intensities = find_pixels(rcnn_result, mask) bgr_arry = run_kmeans(list_intensities) # rgb_arry_init = np.zeros((1, 1, 3), dtype="uint8") # rgb_arry_init[0] = [r, g, b] # print ('bgr val', bgr_arry, type(bgr_arry)) val = cl.label(bgr_arry[::-1]) color_name = val['Generic Color Name'] print('index', color_name) cv2.imshow('Segmentation result', rcnn_result) cv2.waitKey(0) cv2.destroyAllWindows() cv2.putText(rcnn_result, str(color_name), (600, 500), cv2.FONT_HERSHEY_SIMPLEX, 5, (255, 255, 255), 2) cv2.imshow('Final result', rcnn_result) cv2.waitKey(0) cv2.destroyAllWindows() else: print('No car found')
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 # detect the shape of the contour and label the color shape = sd.detect(c) color = cl.label(lab, c) # multiply the contour (x, y) coords by the resize ratio, # then draw the contours and the name of the shape and labeled # color on the image 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) # show image cv2.imshow('image', image)
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) c = c.astype("float") c *= ratio c = c.astype("int") text = "{} {}".format(color, shape) print(text) with open('result.txt', 'w') as f: f.write(text) f.write('\n') cv2.drawContours(image, [c], -1, (0, 255, 0), 2) cv2.putText(image, text, (cX, cY), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) cv2.fillConvexPoly(image1, np.array(approx, np.int32), color_value)