本文整理匯總了Python中cv2.inRange方法的典型用法代碼示例。如果您正苦於以下問題:Python cv2.inRange方法的具體用法?Python cv2.inRange怎麽用?Python cv2.inRange使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類cv2
的用法示例。
在下文中一共展示了cv2.inRange方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: standard_test
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import inRange [as 別名]
def standard_test(self):
for fnum in range(self.start_fnum, self.stop_fnum):
frame = util.get_frame(self.capture, fnum)
frame = frame[280:, :]
frame_HSV = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(frame_HSV, (self.low_H, self.low_S, self.low_V),
(self.high_H, self.high_S, self.high_V))
res = cv2.bitwise_and(frame, frame, mask=mask)
res_inv = cv2.bitwise_and(frame, frame, mask=cv2.bitwise_not(mask))
cv2.imshow(self.window_name, mask)
cv2.imshow('Video Capture AND', res)
cv2.imshow('Video Capture INV', res_inv)
if cv2.waitKey(30) & 0xFF == ord('q'):
break
# A number of methods corresponding to the various trackbars available.
示例2: remove_other_color
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import inRange [as 別名]
def remove_other_color(img):
frame = cv2.GaussianBlur(img, (3,3), 0)
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# define range of blue color in HSV
lower_blue = np.array([100,128,0])
upper_blue = np.array([215,255,255])
# Threshold the HSV image to get only blue colors
mask_blue = cv2.inRange(hsv, lower_blue, upper_blue)
lower_white = np.array([0,0,128], dtype=np.uint8)
upper_white = np.array([255,255,255], dtype=np.uint8)
# Threshold the HSV image to get only blue colors
mask_white = cv2.inRange(hsv, lower_white, upper_white)
lower_black = np.array([0,0,0], dtype=np.uint8)
upper_black = np.array([170,150,50], dtype=np.uint8)
mask_black = cv2.inRange(hsv, lower_black, upper_black)
mask_1 = cv2.bitwise_or(mask_blue, mask_white)
mask = cv2.bitwise_or(mask_1, mask_black)
# Bitwise-AND mask and original image
#res = cv2.bitwise_and(frame,frame, mask= mask)
return mask
示例3: pick_color
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import inRange [as 別名]
def pick_color(event,x,y,flags,param):
if event == cv2.EVENT_LBUTTONDOWN:
pixel = image_hsv[y,x]
#HUE, SATURATION, AND VALUE (BRIGHTNESS) RANGES. TOLERANCE COULD BE ADJUSTED.
# Set range = 0 for hue and range = 1 for saturation and brightness
# set upper_or_lower = 1 for upper and upper_or_lower = 0 for lower
hue_upper = check_boundaries(pixel[0], 10, 0, 1)
hue_lower = check_boundaries(pixel[0], 10, 0, 0)
saturation_upper = check_boundaries(pixel[1], 10, 1, 1)
saturation_lower = check_boundaries(pixel[1], 10, 1, 0)
value_upper = check_boundaries(pixel[2], 40, 1, 1)
value_lower = check_boundaries(pixel[2], 40, 1, 0)
upper = np.array([hue_upper, saturation_upper, value_upper])
lower = np.array([hue_lower, saturation_lower, value_lower])
print(lower, upper)
#A MONOCHROME MASK FOR GETTING A BETTER VISION OVER THE COLORS
image_mask = cv2.inRange(image_hsv,lower,upper)
cv2.imshow("Mask",image_mask)
示例4: _update_mean_shift_bookkeeping
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import inRange [as 別名]
def _update_mean_shift_bookkeeping(self, frame, box_grouped):
"""Preprocess all valid bounding boxes for mean-shift tracking
This method preprocesses all relevant bounding boxes (those that
have been detected by both mean-shift tracking and saliency) for
the next mean-shift step.
:param frame: current RGB input frame
:param box_grouped: list of bounding boxes
"""
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
self.object_roi = []
self.object_box = []
for box in box_grouped:
(x, y, w, h) = box
hsv_roi = hsv[y:y + h, x:x + w]
mask = cv2.inRange(hsv_roi, np.array((0., 60., 32.)),
np.array((180., 255., 255.)))
roi_hist = cv2.calcHist([hsv_roi], [0], mask, [180], [0, 180])
cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX)
self.object_roi.append(roi_hist)
self.object_box.append(box)
示例5: calculate_roi_hist
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import inRange [as 別名]
def calculate_roi_hist(self, frame):
"""Calculates region of interest histogram.
Args:
frame: The np.array image frame to calculate ROI histogram for.
"""
(x, y, w, h) = self.box
roi = frame[y:y + h, x:x + w]
hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv_roi, np.array((0., 60., 32.)),
np.array((180., 255., 255.)))
roi_hist = cv2.calcHist([hsv_roi], [0, 1], mask, [180, 255],
[0, 180, 0, 255])
cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX)
self.roi_hist = roi_hist
# Run this every frame
示例6: threshold_video
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import inRange [as 別名]
def threshold_video(lower_color, upper_color, blur):
# Convert BGR to HSV
hsv = cv2.cvtColor(blur, cv2.COLOR_BGR2HSV)
# hold the HSV image to get only red colors
mask = cv2.inRange(hsv, lower_color, upper_color)
# Returns the masked imageBlurs video to smooth out image
return mask
# Finds the tape targets from the masked image and displays them on original stream + network tales
示例7: detect_shirt
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import inRange [as 別名]
def detect_shirt(self):
#self.dst=cv2.inRange(self.norm_rgb,np.array([self.lb,self.lg,self.lr],np.uint8),np.array([self.b,self.g,self.r],np.uint8))
self.dst=cv2.inRange(self.norm_rgb,np.array([20,20,20],np.uint8),np.array([255,110,80],np.uint8))
cv2.threshold(self.dst,0,255,cv2.THRESH_OTSU+cv2.THRESH_BINARY)
fg=cv2.erode(self.dst,None,iterations=2)
#cv2.imshow("fore",fg)
bg=cv2.dilate(self.dst,None,iterations=3)
_,bg=cv2.threshold(bg, 1,128,1)
#cv2.imshow("back",bg)
mark=cv2.add(fg,bg)
mark32=np.int32(mark)
cv2.watershed(self.norm_rgb,mark32)
self.m=cv2.convertScaleAbs(mark32)
_,self.m=cv2.threshold(self.m,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
#cv2.imshow("final_tshirt",self.m)
cntr,h=cv2.findContours(self.m,cv2.cv.CV_RETR_EXTERNAL,cv2.cv.CV_CHAIN_APPROX_SIMPLE)
return self.m,cntr
示例8: find_red
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import inRange [as 別名]
def find_red(img):
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv,(130,130,180),(255,255,255))
mask = cv2.erode(mask, np.ones((2,1)) , iterations=1)
mask = cv2.dilate(mask, None, iterations=3)
cnts = cv2.findContours(mask, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[-2]
frame=img.copy()
###based on example from http://www.pyimagesearch.com/2015/09/14/ball-tracking-with-opencv
if len(cnts) > 0:
c = max(cnts, key=cv2.contourArea)
((x, y), radius) = cv2.minEnclosingCircle(c)
M = cv2.moments(c)
center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
if radius > 3:
cv2.circle(frame, (int(x), int(y)), 12,(0, 255, 255), 2)
return frame
示例9: getMonMask
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import inRange [as 別名]
def getMonMask(self, img):
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# define range of blue color in HSV
lower_blue = np.array([94, 130, 70])
upper_blue = np.array([114, 160, 110])
# Threshold the HSV image to get only shadow colors
mask = cv2.inRange(hsv, lower_blue, upper_blue)
kernel = np.ones((2, 2), np.uint8)
mask = cv2.dilate(mask, kernel, iterations=1)
final_mask = 255 - cv2.medianBlur(mask, 3) # invert mask
return final_mask
# Detect gym from raid sighting image
示例10: _maskBackground
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import inRange [as 別名]
def _maskBackground(self, img, mask_corners = True):
h,w,c = np.shape(img)
blur_img=cv2.blur(img, (5,5))
hsv = cv2.cvtColor(blur_img, cv2.COLOR_BGR2HSV)
lower_color = np.array([22,28,26])
upper_color = np.array([103,255,255])
# create binary mask by finding background color range
mask = cv2.inRange(hsv, self.lower_mask_color, self.upper_mask_color)
# remove the corners from mask since they are prone to illumination problems
if(mask_corners):
circle_mask = np.zeros((h, w), np.uint8)
circle_mask[:, :] = 255
cv2.circle(circle_mask,(w/2, h/2), min(w/2, h/2), 0, -1)
mask = cv2.bitwise_or(mask,circle_mask)
# invert mask to get white objects on black background
#inverse_mask = 255 - mask
if self._interactive: cv2.imshow("binary mask", mask)
if self._interactive: cv2.waitKey(0)
return mask
示例11: colorTarget
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import inRange [as 別名]
def colorTarget(color_range=((0, 0, 0), (255, 255, 255))):
image = cam.newImage()
if filter == 'RGB':
frame_to_thresh = image.copy()
else:
frame_to_thresh = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) # convert image to hsv colorspace RENAME THIS TO IMAGE_HSV
thresh = cv2.inRange(frame_to_thresh, color_range[0], color_range[1])
mask = thresh
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2] # generates number of contiguous "1" pixels
if len(cnts) == 0: # begin processing if there are "1" pixels discovered
return np.array([None, None, 0])
else:
c = max(cnts, key=cv2.contourArea) # return the largest target area
((x, y), radius) = cv2.minEnclosingCircle(c)
if radius > 4:
return np.array([round(x, 1), round(y, 1), round(radius, 1)])
示例12: colorTarget
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import inRange [as 別名]
def colorTarget(color_range=((0, 0, 0), (255, 255, 255))):
image = cam.newImage()
if filter == 'RGB':
frame_to_thresh = image.copy()
else:
frame_to_thresh = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) # convert image to hsv colorspace RENAME THIS TO IMAGE_HSV
thresh = cv2.inRange(frame_to_thresh, color_range[0], color_range[1])
# apply a blur function
kernel = np.ones((5, 5), np.uint8)
mask = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel) # Apply blur
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel) # Apply blur 2nd iteration
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2] # generates number of contiguous "1" pixels
if len(cnts) > 0: # begin processing if there are "1" pixels discovered
c = max(cnts, key=cv2.contourArea) # return the largest target area
((x, y), radius) = cv2.minEnclosingCircle(c)
return np.array([round(x, 1), round(y, 1), round(radius, 1)])
else:
return np.array([None, None, 0])
示例13: image_callback
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import inRange [as 別名]
def image_callback(self, msg):
image = self.bridge.imgmsg_to_cv2(msg,desired_encoding='bgr8')
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower_yellow = numpy.array([ 10, 10, 10])
upper_yellow = numpy.array([255, 255, 250])
mask = cv2.inRange(hsv, lower_yellow, upper_yellow)
h, w, d = image.shape
search_top = 3*h/4
search_bot = 3*h/4 + 20
mask[0:search_top, 0:w] = 0
mask[search_bot:h, 0:w] = 0
M = cv2.moments(mask)
if M['m00'] > 0:
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])
cv2.circle(image, (cx, cy), 20, (0,0,255), -1)
# BEGIN CONTROL
err = cx - w/2
self.twist.linear.x = 0.2
self.twist.angular.z = -float(err) / 100
self.cmd_vel_pub.publish(self.twist)
# END CONTROL
cv2.imshow("window", image)
cv2.waitKey(3)
示例14: returnMask
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import inRange [as 別名]
def returnMask(self, frame, morph_opening=True, blur=True, kernel_size=5, iterations=1):
"""Given an input frame return the black/white mask.
This version of the function does not use the blur and bitwise
operations, then the resulting frame contains white pixels
in correspondance of the skin found during the searching process.
@param frame the original frame (color)
"""
#Convert to HSV and eliminate pixels outside the range
frame_hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
frame_filtered = cv2.inRange(frame_hsv, self.min_range, self.max_range)
if(morph_opening==True):
kernel = np.ones((kernel_size,kernel_size), np.uint8)
frame_filtered = cv2.morphologyEx(frame_filtered, cv2.MORPH_OPEN, kernel, iterations=iterations)
#Applying Gaussian Blur
if(blur==True):
frame_filtered = cv2.GaussianBlur(frame_filtered, (kernel_size,kernel_size), 0)
return frame_filtered
示例15: getthresholdedimg
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import inRange [as 別名]
def getthresholdedimg(hsv):
yellow = cv2.inRange(hsv, np.array((20, 100, 100)), np.array((30, 255, 255)))
blue = cv2.inRange(hsv, np.array((100, 100, 100)), np.array((120, 255, 255)))
both = cv2.add(yellow, blue)
return both