Python cv2 模块,bitwise_and() 实例源码

我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用cv2.bitwise_and()

项目:CE264-Computer_Vision    作者:RobinCPC    | 项目源码 | 文件源码
def skin_calib(self, raw_yrb):
        mask_skin = cv2.inRange(raw_yrb, self.mask_lower_yrb, self.mask_upper_yrb)
        cal_skin = cv2.bitwise_and(raw_yrb, raw_yrb, mask=mask_skin)
        cv2.imshow('YRB_calib', cal_skin)
        k = cv2.waitKey(5) & 0xFF
        if k == ord('s'):
            self.calib_switch = False
            cv2.destroyWindow('YRB_calib')

        ymin = cv2.getTrackbarPos('Ymin', 'YRB_calib')
        ymax = cv2.getTrackbarPos('Ymax', 'YRB_calib')
        rmin = cv2.getTrackbarPos('CRmin', 'YRB_calib')
        rmax = cv2.getTrackbarPos('CRmax', 'YRB_calib')
        bmin = cv2.getTrackbarPos('CBmin', 'YRB_calib')
        bmax = cv2.getTrackbarPos('CBmax', 'YRB_calib')
        self.mask_lower_yrb = np.array([ymin, rmin, bmin])
        self.mask_upper_yrb = np.array([ymax, rmax, bmax])


# Do skin detection with some filtering
项目:OpenAI_Challenges    作者:AlwaysLearningDeeper    | 项目源码 | 文件源码
def roi(img,vertices):
    # blank mask:
    mask = np.zeros_like(img)

    # filling pixels inside the polygon defined by "vertices" with the fill color
    cv2.fillPoly(mask, vertices, 255)

    # returning the image only where mask pixels are nonzero
    masked = cv2.bitwise_and(img, mask)
    return masked
项目:Opencv-Python-Examples-    作者:arijitx    | 项目源码 | 文件源码
def color_picker(rect):
    global img,img_gray2,hsv
    roi=img[rect[0][1]:rect[1][1],rect[0][0]:rect[1][0]]
    b,g,r,_=np.uint8(cv2.mean(roi))
    color=cv2.cvtColor(np.uint8([[[b,g,r]]]),cv2.COLOR_BGR2HSV)
    h= color[0][0][0]
    # define range of blue color in HSV
    lower = np.array([h-10,50,50])
    upper = np.array([h+10,255,255])

    # Threshold the HSV image to get only blue colors
    mask = cv2.inRange(hsv, lower, upper)

    # Bitwise-AND mask and original image
    res = cv2.bitwise_and(img,img, mask= mask)
    res2=cv2.bitwise_and(img_gray2,img_gray2, mask= cv2.bitwise_not(mask))
    return res+res2
项目:PiStorms    作者:mindsensors    | 项目源码 | 文件源码
def findSquare( self,frame ):
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        blurred = cv2.GaussianBlur(gray, (7, 7), 0)
        edged = cv2.Canny(blurred, 60, 60)
        # find contours in the edge map
        (cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        # loop over our contours to find hexagon
        cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:50]
        screenCnt = None
        for c in cnts:
            # approximate the contour
            peri = cv2.arcLength(c, True)
            approx = cv2.approxPolyDP(c, 0.004 * peri, True)
            # if our approximated contour has four points, then
            # we can assume that we have found our squeare

            if len(approx) >= 4:
                screenCnt = approx
                x,y,w,h = cv2.boundingRect(c)
                cv2.drawContours(image, [approx], -1, (0, 0, 255), 1)
                #cv2.imshow("Screen", image)
                #create the mask and remove rest of the background
                mask = np.zeros(image.shape[:2], dtype = "uint8")
                cv2.drawContours(mask, [screenCnt], -1, 255, -1)
                masked = cv2.bitwise_and(image, image, mask = mask)
                #cv2.imshow("Masked",masked  )
                #crop the masked image to to be compared to referance image
                cropped = masked[y:y+h,x:x+w]
                #scale the image so it is fixed size as referance image
                cropped = cv2.resize(cropped, (200,200), interpolation =cv2.INTER_AREA)

                return cropped
项目:pc-drone    作者:perrytsao    | 项目源码 | 文件源码
def add_blobs(crop_frame):
    frame=cv2.GaussianBlur(crop_frame, (3, 3), 0)
    # Convert BGR to HSV
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    # define range of green color in HSV
    lower_green = np.array([70,50,50])
    upper_green = np.array([85,255,255])
    # Threshold the HSV image to get only blue colors
    mask = cv2.inRange(hsv, lower_green, upper_green)
    mask = cv2.erode(mask, None, iterations=1)
    mask = cv2.dilate(mask, None, iterations=1)    
    # Bitwise-AND mask and original image
    res = cv2.bitwise_and(frame,frame, mask= mask)
    detector = cv2.SimpleBlobDetector_create(params)
    # Detect blobs.
    reversemask=255-mask
    keypoints = detector.detect(reversemask)
    if keypoints:
        print "found blobs"
        if len(keypoints) > 4:
            keypoints.sort(key=(lambda s: s.size))
            keypoints=keypoints[0:3]
        # Draw detected blobs as red circles.
        # cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures the size of the circle corresponds to the size of blob
        im_with_keypoints = cv2.drawKeypoints(frame, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
    else:
        print "no blobs"
        im_with_keypoints=crop_frame

    return im_with_keypoints #, max_blob_dist, blob_center, keypoint_in_orders
项目:CE264-Computer_Vision    作者:RobinCPC    | 项目源码 | 文件源码
def skin_detect(self, raw_yrb, img_src):
        # use median blurring to remove signal noise in YCRCB domain
        raw_yrb = cv2.medianBlur(raw_yrb, 5)
        mask_skin = cv2.inRange(raw_yrb, self.mask_lower_yrb, self.mask_upper_yrb)

        # morphological transform to remove unwanted part
        kernel = np.ones((5, 5), np.uint8)
        #mask_skin = cv2.morphologyEx(mask_skin, cv2.MORPH_OPEN, kernel)
        mask_skin = cv2.dilate(mask_skin, kernel, iterations=2)

        res_skin = cv2.bitwise_and(img_src, img_src, mask=mask_skin)
        #res_skin_dn = cv2.fastNlMeansDenoisingColored(res_skin, None, 10, 10, 7,21)

        return res_skin


# Do background subtraction with some filtering
项目:reconstruction    作者:microelly2    | 项目源码 | 文件源码
def execute_ColorSpace(proxy,obj):

    try: img=obj.sourceObject.Proxy.img.copy()
    except: img=cv2.imread(__dir__+'/icons/freek.png')
    hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)

    lower = np.array([max(obj.h1-obj.h2,0),max(obj.s1-obj.s2,0),max(obj.v1-obj.v2,0)])
    upper = np.array([min(obj.h1+obj.h2,255),min(obj.s1+obj.s2,255),min(obj.v1+obj.v2,255)])
    say("ee")
    say(lower)
    say(upper)

    mask = cv2.inRange(hsv, lower, upper)
    mask = cv2.inRange(img, lower, upper)

    res = cv2.bitwise_and(img,img, mask= mask)

    obj.Proxy.img=res
