我们从Python开源项目中,提取了以下26个代码示例,用于说明如何使用cv2.COLOR_HSV2RGB。
def optical_flow(one, two): """ method taken from (https://chatbotslife.com/autonomous-vehicle-speed-estimation-from-dashboard-cam-ca96c24120e4) """ one_g = cv2.cvtColor(one, cv2.COLOR_RGB2GRAY) two_g = cv2.cvtColor(two, cv2.COLOR_RGB2GRAY) hsv = np.zeros((120, 320, 3)) # set saturation hsv[:,:,1] = cv2.cvtColor(two, cv2.COLOR_RGB2HSV)[:,:,1] # obtain dense optical flow paramters flow = cv2.calcOpticalFlowFarneback(one_g, two_g, flow=None, pyr_scale=0.5, levels=1, winsize=15, iterations=2, poly_n=5, poly_sigma=1.1, flags=0) # convert from cartesian to polar mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1]) # hue corresponds to direction hsv[:,:,0] = ang * (180/ np.pi / 2) # value corresponds to magnitude hsv[:,:,2] = cv2.normalize(mag,None,0,255,cv2.NORM_MINMAX) # convert HSV to int32's hsv = np.asarray(hsv, dtype= np.float32) rgb_flow = cv2.cvtColor(hsv,cv2.COLOR_HSV2RGB) return rgb_flow
def dumper(model,kind,fname=None): if not fname: fname = '{}/models/{}-{}.h5'.format(ROOT, str(datetime.now()).replace(' ','-'),kind) try: with open(fname,'w') as f: model.save(fname) except IOError: raise IOError('Unable to open: {}'.format(fname)) return fname # def random_bright_shift(image): # print np.asarray(image,dtype=np.uint8) # image = np.asarray(image,dtype=np.uint8) # image1 = cv2.cvtColor(image,cv2.COLOR_RGB2HSV) # random_bright = .25+np.random.uniform() # image1[:,:,2] = image1[:,:,2]*random_bright # image1 = cv2.cvtColor(image1,cv2.COLOR_HSV2RGB) # return image1
def change_brightness(img_arr): # print('change brightness called') adjusted_imgs = np.array([img_arr[0]]) for img_num in range(0, len(img_arr)): img = img_arr[img_num] # print('array access') # show_image(img) hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV) # print('rgb2hsv') # show_image(hsv) rando = np.random.uniform() # print('rando is', rando) hsv[:,:, 2] = hsv[:,:, 2] * (.25 + rando) new_img = cv2.cvtColor(hsv, cv2.COLOR_HSV2RGB) # print('hsv2rgb') # show_image(new_img) # new_img = cv2.cvtColor(new_img, cv2.COLOR_BGR2RGB) # show_images(img.reshape((1,) + img.shape), new_img.reshape((1,) + new_img.shape)) adjusted_imgs = np.append(adjusted_imgs, new_img.reshape((1,) + new_img.shape), axis=0) adjusted_imgs = np.delete(adjusted_imgs, 0, 0) return adjusted_imgs
def _brightness(image, min=0.5, max=2.0): ''' Randomly change the brightness of the input image. Protected against overflow. ''' hsv = cv2.cvtColor(image,cv2.COLOR_RGB2HSV) random_br = np.random.uniform(min,max) #To protect against overflow: Calculate a mask for all pixels #where adjustment of the brightness would exceed the maximum #brightness value and set the value to the maximum at those pixels. mask = hsv[:,:,2] * random_br > 255 v_channel = np.where(mask, 255, hsv[:,:,2] * random_br) hsv[:,:,2] = v_channel return cv2.cvtColor(hsv,cv2.COLOR_HSV2RGB)
def histogram_eq(image): ''' Perform histogram equalization on the input image. See https://en.wikipedia.org/wiki/Histogram_equalization. ''' image1 = np.copy(image) image1 = cv2.cvtColor(image1, cv2.COLOR_RGB2HSV) image1[:,:,2] = cv2.equalizeHist(image1[:,:,2]) image1 = cv2.cvtColor(image1, cv2.COLOR_HSV2RGB) return image1
def alter_HSV(img, change_probability = 0.6): if random.random() < 1-change_probability: return img addToHue = random.randint(0,179) addToSaturation = random.gauss(60, 20) addToValue = random.randint(-50,50) hsvVersion = cv2.cvtColor(img, cv2.COLOR_RGB2HSV) channels = hsvVersion.transpose(2, 0, 1) channels[0] = ((channels[0].astype(int) + addToHue)%180).astype(np.uint8) channels[1] = (np.maximum(0, np.minimum(255, (channels[1].astype(int) + addToSaturation)))).astype(np.uint8) channels[2] = (np.maximum(0, np.minimum(255, (channels[2].astype(int) + addToValue)))).astype(np.uint8) hsvVersion = channels.transpose(1,2,0) return cv2.cvtColor(hsvVersion, cv2.COLOR_HSV2RGB) #%%
def SmoothFieldMask(self, mask): # erst Close und dann DILATE führt zu guter Erkennung der Umrandung oben kernel = np.ones((20,20),np.uint8) kernel = np.ones((5,5),np.uint8) mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel) #kernel = np.ones((20,20),np.uint8) #mask = cv2.morphologyEx(mask, cv2.MORPH_DILATE, kernel) #kernel = np.ones((20,20),np.uint8) mask = cv2.GaussianBlur(mask,(11,11),0) #mask = cv2.morphologyEx(mask, cv2.MORPH_ERODE, kernel) # plt.imshow(cv2.cvtColor(cv2.bitwise_and(self.ImgHSV,self.ImgHSV,mask=mask),cv2.COLOR_HSV2RGB),cmap="gray") # plt.show() return mask
def optical_flow(one, two): """ method taken from https://chatbotslife.com/autonomous-vehicle-speed-estimation-from-dashboard-cam-ca96c24120e4 input: image_current, image_next (RGB images) calculates optical flow magnitude and angle and places it into HSV image """ one_g = cv2.cvtColor(one, cv2.COLOR_RGB2GRAY) two_g = cv2.cvtColor(two, cv2.COLOR_RGB2GRAY) hsv = np.zeros((120, 320, 3)) # set saturation hsv[:,:,1] = cv2.cvtColor(two, cv2.COLOR_RGB2HSV)[:,:,1] # obtain dense optical flow paramters flow = cv2.calcOpticalFlowFarneback(one_g, two_g, flow=None, pyr_scale=0.5, levels=1, winsize=10, iterations=2, poly_n=5, poly_sigma=1.1, flags=0) # convert from cartesian to polar mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1]) # hue corresponds to direction hsv[:,:,0] = ang * (180/ np.pi / 2) # value corresponds to magnitude hsv[:,:,2] = cv2.normalize(mag,None,0,255,cv2.NORM_MINMAX) # convert HSV to int32's hsv = np.asarray(hsv, dtype= np.float32) rgb_flow = cv2.cvtColor(hsv,cv2.COLOR_HSV2RGB) return rgb_flow
def hsv_augment(im, hue, saturation, value): """ Augments an image with additive hue, saturation and value. `im` should be 01c RGB in range 0-1. `hue`, `saturation` and `value` should be scalars between -1 and 1. Return value: a 01c RGB image. """ # Convert to HSV im = cv2.cvtColor(im, cv2.COLOR_RGB2HSV) # Rescale hue from 0-360 to 0-1. im[:, :, 0] /= 360. # Mask value == 0 black_indices = im[:, :, 2] == 0 # Add random hue, saturation and value im[:, :, 0] = (im[:, :, 0] + hue) % 1 im[:, :, 1] = im[:, :, 1] + saturation im[:, :, 2] = im[:, :, 2] + value # Pixels that were black stay black im[black_indices, 2] = 0 # Clip pixels from 0 to 1 im = np.clip(im, 0, 1) # Rescale hue from 0-1 to 0-360. im[:, :, 0] *= 360. # Convert back to RGB in 0-1 range. im = cv2.cvtColor(im, cv2.COLOR_HSV2RGB) return im
def hsv_to_rgb(colors): _colors = np.array([colors], dtype=np.uint8) # cv2.cvtColor only accept 2d array rgb_colors = cv2.cvtColor(_colors, cv2.COLOR_HSV2RGB)[0].astype(np.double) return rgb_colors
def change_one(img): print('before') show_image(img) hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV) print('rgb2hsv') show_image(hsv) rando = np.random.uniform() # print('rando is', rando) hsv[:,:, 2] = hsv[:,:, 2] * (.25 + rando) new_img = cv2.cvtColor(hsv, cv2.COLOR_HSV2RGB) print('hsv2rgb') show_image(new_img)
def random_brightness(image): """ Randomly adjust brightness of the image. """ # HSV (Hue, Saturation, Value) is also called HSB ('B' for Brightness). hsv = cv2.cvtColor(image, cv2.COLOR_RGB2HSV) ratio = 1.0 + 0.4 * (np.random.rand() - 0.5) hsv[:,:,2] = hsv[:,:,2] * ratio return cv2.cvtColor(hsv, cv2.COLOR_HSV2RGB)
def image_HSV(img): # HSV brightness transform img = cv2.cvtColor(img,cv2.COLOR_RGB2HSV) brightness = np.random.uniform(0.5,1.1) img[:,:,2] = img[:,:,2]*brightness return cv2.cvtColor(img,cv2.COLOR_HSV2RGB)
def randomize_brightness(image): image = cv2.cvtColor(image, cv2.COLOR_RGB2HSV) random_brightness = .1 + np.random.uniform() image[:,:,2] = image[:,:,2] * random_brightness image = cv2.cvtColor(image, cv2.COLOR_HSV2RGB) return image
def augment_brightness_camera_images(image): image1 = cv2.cvtColor(image,cv2.COLOR_RGB2HSV) random_bright = .25+np.random.uniform() #print(random_bright) image1[:,:,2] = image1[:,:,2]*random_bright image1 = cv2.cvtColor(image1,cv2.COLOR_HSV2RGB) return image1
def render(self, img_rgb): # warming filter: increase red, decrease blue c_r, c_g, c_b = cv2.split(img_rgb) c_r = cv2.LUT(c_r, self.incr_ch_lut).astype(np.uint8) c_b = cv2.LUT(c_b, self.decr_ch_lut).astype(np.uint8) img_rgb = cv2.merge((c_r, c_g, c_b)) # increase color saturation c_h, c_s, c_v = cv2.split(cv2.cvtColor(img_rgb, cv2.COLOR_RGB2HSV)) c_s = cv2.LUT(c_s, self.incr_ch_lut).astype(np.uint8) return cv2.cvtColor(cv2.merge((c_h, c_s, c_v)), cv2.COLOR_HSV2RGB)
def render(self, img_rgb): # cooling filter: increase blue, decrease red c_r, c_g, c_b = cv2.split(img_rgb) c_r = cv2.LUT(c_r, self.decr_ch_lut).astype(np.uint8) c_b = cv2.LUT(c_b, self.incr_ch_lut).astype(np.uint8) img_rgb = cv2.merge((c_r, c_g, c_b)) # decrease color saturation c_h, c_s, c_v = cv2.split(cv2.cvtColor(img_rgb, cv2.COLOR_RGB2HSV)) c_s = cv2.LUT(c_s, self.decr_ch_lut).astype(np.uint8) return cv2.cvtColor(cv2.merge((c_h, c_s, c_v)), cv2.COLOR_HSV2RGB)
def show_image(self, image, draw=[]): """ :param image: :param draw: :return: None """ if 0 != draw: for task in draw: image = task(image) plt.figure() plt.imshow(cv2.cvtColor(image, cv2.COLOR_HSV2RGB)) plt.show()
def get_center_scale(self, calibration_image): """ :param calibration_image: The HSV-image to use for calculation :return: Position of center point in image (tuple), ratio px per cm (reproduction scale) """ gray = cv2.cvtColor(calibration_image, cv2.COLOR_HSV2BGR) gray = cv2.cvtColor(gray, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (5, 5), 1) circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1, 100, param1=50, param2=30, minRadius=50, maxRadius=300) center_circle = (0, 0, 0) min_dist = 0xFFFFFFFFFFF for circle in circles[0]: dist_x = abs(circle[0] - calibration_image.shape[1] / 2) dist_y = abs(circle[1] - calibration_image.shape[0] / 2) if(dist_x + dist_y) < min_dist: min_dist = dist_x + dist_y center_circle = circle rgb = cv2.cvtColor(calibration_image, cv2.COLOR_HSV2RGB) cv2.circle(rgb, (center_circle[0], center_circle[1]), center_circle[2], (0, 255, 0), 1) center = center_circle[0], center_circle[1] radius = center_circle[2] ratio_pxcm = radius / 10.25 self.center = center self.ratio_pxcm = ratio_pxcm return [center, ratio_pxcm]
def ShowImage(self): """ Opens the image in a window with the ball marked red :return: """ rgb = cv2.cvtColor(self.ImgHSV,cv2.COLOR_HSV2RGB) if(len(self.FieldContours) > 0): cv2.drawContours(rgb,self.FieldContours,-1,(0,0,0),3,lineType=cv2.LINE_8) cv2.rectangle(rgb,(((self.FieldContours[0])[0])[0],((self.FieldContours[0])[0])[1]),(((self.FieldContours[2])[0])[0],((self.FieldContours[2])[0])[1]),(0,0,0),3) cv2.circle(rgb,self.BallCenter, 2, (255,0,0), 2) #plt.imshow(rgb) # plt.show() #cv2.imshow('frame',cv2.cvtColor(rgb,cv2.COLOR_RGB2BGR)) # def CreateMask(self, LowerBorder, UpperBorder): """ Internal function. Creates a mask by color :param LowerBorder: Lower border of the color to match (HSV) :param UpperBorder: Higher border of the color to match (HSV) :return: mask array """ # Find Image areas, that have the right color and thus may contain the ball # mask = cv2.inRange(self.ImgHSV,LowerBorder,UpperBorder) #plt.imshow(cv2.cvtColor(cv2.bitwise_and(self.ImgHSV,self.ImgHSV,mask=mask),cv2.COLOR_HSV2RGB),cmap="gray") #plt.show() # return mask
def augment_brightness_camera_images(image): image1 = cv2.cvtColor(image,cv2.COLOR_RGB2HSV) random_bright = .25+np.random.uniform() image1[:,:,2] = image1[:,:,2]*random_bright image1 = cv2.cvtColor(image1,cv2.COLOR_HSV2RGB) return image1
def change_brightness(image): image1 = cv2.cvtColor(image, cv2.COLOR_RGB2HSV) image1 = np.array(image1, dtype=np.float64) random_bright = 0.5 + np.random.uniform() image1[:, :, 2] = image1[:, :, 2]*random_bright image1[:, :, 2][image1[:, :, 2] > 255] = 255 image1 = np.array(image1, dtype=np.uint8) image1 = cv2.cvtColor(image1, cv2.COLOR_HSV2RGB) return image1
def calibrate_ball_color(ball_position): Interface.message("Please put the ball an the center spot!") # Interface.wait_for_user_command(Interface.start_calibration) Interface.message("Calibration starts - 5 seconds for positioning...") # The user has to put the ball onto the center spot for calibration. # A marker cross will appear on the center spot for some time. # At the moment the color calibration is done using a fixed image, that has # to be cropped to the right size. # Therefore, the size of the images from the camera is needed. t_end = time.time() # + 1 _done = 0 # When the fixed image is used for calibration, at least one execution is # needed to get the size of the camera images while time.time() < t_end or not _done: image = Camera.get_newest_frame() x1 = int(round(ball_position[0] - image.shape[1]/20, 0)) x2 = int(round(ball_position[0] + image.shape[1]/20, 0)) y1 = int(round(ball_position[1] - image.shape[0]/20, 0)) y2 = int(round(ball_position[1] + image.shape[0]/20, 0)) marked_image = image.copy() # draw the marker cross cv2.line(marked_image, (x1, ball_position[1]), (x2, ball_position[1]), (0, 255, 255), 2) cv2.line(marked_image, (ball_position[0], y1), (ball_position[0], y2), (0, 255, 255), 2) Interface.show_video(marked_image, GetSourceVar) _done = 1 # calibration_image = Camera.get_newest_frame() calibration_image = cv2.cvtColor(cv2.imread(r'./BallCalibration.PNG'), cv2.COLOR_BGR2HSV) calibration_image = cv2.resize(calibration_image, (image.shape[1], image.shape[0])) # The initialization is done with only a small part of the image around the center spot. x1 = int(round(ball_position[0] - calibration_image.shape[1]/10, 0)) x2 = int(round(ball_position[0] + calibration_image.shape[1]/10, 0)) y1 = int(round(ball_position[1] - calibration_image.shape[0]/10, 0)) y2 = int(round(ball_position[1] + calibration_image.shape[0]/10, 0)) image_crop = calibration_image[y1:y2, x1:x2] # calibration_image = cv2.line(calibration_image,(x1,ball_position[1]),(x2,ball_position[1]),(0,255,255),2) # calibration_image = cv2.line(calibration_image,(ball_position[0],y1),(ball_position[0],y2),(0,255,255),2) # plt.imshow(cv2.cvtColor(calibration_image, cv2.COLOR_HSV2RGB)) # plt.show() DetectBall.calibrate(image_crop)