# import the necessary packages from picamera.array import PiRGBArray from picamera import PiCamera import time import cv2 import numpy as np import imutils # initialize the camera and grab a reference to the raw camera capture camera = PiCamera() camera.resolution = (640, 480) camera.framerate = 32 rawCapture = PiRGBArray(camera, size=(640, 480)) camera.awb_mode='fluorescent' camera.exposure_mode='fixedfps' # allow the camera to warmup time.sleep(0.1) # capture frames from the camera for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True): # grab the raw NumPy array representing the image, then initialize the timestamp # and occupied/unoccupied text image = frame.array # Convert BGR to HSV hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) # define range of blue color in HSV ## lower_blue = np.array([110,50,50]) ## upper_blue = np.array([130,255,255]) # define range of blue color in HSV lower_blue = np.array([80,50,60]) upper_blue = np.array([255,255,255]) # Threshold the HSV image to get only blue colors mask = cv2.inRange(hsv, lower_blue, upper_blue) # Bitwise-AND mask and original image res = cv2.bitwise_and(image,image, mask= mask) #Croping the mask image around a ROI crop_mask=mask[50:600, 50:600] crop_image=image[50:600, 50:600] crop_image_copy=crop_image #close the image tho clean the mask kernel=np.ones((3,3), np.uint8) close_crop_mask=cv2.morphologyEx(crop_mask, cv2.MORPH_CLOSE, kernel) invert_close_crop_mask=(255-close_crop_mask) # find contours in the thresholded image ## cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) cnts = cv2.findContours(invert_close_crop_mask.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if imutils.is_cv2() else cnts[1] # Draw the contours # compute the center of the contour # loop over the contours vec_area=np.zeros(len(cnts)) ## print vec_area k=0 for c in cnts: ## c=cnts[1] M = cv2.moments(c) ## print M if M["m00"] !=0: cX = int(M["m10"] / M["m00"]) cY = int(M["m01"] / M["m00"]) else: cX, cY = 0, 0 # Contour Area area = cv2.contourArea(c) ## print area ## print c vec_area[k]=area k=k+1 # iterate through all the top-level contours, # draw each connected component with its own random color ## int idx = 0; ## for( ; idx >= 0; idx = hierarchy[idx][0] ) ## Scalar color( rand()&255, rand()&255, rand()&255 ); ## drawContours( dst, contours, idx, color, CV_FILLED, 8, hierarchy ); R=np.linspace(1,0,len(cnts))*255 G=np.linspace(0,1,len(cnts))*255 print G B=0 print vec_area idx=[i[0] for i in sorted(enumerate(vec_area), key=lambda x:x[1])] print idx ## drawing = np.ones(invert_close_crop_mask.shape,np.uint8)*255*invert_close_crop_mask; ## print drawing kk=0 for c in cnts: cv2.drawContours(crop_image, [c], -1, (round(R[idx[kk]]),255,round(G[idx[kk]])), -1 ); kk=kk+1 ## ## drawing = np.zeros(invert_close_crop_mask.shape,np.uint8); ## # draw the contour and center of the shape on the image ## if area > 100: ## imgContour=cv2.drawContours(drawing, [c], 0, (0, 255, 0), 1) ## cv2.imshow('crop_image_copy',crop_image_copy) cv2.imshow('crop_image',crop_image) cv2.imshow('mask',mask) ## cv2.imshow('drawing',drawing) cv2.imshow('invert_close_crop_mask',invert_close_crop_mask) cv2.imshow('res',res) ## cv2.imshow('imgContour',imgContour) k = cv2.waitKey(5) & 0xFF # clear the stream in preparation for the next frame rawCapture.truncate(0) if k == 2: break