项目:reconstruction    作者:microelly2    | 项目源码 | 文件源码
def execute_HSV(proxy,obj):

    say("hsv ..")

    try: img=obj.sourceObject.Proxy.img.copy()
    except: img=cv2.imread(__dir__+'/icons/freek.png')

    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

    lower=np.array([obj.valueColor-obj.deltaColor,0,0])
    upper=np.array([obj.valueColor+obj.deltaColor,255,255])
    mask = cv2.inRange(hsv, lower, upper)

    res = cv2.bitwise_and(hsv,hsv, mask= mask)

    obj.Proxy.img=res
项目:SelfDrivingCar    作者:aguijarro    | 项目源码 | 文件源码
def region_of_interest(img, vertices):
    """
    Applies an image mask.
    Only keeps the region of the image defined by the polygon
    formed from `vertices`. The rest of the image is set to black.
    """
    # defining a blank mask to start with
    mask = np.zeros_like(img)
    # defining a 3 channel or 1 channel color to fill the mask with depending on the input image
    if len(img.shape) > 2:
        channel_count = img.shape[2]  # i.e. 3 or 4 depending on your image
        ignore_mask_color = (255,) * channel_count
    else:
        ignore_mask_color = 255
    # filling pixels inside the polygon defined by "vertices" with the fill color
    cv2.fillPoly(mask, vertices, ignore_mask_color)
    # returning the image only where mask pixels are nonzero
    masked_image = cv2.bitwise_and(img, mask)
    return masked_image
项目:bot2017Fin    作者:AllanYiin    | 项目源码 | 文件源码
def equal_color(img: Image, color):
    arr_img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
    arr_img = cv2.resize(arr_img, (img.size[0] * 10, img.size[1] * 10))
    boundaries = []
    boundaries.append(([max(color[2] - 15, 0), max(color[1] - 15, 0), max(color[0] - 15, 0)],
                       [min(color[2] + 15, 255), min(color[1] + 15, 255), min(color[0] + 15, 255)]))
    for (lower, upper) in boundaries:
        lower = np.array(lower, dtype="uint8")
        upper = np.array(upper, dtype="uint8")

        # find the colors within the specified boundaries and apply
        # the mask
        mask = cv2.inRange(arr_img, lower, upper)
        res = cv2.bitwise_and(arr_img, arr_img, mask=mask)
        res = cv2.resize(res, (img.size[0], img.size[1]))
        cv2_im = cv2.cvtColor(res, cv2.COLOR_BGR2RGB)
        output_img = Image.fromarray(cv2_im)

        return output_img
项目:DeepFryBot    作者:asdvek    | 项目源码 | 文件源码
def find_chars(img):
    gray = np.array(img.convert("L"))
    ret, mask = cv2.threshold(gray, 180, 255, cv2.THRESH_BINARY)
    image_final = cv2.bitwise_and(gray, gray, mask=mask)
    ret, new_img = cv2.threshold(image_final, 180, 255, cv2.THRESH_BINARY_INV)
    kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3))
    dilated = cv2.dilate(new_img, kernel, iterations=1)
    # Image.fromarray(dilated).save('out.png') # for debugging
    _, contours, hierarchy = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

    coords = []
    for contour in contours:
        # get rectangle bounding contour
        [x, y, w, h] = cv2.boundingRect(contour)
        # ignore large chars (probably not chars)
        if w > 70 and h > 70:
            continue
        coords.append((x, y, w, h))
    return coords


# find list of eye coordinates in image
项目:AutonomousParking    作者:jovanduy    | 项目源码 | 文件源码
def overlay_img(self):
        """Overlay the transparent, transformed image of the arc onto our CV image"""
        #overlay the arc on the image
        rows, cols, channels = self.transformed.shape
        roi = self.cv_image[0:rows, 0:cols]

        #change arc_image to grayscale
        arc2gray = cv2.cvtColor(self.transformed, cv2.COLOR_BGR2GRAY)
        ret, mask = cv2.threshold(arc2gray, 10, 255, cv2.THRESH_BINARY)
        mask_inv = cv2.bitwise_not(mask)

        #black out area of arc in ROI
        img1_bg = cv2.bitwise_and(roi, roi, mask=mask_inv)
        img2_fg = cv2.bitwise_and(self.transformed, self.transformed, mask=mask)

        #put arc on ROI and modify the main image
        dst = cv2.add(img1_bg, img2_fg)
        self.cv_image[0:rows, 0:cols] = dst
项目:python-image-processing    作者:karaage0703    | 项目源码 | 文件源码
def extract_color( src, h_th_low, h_th_up, s_th, v_th ):
    hsv = cv2.cvtColor(src, cv2.COLOR_BGR2HSV)
    h, s, v = cv2.split(hsv)
    if h_th_low > h_th_up:
        ret, h_dst_1 = cv2.threshold(h, h_th_low, 255, cv2.THRESH_BINARY) 
        ret, h_dst_2 = cv2.threshold(h, h_th_up,  255, cv2.THRESH_BINARY_INV)

        dst = cv2.bitwise_or(h_dst_1, h_dst_2)
    else:
        ret, dst = cv2.threshold(h,   h_th_low, 255, cv2.THRESH_TOZERO) 
        ret, dst = cv2.threshold(dst, h_th_up,  255, cv2.THRESH_TOZERO_INV)
        ret, dst = cv2.threshold(dst, 0, 255, cv2.THRESH_BINARY)

    ret, s_dst = cv2.threshold(s, s_th, 255, cv2.THRESH_BINARY)
    ret, v_dst = cv2.threshold(v, v_th, 255, cv2.THRESH_BINARY)
    dst = cv2.bitwise_and(dst, s_dst)
    dst = cv2.bitwise_and(dst, v_dst)
    return dst
项目:histonets-cv    作者:sul-cidr    | 项目源码 | 文件源码
def output_as_mask(f):
    """Decorator to add a return_mask option when image and mask are being
    returned"""

    def wrapper(*args, **kwargs):
        return_mask = kwargs.pop('return_mask', False)
        image, mask = f(*args, **kwargs)
        if return_mask:
            return mask
        else:
            return cv2.bitwise_and(image, image, mask=mask)

    wrapper.__name__ = f.__name__
    wrapper.__doc__ = """{}
    The binary mask can also be returned instead by setting
    return_mask to True.""".format(f.__doc__)
    return wrapper
项目:diy_driverless_car_ROS    作者:wilselby    | 项目源码 | 文件源码
def binary_thresh( img,  boundaries,  filter):

     if filter == 'RGB':
         frame_to_thresh = img.copy()
     else:
         frame_to_thresh = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

     for (lower,  upper) in boundaries:

         # create numpy arrays from the boundaries
         lower = np.array(lower,  dtype = "uint8")
         upper = np.array(upper,  dtype = "uint8")

         # find the colors within the specified boundaries and apply the mask
         mask = cv2.inRange(frame_to_thresh,  lower,  upper)
         output = cv2.bitwise_and(frame_to_thresh, frame_to_thresh,  mask = mask)   #Returns an RGB image

     return mask
项目:diy_driverless_car_ROS    作者:wilselby    | 项目源码 | 文件源码
def region_of_interest(img, vertices):
    """
    Applies an image mask.

    Only keeps the region of the image defined by the polygon
    formed from `vertices`. The rest of the image is set to black.
    """
    #defining a blank mask to start with
    mask = np.zeros_like(img)   

    #defining a 3 channel or 1 channel color to fill the mask with depending on the input image
    if len(img.shape) > 2:
        channel_count = img.shape[2]  # i.e. 3 or 4 depending on your image
        ignore_mask_color = (255,) * channel_count
    else:
        ignore_mask_color = 255

    #filling pixels inside the polygon defined by "vertices" with the fill color    
    cv2.fillPoly(mask, vertices, ignore_mask_color)

    #returning the image only where mask pixels are nonzero
    masked_image = cv2.bitwise_and(img, mask)
    return masked_image
项目:cv_build18    作者:eknight7    | 项目源码 | 文件源码
def print_hsv(x):
    if x == 1:
        H_low = cv2.getTrackbarPos("H_low", "Segmented")
        S_low = cv2.getTrackbarPos("S_low", "Segmented")
        V_low = cv2.getTrackbarPos("V_low", "Segmented")
        H_high = cv2.getTrackbarPos("H_high", "Segmented")
        S_high = cv2.getTrackbarPos("S_high", "Segmented")
        V_high = cv2.getTrackbarPos("V_high", "Segmented")

        low = np.array([H_low, S_low, V_low])
        high = np.array([H_high, S_high, V_high])
        print "HSV Low: ", low, ", High: ", high

        save_name = 'seg' + '_lh_' + str(low[0]) + '_ls_' + str(low[1]) + '_lv_' + str(low[2]) \
                    + '_hh_' + str(high[0]) + '_hs_' + str(high[1]) + '_hv_' + str(high[2]) \
                    + '_' + img_str

        mask = cv2.inRange(birdsHSV, low, high)
        result = cv2.bitwise_and(birdsImg, birdsImg, mask = mask)
        res_name = '../results/' + save_name
        print "Saving result as", res_name
        cv2.imwrite(res_name, result)
项目:Conquest_kshitij    作者:pigeon-kgp    | 项目源码 | 文件源码
def blob__Detec(image):
    img=copy(image)
    height, width, channels = img.shape
    new_img=np.ones((height,width,channels), np.uint8)
    HSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    Yellow={'min':(20,100,100),'max':(30, 255, 255)}
    Blue={'min':(50,100,100),'max':(100,255,255)}
    Brown={'min':(0,100,0),'max':(20,255,255)}



    mask_b=cv2.inRange(HSV,Blue['min'],Blue['max'])
    mask_br=cv2.inRange(HSV,Brown['min'],Brown['max'])
    mask_y=cv2.inRange(HSV,Yellow['min'],Yellow['max'])

    blue=cv2.bitwise_and(img,img,mask=mask_b)
    yellow=cv2.bitwise_and(img,img,mask=mask_y)
    brown=cv2.bitwise_and(img,img,mask=mask_br)

    new_img=cv2.add(blue,brown)
    new_img=cv2.add(new_img,yellow)

    return new_img
项目:flight-stone    作者:asmateus    | 项目源码 | 文件源码
def hsvModer(self, index, hsv_valueT, hsv_value_B):
        img_BGR = self.img[index]
        img_RGB = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2RGB)
        img_HSV = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2HSV)

        lower_red = np.array(hsv_value_B)
        upper_red = np.array(hsv_valueT)

        mask = cv2.inRange(img_HSV, lower_red, upper_red)
        res = cv2.bitwise_and(img_RGB, img_RGB, mask=mask)
        if self.erosion:
            kernel = np.ones((5, 5), np.uint8)
            res = cv2.erode(res, kernel, iterations=1)
        if self.dilate:
            kernel = np.ones((9, 9), np.uint8)
            res = cv2.dilate(res, kernel, iterations=1)

        return res
项目:UAV-ODF    作者:rixmit    | 项目源码 | 文件源码
def matchDetectionsOverlap(self, detections, truthDetections, frameSize):
        self.log.info("Calculating overlap for each detection..")

        truthCanvas = np.zeros((frameSize[0], frameSize[1], 1), np.uint8)
        for truthDetection in truthDetections:
            truthCanvas[truthDetection['ymin']:truthDetection['ymax'], truthDetection['xmin']:truthDetection['xmax']] = 255

        overlapRatios = []
        for detection in detections:
            detectionCanvas = np.zeros((frameSize[0], frameSize[1], 1), np.uint8)
            detectionCanvas[detection['ymin']:detection['ymax'], detection['xmin']:detection['xmax']] = 255

            canvas = cv2.bitwise_and(detectionCanvas, truthCanvas)
            detectionCount = np.count_nonzero(detectionCanvas)
            overlapCount = np.count_nonzero(canvas)
            overlap = (overlapCount*1.0)/detectionCount

            overlapRatios.append(overlap)

        return overlapRatios
项目:PicFilter    作者:dhuadaar    | 项目源码 | 文件源码
def render(self,frame):
        numDownSamples = 2
        img_rgb = frame
        # number of downscaling steps
        numBilateralFilters = 7
        # number of bilateral filtering steps
        # -- STEP 1 --
        # downsample image using Gaussian pyramid
        img_color = img_rgb
        for _ in xrange(numDownSamples):
            img_color = cv2.pyrDown(img_color)
        # repeatedly apply small bilateral filter instead of applying
        # one large filter
        for _ in xrange(numBilateralFilters):
            img_color = cv2.bilateralFilter(img_color, 9, 9, 7)

        # upsample image to original size
        for _ in xrange(numDownSamples):
            img_color = cv2.pyrUp(img_color)
        # convert to grayscale and apply median blur
        img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2GRAY)
        img_blur = cv2.medianBlur(img_gray, 7)

        # detect and enhance edges
        img_edge = cv2.adaptiveThreshold(img_blur, 255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY, 9, 2)
        # -- STEP 5 --
        # convert back to color so that it can be bit-ANDed with color image
        img_edge = cv2.cvtColor(img_edge, cv2.COLOR_GRAY2RGB)
        final = cv2.bitwise_and(img_color, img_edge)
        return cv2.medianBlur(final,7)
项目:Self-Driving-Car-ND-Predict-Steering-Angle-with-CV    作者:sjamthe    | 项目源码 | 文件源码
def yellowgrayscale(image):
    #enhance yellow then find grayscale

    #image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    # define range of yellow color in HSV
    #lower = np.array([40,40,40])
    #upper = np.array([150,255,255])

    #RGB limits
    lower = np.array([80,80,40])
    upper = np.array([255,255,80])

    # Threshold the HSV image to get only yellow colors
    mask = cv2.inRange(image, lower, upper)
    #show_image('mask',mask)

    # Bitwise-AND mask and original image
    res = cv2.bitwise_and(image,image, mask= mask)
    res = cv2.addWeighted(res, 1.0, image, 1.0, 0)
    res = grayscale(res)

    return res
项目:Self-Driving-Car-ND-Predict-Steering-Angle-with-CV    作者:sjamthe    | 项目源码 | 文件源码
def region_of_interest(img, vertices):
    """
    Applies an image mask.

    Only keeps the region of the image defined by the polygon
    formed from `vertices`. The rest of the image is set to black.
    """
    #defining a blank mask to start with
    mask = np.zeros_like(img)

    #defining a 3 channel or 1 channel color to fill the mask with depending on the input image
    if len(img.shape) > 2:
        channel_count = img.shape[2]  # i.e. 3 or 4 depending on your image
        ignore_mask_color = (255,) * channel_count
    else:
        ignore_mask_color = 255

    #filling pixels inside the polygon defined by "vertices" with the fill color
    cv2.fillPoly(mask, vertices, ignore_mask_color)

    #returning the image only where mask pixels are nonzero
    masked_image = cv2.bitwise_and(img, mask)
    return masked_image
项目:autonomous_driving    作者:StatueFungus    | 项目源码 | 文件源码
def filter_color(self, image, lower_color, upper_color):
        '''
            Methode maskiert Bereiche auf einem Bild welche nicht im mitgegebenen
            HSV Farbraum liegen.

            Parameter
            ---------
            image : Bild
            lower_color : Tupel
                >> (h,s,v)
            upper_color : Tupel
                >> (h,s,v)

            Rückgabe
            ---------
            image : Bild

        '''
        hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
        lower_color = np.array(lower_color, np.uint8)
        upper_color = np.array(upper_color, np.uint8)

        color_mask = cv2.inRange(hsv, lower_color, upper_color)
        return cv2.bitwise_and(image, image, mask=color_mask)
项目:robik    作者:RecunchoMaker    | 项目源码 | 文件源码
def get_color(self, frame, color1, color2):
        mask = cv2.inRange(self.hsv, color1, color2)
        res = cv2.bitwise_and(frame, frame, mask = mask)
        return res
项目:detection-2016-nipsws    作者:imatge-upc    | 项目源码 | 文件源码
def calculate_iou(img_mask, gt_mask):
    gt_mask *= 1.0
    img_and = cv2.bitwise_and(img_mask, gt_mask)
    img_or = cv2.bitwise_or(img_mask, gt_mask)
    j = np.count_nonzero(img_and)
    i = np.count_nonzero(img_or)
    iou = float(float(j)/float(i))
    return iou
项目:detection-2016-nipsws    作者:imatge-upc    | 项目源码 | 文件源码
def calculate_overlapping(img_mask, gt_mask):
    gt_mask *= 1.0
    img_and = cv2.bitwise_and(img_mask, gt_mask)
    j = np.count_nonzero(img_and)
    i = np.count_nonzero(gt_mask)
    overlap = float(float(j)/float(i))
    return overlap
项目:Vision2016    作者:Team3309    | 项目源码 | 文件源码
def hsv_threshold(img, hue_min, hue_max, sat_min, sat_max, val_min, val_max):
    """
    Threshold an HSV image given separate min/max values for each channel.
    :param img: an hsv image
    :param hue_min:
    :param hue_max:
    :param sat_min:
    :param sat_max:
    :param val_min:
    :param val_max:
    :return: result of the threshold (each binary channel AND'ed together)
    """

    hue, sat, val = cv2.split(img)

    hue_bin = np.zeros(hue.shape, dtype=np.uint8)
    sat_bin = np.zeros(sat.shape, dtype=np.uint8)
    val_bin = np.zeros(val.shape, dtype=np.uint8)

    cv2.inRange(hue, hue_min, hue_max, hue_bin)
    cv2.inRange(sat, sat_min, sat_max, sat_bin)
    cv2.inRange(val, val_min, val_max, val_bin)

    bin = np.copy(hue_bin)
    cv2.bitwise_and(sat_bin, bin, bin)
    cv2.bitwise_and(val_bin, bin, bin)

    return bin
项目:CE264-Computer_Vision    作者:RobinCPC    | 项目源码 | 文件源码
def background_subtract(self, img_src):
        fgmask = self.fgbg.apply(cv2.GaussianBlur(img_src, (25, 25), 0))
        kernel = np.ones((5, 5), np.uint8)
        fgmask = cv2.dilate(fgmask, kernel, iterations=2)
        #fgmask = self.fgbg.apply(cv2.medianBlur(img_src, 11))
        org_fg = cv2.bitwise_and(img_src, img_src, mask=fgmask)
        return org_fg

# Update Position of ROI
项目:piwall-cvtools    作者:infinnovation    | 项目源码 | 文件源码
def maskLogoOverImage(self):
        # Load two images
        img1 = cv2.imread('messi5.jpg')
        img2 = cv2.imread('opencv_logo.png')

        # I want to put logo on top-left corner, So I create a ROI
        rows,cols,channels = img2.shape
        roi = img1[0:rows, 0:cols ]

        # Now create a mask of logo and create its inverse mask also
        img2gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
        ret, mask = cv2.threshold(img2gray, 10, 255, cv2.THRESH_BINARY)
        mask_inv = cv2.bitwise_not(mask)

        # Now black-out the area of logo in ROI
        img1_bg = cv2.bitwise_and(roi,roi,mask = mask_inv)

        # Take only region of logo from logo image.
        img2_fg = cv2.bitwise_and(img2,img2,mask = mask)

        # Put logo in ROI and modify the main image
        dst = cv2.add(img1_bg,img2_fg)
        img1[0:rows, 0:cols ] = dst

        cv2.imshow('res',img1)
        cv2.waitKey(0)
        cv2.destroyAllWindows()


#####################################################################################################################
# Prototypes & Convenient CLI/GUI Dispatcher to rebuild mental picture of where we are/repeat on new platforms.
#####################################################################################################################
项目:Yugioh-bot    作者:will7200    | 项目源码 | 文件源码
def mask_image(lower_mask, upper_mask, img, apply_mask=False):
    """" Masks an image according to the upper and lower bounds
    Parameters
    ----------
    lower_mask : ndarray
        lower mask to apply to image, length must match image channels
    upper_mask : ndarray
        upper mask to apply to image, length must match image channels
    img :  ndarray
        image to apply mask to
    apply_mask : bool
        returns the masked image instead of the mask itself
    """
    shape = np.array(img.shape).flatten()
    if len(np.array(img.shape).flatten()) == 3:
        shape_size = shape[-1]
    else:
        shape_size = 1
    assert (len(lower_mask) == shape_size)
    assert (len(upper_mask) == shape_size)
    color_min = np.array(lower_mask, np.uint8)
    color_max = np.array(upper_mask, np.uint8)
    new_img = cv2.inRange(img, color_min, color_max)
    if apply_mask:
        return cv2.bitwise_and(img, img, mask=new_img)
    return new_img
项目:skastic    作者:mypalmike    | 项目源码 | 文件源码
def text_mask_img(self):
    img = self.img_grey.copy()
    mask = np.ones(img.shape[:2], dtype="uint8") * 255
    for contour_node in self.contour_nodes:
      cv2.drawContours(mask, contour_node.contour, -1, 0, -1)
    image = cv2.bitwise_and(img, img, mask=mask)
    return img
项目:Simple-Lane-Detection-System    作者:shivamsardana    | 项目源码 | 文件源码
def region_of_interest(img, vertices):
    """

    Applies an image mask.

    Only keeps the region of the image defined by the polygon
    formed from `vertices`. The rest of the image is set to black.
    """

    # defining a blank mask to start with

    mask = np.zeros_like(img)

    # defining a 3 channel or 1 channel color to fill the mask with depending on the input image

    if len(img.shape) > 2:
        channel_count = img.shape[2]  # i.e. 3 or 4 depending on your image
        ignore_mask_color = (255,) * channel_count
    else:
        ignore_mask_color = 255

    # filling pixels inside the polygon defined by "vertices" with the fill color

    cv2.fillPoly(mask, vertices, ignore_mask_color)

    # returning the image only where mask pixels are nonzero

    masked_image = cv2.bitwise_and(img, mask)
    return masked_image
项目:retinal-exudates-detection    作者:getsanjeev    | 项目源码 | 文件源码
def extract_bv(image):          
    clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
    contrast_enhanced_green_fundus = clahe.apply(image)
    # applying alternate sequential filtering (3 times closing opening)
    r1 = cv2.morphologyEx(contrast_enhanced_green_fundus, cv2.MORPH_OPEN, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5)), iterations = 1)
    R1 = cv2.morphologyEx(r1, cv2.MORPH_CLOSE, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5)), iterations = 1)
    r2 = cv2.morphologyEx(R1, cv2.MORPH_OPEN, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(11,11)), iterations = 1)
    R2 = cv2.morphologyEx(r2, cv2.MORPH_CLOSE, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(11,11)), iterations = 1)
    r3 = cv2.morphologyEx(R2, cv2.MORPH_OPEN, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(23,23)), iterations = 1)
    R3 = cv2.morphologyEx(r3, cv2.MORPH_CLOSE, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(23,23)), iterations = 1)
    f4 = cv2.subtract(R3,contrast_enhanced_green_fundus)
    f5 = clahe.apply(f4)

    # removing very small contours through area parameter noise removal
    ret,f6 = cv2.threshold(f5,15,255,cv2.THRESH_BINARY)
    mask = np.ones(f5.shape[:2], dtype="uint8") * 255
    im2, contours, hierarchy = cv2.findContours(f6.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
    for cnt in contours:
        if cv2.contourArea(cnt) <= 200:
            cv2.drawContours(mask, [cnt], -1, 0, -1)            
    im = cv2.bitwise_and(f5, f5, mask=mask)
    ret,fin = cv2.threshold(im,15,255,cv2.THRESH_BINARY_INV)            
    newfin = cv2.erode(fin, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3)), iterations=1)   

    # removing blobs of microaneurysm & unwanted bigger chunks taking in consideration they are not straight lines like blood
    # vessels and also in an interval of area
    fundus_eroded = cv2.bitwise_not(newfin)
    xmask = np.ones(image.shape[:2], dtype="uint8") * 255
    x1, xcontours, xhierarchy = cv2.findContours(fundus_eroded.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)    
    for cnt in xcontours:
        shape = "unidentified"
        peri = cv2.arcLength(cnt, True)
        approx = cv2.approxPolyDP(cnt, 0.04 * peri, False)
        if len(approx) > 4 and cv2.contourArea(cnt) <= 3000 and cv2.contourArea(cnt) >= 100:
            shape = "circle"    
        else:
            shape = "veins"
        if(shape=="circle"):
            cv2.drawContours(xmask, [cnt], -1, 0, -1)   

    finimage = cv2.bitwise_and(fundus_eroded,fundus_eroded,mask=xmask)  
    blood_vessels = cv2.bitwise_not(finimage)
    dilated = cv2.erode(blood_vessels, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(7,7)), iterations=1)
    #dilated1 = cv2.dilate(blood_vessels, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3)), iterations=1)
    blood_vessels_1 = cv2.bitwise_not(dilated)
    return blood_vessels_1
项目:retinal-exudates-detection    作者:getsanjeev    | 项目源码 | 文件源码
def extract_bv(image):
    clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
    contrast_enhanced_green_fundus = clahe.apply(image)
    # applying alternate sequential filtering (3 times closing opening)
    r1 = cv2.morphologyEx(contrast_enhanced_green_fundus, cv2.MORPH_OPEN, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5)), iterations = 1)
    R1 = cv2.morphologyEx(r1, cv2.MORPH_CLOSE, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5)), iterations = 1)
    r2 = cv2.morphologyEx(R1, cv2.MORPH_OPEN, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(11,11)), iterations = 1)
    R2 = cv2.morphologyEx(r2, cv2.MORPH_CLOSE, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(11,11)), iterations = 1)
    r3 = cv2.morphologyEx(R2, cv2.MORPH_OPEN, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(23,23)), iterations = 1)
    R3 = cv2.morphologyEx(r3, cv2.MORPH_CLOSE, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(23,23)), iterations = 1)
    f4 = cv2.subtract(R3,contrast_enhanced_green_fundus)
    f5 = clahe.apply(f4)

    # removing very small contours through area parameter noise removal
    ret,f6 = cv2.threshold(f5,15,255,cv2.THRESH_BINARY)
    mask = np.ones(f5.shape[:2], dtype="uint8") * 255
    im2, contours, hierarchy = cv2.findContours(f6.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
    for cnt in contours:
        if cv2.contourArea(cnt) <= 200:
            cv2.drawContours(mask, [cnt], -1, 0, -1)            
    im = cv2.bitwise_and(f5, f5, mask=mask)
    ret,fin = cv2.threshold(im,15,255,cv2.THRESH_BINARY_INV)            
    newfin = cv2.erode(fin, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3)), iterations=1)   

    # removing blobs of microaneurysm & unwanted bigger chunks taking in consideration they are not straight lines like blood
    # vessels and also in an interval of area
    fundus_eroded = cv2.bitwise_not(newfin)
    xmask = np.ones(image.shape[:2], dtype="uint8") * 255
    x1, xcontours, xhierarchy = cv2.findContours(fundus_eroded.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)    
    for cnt in xcontours:
        shape = "unidentified"
        peri = cv2.arcLength(cnt, True)
        approx = cv2.approxPolyDP(cnt, 0.04 * peri, False)
        if len(approx) > 4 and cv2.contourArea(cnt) <= 3000 and cv2.contourArea(cnt) >= 100:
            shape = "circle"    
        else:
            shape = "veins"
        if(shape=="circle"):
            cv2.drawContours(xmask, [cnt], -1, 0, -1)   

    finimage = cv2.bitwise_and(fundus_eroded,fundus_eroded,mask=xmask)  
    blood_vessels = cv2.bitwise_not(finimage)
    dilated = cv2.erode(blood_vessels, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(7,7)), iterations=1)
    #dilated1 = cv2.dilate(blood_vessels, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3)), iterations=1)
    blood_vessels_1 = cv2.bitwise_not(dilated)
    return blood_vessels_1
项目:UVA    作者:chiachun    | 项目源码 | 文件源码
def skin_filter(cfg, vd):
    df = pd.read_csv(vd.photo_csv, index_col=0)
    numbers = df.number.tolist()
    notface = []
    for number in numbers:
        lower = np.array([0, 48, 80], dtype = "uint8")
        upper = np.array([13, 255, 255], dtype = "uint8")
        image = cv2.imread('%s/%d.png' % (vd.photo_dir, number), cv2.IMREAD_COLOR)
        converted = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
        skinMask = cv2.inRange(converted, lower, upper)

        # apply a series of erosions and dilations to the mask
        # using an elliptical kernel
        kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11, 11))
        skinMask = cv2.erode(skinMask, kernel, iterations = 2)
        skinMask = cv2.dilate(skinMask, kernel, iterations = 2)

        # blur the mask to help remove noise, then apply the
        # mask to the frame
        skinMask = cv2.GaussianBlur(skinMask, (3, 3), 0)
        skin = cv2.bitwise_and(image, image, mask = skinMask)
        if len(skin.nonzero()[0]) < cfg.min_skin_pixels:
            notface.append(number)
    print '%d/%d are faces' % ( len(df) - len(notface), len(df) )
    df['face']= 1
    df.loc[df.number.isin(notface),'face'] = -99
    df.to_csv(vd.photo_csv)
项目:vbcg    作者:nspi    | 项目源码 | 文件源码
def __add_figure_to_frame(self, frame, figure_location):
        """This function is used to add a file from hard disk to the figure
        Algorithm source: http://docs.opencv.org/trunk/d0/d86/tutorial_py_image_arithmetics.html
        """
        # Get size of frame
        height, width, channels = frame.shape

        # Only add icon when the frame is big enough
        if height >= 100 and width >= 100:

            # Load heart icon
            icon_heart = cv2.imread(figure_location)
            # Convert to RGB
            icon_heart = cv2.cvtColor(icon_heart, cv2.COLOR_BGR2RGB)
            # Create ROI
            rows, cols, channels = icon_heart.shape
            roi = frame[:rows, :cols, :]
            # Convert heart to greyscale
            icon_heart_gray = cv2.cvtColor(icon_heart, cv2.COLOR_RGB2GRAY)
            # Create mask and inverse mask with binary threshold
            ret, mask = cv2.threshold(icon_heart_gray, 10, 255, cv2.THRESH_BINARY)
            mask_inv = cv2.bitwise_not(mask)
            # Background: Original frame with inverse mask
            frame_bg = cv2.bitwise_and(roi, roi, mask=mask_inv)
            # Foreground: Heart with normal mask
            icon_heart_fg = cv2.bitwise_and(icon_heart, icon_heart, mask=mask)
            # Add heart icon to frame
            icon_heart_final = cv2.add(frame_bg, icon_heart_fg)
            frame[:rows, :cols, :] = icon_heart_final

        return frame

    # Setter and getter following
项目:website-4mb    作者:dettoman    | 项目源码 | 文件源码
def diffImg(t0, t1, t2):
    d1 = cv2.absdiff(t2, t1)
    d2 = cv2.absdiff(t1, t0)
    return cv2.bitwise_and(d1, d2)


#Form Config
项目:andruinoR2    作者:andruino    | 项目源码 | 文件源码
def image_callback(self, msg):
    # BEGIN BRIDGE
    image = self.bridge.imgmsg_to_cv2(msg)
    # END BRIDGE
    # BEGIN HSV
    hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    # END HSV
    # BEGIN FILTER
    lower_yellow = numpy.array([18, 120, 200])
    upper_yellow = numpy.array([28, 255, 255])
    mask = cv2.inRange(hsv, lower_yellow, upper_yellow)
    # END FILTER
    masked = cv2.bitwise_and(image, image, mask=mask)
    cv2.imshow("window", mask ) 
    cv2.waitKey(3)
项目:Artificial-Potential-Field    作者:vampcoder    | 项目源码 | 文件源码
def find_robot(im):
    hsv = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)
    lower = np.array([50, 28, 0])
    upper = np.array([60, 168, 255])
    mask = cv2.inRange(hsv, lower, upper)
    result = cv2.bitwise_and(im, im, mask=mask)
    blur = cv2.blur(result, (5, 5))
    bw = cv2.cvtColor(blur, cv2.COLOR_HSV2BGR)
    bw2 = cv2.cvtColor(bw, cv2.COLOR_BGR2GRAY)
    ret, th3 = cv2.threshold(bw2, 30, 255, cv2.THRESH_BINARY)
    edges = cv2.Canny(th3, 100, 200)
    th4 = copy.copy(th3)
    perimeter = 0
    j = 0
    image, contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
    cnt = np.array([])
    for i in range(len(contours)):
        if (perimeter < cv2.contourArea(contours[i])):
            perimeter = cv2.contourArea(contours[i])
            j = i;
            cnt = contours[j]

    x = 0
    y = 0
    for i in range(len(cnt)):
        x = x + cnt[i][0][0]
        y = y + cnt[i][0][1]
    x = x / len(cnt)
    y = y / len(cnt)
    #print x, y
    x = int(x)
    y = int(y)
    cv2.circle(im, (x, y), 5, (255, 0, 255), 2)
    #show_image(im)
    return (int(x), int(y))
项目:Artificial-Potential-Field    作者:vampcoder    | 项目源码 | 文件源码
def find_robot(frame):
    im = copy.copy(frame)
    hsv = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)
    lower = np.array([50, 28, 0])
    upper = np.array([60, 168, 255])
    mask = cv2.inRange(hsv, lower, upper)
    result = cv2.bitwise_and(im, im, mask=mask)
    blur = cv2.blur(result, (5, 5))
    bw = cv2.cvtColor(blur, cv2.COLOR_HSV2BGR)
    bw2 = cv2.cvtColor(bw, cv2.COLOR_BGR2GRAY)
    ret, th3 = cv2.threshold(bw2, 30, 255, cv2.THRESH_BINARY)
    edges = cv2.Canny(th3, 100, 200)
    th4 = copy.copy(th3)
    perimeter = 0
    j = 0
    image, contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
    cnt = np.array([])
    for i in range(len(contours)):
        if (perimeter < cv2.contourArea(contours[i])):
            perimeter = cv2.contourArea(contours[i])
            j = i;
            cnt = contours[j]

    x = 0
    y = 0
    for i in range(len(cnt)):
        x = x + cnt[i][0][0]
        y = y + cnt[i][0][1]
    x = x / len(cnt)
    y = y / len(cnt)
    #print x, y
    x = int(x)
    y = int(y)
    cv2.circle(im, (x, y), 5, (255, 0, 255), 2)
    cv2.imshow('img', im)
    k = cv2.waitKey(0)
    cv2.imwrite('robot.jpg', im)
    #show_image(im)
    return (int(x), int(y))
项目:dream2016_dm    作者:lishen    | 项目源码 | 文件源码
def suppress_artifacts(self, img, global_threshold=.05, fill_holes=False, 
                           smooth_boundary=True, kernel_size=15):
        '''Mask artifacts from an input image
        Artifacts refer to textual markings and other small objects that are 
        not related to the breast region.

        Args:
            img (2D array): input image as a numpy 2D array.
            global_threshold ([int]): a global threshold as a cutoff for low 
                    intensities for image binarization. Default is 18.
            kernel_size ([int]): kernel size for morphological operations. 
                    Default is 15.
        Returns:
            a tuple of (output_image, breast_mask). Both are 2D numpy arrays.
        '''
        maxval = self.max_pix_val(img.dtype)
        if global_threshold < 1.:
            low_th = int(img.max()*global_threshold)
        else:
            low_th = int(global_threshold)
        _, img_bin = cv2.threshold(img, low_th, maxval=maxval, 
                                   type=cv2.THRESH_BINARY)
        breast_mask = self.select_largest_obj(img_bin, lab_val=maxval, 
                                              fill_holes=True, 
                                              smooth_boundary=True, 
                                              kernel_size=kernel_size)
        img_suppr = cv2.bitwise_and(img, breast_mask)

        return (img_suppr, breast_mask)
项目:dream2016_dm    作者:lishen    | 项目源码 | 文件源码
def segment_breast(cls, img, low_int_threshold=.05, crop=True):
        '''Perform breast segmentation
        Args:
            low_int_threshold([float or int]): Low intensity threshold to 
                    filter out background. It can be a fraction of the max 
                    intensity value or an integer intensity value.
            crop ([bool]): Whether or not to crop the image.
        Returns:
            An image of the segmented breast.
        NOTES: the low_int_threshold is applied to an image of dtype 'uint8',
            which has a max value of 255.
        '''
        # Create img for thresholding and contours.
        img_8u = (img.astype('float32')/img.max()*255).astype('uint8')
        if low_int_threshold < 1.:
            low_th = int(img_8u.max()*low_int_threshold)
        else:
            low_th = int(low_int_threshold)
        _, img_bin = cv2.threshold(
            img_8u, low_th, maxval=255, type=cv2.THRESH_BINARY)
        ver = (cv2.__version__).split('.')
        if int(ver[0]) < 3:
            contours,_ = cv2.findContours(
                img_bin.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
        else:
            _,contours,_ = cv2.findContours(
                img_bin.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
        cont_areas = [ cv2.contourArea(cont) for cont in contours ]
        idx = np.argmax(cont_areas)  # find the largest contour, i.e. breast.
        breast_mask = cv2.drawContours(
            np.zeros_like(img_bin), contours, idx, 255, -1)  # fill the contour.
        # segment the breast.
        img_breast_only = cv2.bitwise_and(img, img, mask=breast_mask)
        x,y,w,h = cv2.boundingRect(contours[idx])
        if crop:
            img_breast_only = img_breast_only[y:y+h, x:x+w]
        return img_breast_only, (x,y,w,h)
项目:hand-gesture-recognition-opencv    作者:mahaveerverma    | 项目源码 | 文件源码
def remove_bg(frame):
    fg_mask=bg_model.apply(frame)
    kernel = np.ones((3,3),np.uint8)
    fg_mask=cv2.erode(fg_mask,kernel,iterations = 1)
    frame=cv2.bitwise_and(frame,frame,mask=fg_mask)
    return frame

# ------------------------ BEGIN ------------------------ #

# Camera
项目:pdc-project    作者:ealain    | 项目源码 | 文件源码
def screen_position():
    '''
    Returns position of rectangle x,y,w,h if:
        - bounding rectangle is big enough (more than 0.2 % of total captured image)
        - contour's area is more than 70% of the area of the bounding rectangle
    '''
    cap = cv2.VideoCapture(0)
    nb_max_iterations = int(30*ALLOWED_DETECTION_TIME)
    for it in range(nb_max_iterations):
        ret, frame = cap.read()
        mask = detect_color(frame)
        masked_frame = cv2.bitwise_and(frame, frame, mask=mask)
        imgray = cv2.cvtColor(masked_frame, cv2.COLOR_BGR2GRAY)
        contours, hierarchy = cv2.findContours(imgray,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
        contours = sorted(contours, key=lambda x:len(x), reverse=True)
        i = 0
        while(i < len(contours)):
            x,y,w,h = cv2.boundingRect(contours[i])
            rect_cnt = np.array([[[x, y]], [[x+w, y]], [[x+w, y+h]], [[x, y+h]]], dtype=np.int32)
            rect_cnt_area = cv2.contourArea(rect_cnt)
            if(cv2.contourArea(contours[i]) > VALID_AREA_RATIO * rect_cnt_area and \
                    rect_cnt_area > VALID_MIN_AREA and \
                    rect_cnt_area < VALID_MAX_AREA):
                cap.release()
                return x,y,w,h
            else:
                i += 1
    cap.release()
    return -1,-1,-1,-1
项目:pdc-project    作者:ealain    | 项目源码 | 文件源码
def main():
    '''
    Test screen_position
    '''
    cap = cv2.VideoCapture(0)
    while(True):
        ret, frame = cap.read()
        mask = detect_color(frame)
        cv2.imshow('frame', frame)
        cv2.imshow('mask', mask)
        masked_frame = cv2.bitwise_and(frame, frame, mask=mask)
        imgray = cv2.cvtColor(masked_frame, cv2.COLOR_BGR2GRAY)
        contours, hierarchy = cv2.findContours(imgray,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
        contours = sorted(contours, key=lambda x:len(x), reverse=True)
        i = 0
        while(i < len(contours)):
            x,y,w,h = cv2.boundingRect(contours[i])
            rect_cnt = np.array([[[x, y]], [[x+w, y]], [[x+w, y+h]], [[x, y+h]]], dtype=np.int32)
            rect_cnt_area = cv2.contourArea(rect_cnt)
            if(cv2.contourArea(contours[i]) > VALID_AREA_RATIO * rect_cnt_area and \
                    rect_cnt_area > VALID_MIN_AREA and \
                    rect_cnt_area < VALID_MAX_AREA):
                cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
                break
            else:
                i += 1

        cv2.imshow('Contours', frame)
        if cv2.waitKey(1) & 0xFF == 27:
            break

    print(get_screen_gray_level(masked_frame))
    cap.release()
    cv2.destroyAllWindows()
项目:histonets-cv    作者:sul-cidr    | 项目源码 | 文件源码
def test_output_as_mask(self):
        image = 255 * np.ones((10, 10), np.uint8)
        mask = np.zeros(image.shape, np.uint8)
        masked = cv2.bitwise_and(image, image, mask=mask)
        func = utils.output_as_mask(lambda x: (x, mask))
        assert np.array_equal(masked, func(image))
        assert np.array_equal(mask, func(image, return_mask=True))
项目:political-ad-classifier    作者:BoudhayanBanerjee    | 项目源码 | 文件源码
def checkBluePixel(inputPath):
    """
    docstring
    """
    im = cv2.imread(inputPath, 1)
    # Convert BGR to HSV
    hsv = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)
    # define range of blue color in HSV
    lower_blue = np.array([110, 50, 50])
    upper_blue = np.array([130, 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(im, im, mask=mask)
    # save temp image
    cv2.imwrite(os.path.join(TEMP, 'temp.png'), res)
    ct = ColorThief(os.path.join(TEMP, 'temp.png'))
    palette = ct.get_palette(color_count=5)
    for p in palette:
        r = p[0]
        g = p[1]
        b = p[2]
        bgr = np.uint8([[[p[2], p[1], p[0]]]])
        hsv = cv2.cvtColor(bgr, cv2.COLOR_BGR2HSV)
        h = hsv[0][0][0]
        s = hsv[0][0][1]
        v = hsv[0][0][2]
        if ((h >= 110 and h <= 130) and (s >= 50 and s <= 255) and (v >= 50 and v <= 255)):
            return True
            break
项目:political-ad-classifier    作者:BoudhayanBanerjee    | 项目源码 | 文件源码
def checkRedPixel(inputPath):
    """
    docstring
    """
    im = cv2.imread(inputPath, 1)
    # Convert BGR to HSV
    hsv = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)
    # define range of red color in HSV
    lower_red = np.array([0, 100, 100])
    upper_red = np.array([10, 255, 255])
    # Threshold the HSV image to get only red colors
    mask1 = cv2.inRange(hsv, lower_red, upper_red)
    # define range of red color in HSV
    lower_red = np.array([170, 100, 100])
    upper_red = np.array([180, 255, 255])
    # Threshold the HSV image to get only red colors
    mask2 = cv2.inRange(hsv, lower_red, upper_red)
    mask = mask1 + mask2
    # Bitwise-AND mask and original image
    res = cv2.bitwise_and(im, im, mask=mask)
    # save temp image
    cv2.imwrite(os.path.join(TEMP, 'temp.png'), res)
    ct = ColorThief(os.path.join(TEMP, 'temp.png'))
    palette = ct.get_palette(color_count=5)
    for p in palette:
        r = p[0]
        g = p[1]
        b = p[2]
        bgr = np.uint8([[[p[2], p[1], p[0]]]])
        hsv = cv2.cvtColor(bgr, cv2.COLOR_BGR2HSV)
        h = hsv[0][0][0]
        s = hsv[0][0][1]
        v = hsv[0][0][2]
        if ((h >= 0 and h <= 10) and (s >= 100 and s <= 255) and (v >= 100 and v <= 255)) or ((h >= 170 and h <= 180) and (s >= 100 and s <= 255) and (v >= 100 and v <= 255)):
            return True
            break
项目:cv_build18    作者:eknight7    | 项目源码 | 文件源码
def track_obj(low_hsv, high_hsv):
    # Open the "default" camera
    vc = cv2.VideoCapture(0)
    # Check if we succeeded in opening camera feed
    if vc.isOpened():
        rval, frame = vc.read()
    else:
        rval = False
    # Display captured frames in a new window
    cv2.namedWindow("Camera Video Feed")
    # Display filtered object frame in a new window
    cv2.namedWindow("Tracking")

    result = frame

    while rval:
        cv2.imshow("Camera Video Feed", frame)
        cv2.imshow("Tracking", result)
        rval, frame = vc.read()
        # Convert to HSV space
        frameHSV = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
        # Filter out components with values in selected range
        # Threshold HSV image
        mask = cv2.inRange(frameHSV, low_hsv, high_hsv)
        result = cv2.bitwise_and(frame, frame, mask = mask)
        # Wait for ESC key press
        key = cv2.waitKey(20)
        if key == 27: # User pressed ESC key
            break
    # Destroy window
    cv2.destroyWindow("Camera Video Feed")
    # Close VideoCapture feed -- Important!
    vc.release()