Python cv2.CV_32S Examples

The following are 5 code examples of cv2.CV_32S(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module cv2 , or try the search function .
Example #1
Source File: lanenet_postprocess.py    From lanenet-lane-detection with Apache License 2.0 4 votes vote down vote up
def _connect_components_analysis(image):
    """
    connect components analysis to remove the small components
    :param image:
    :return:
    """
    if len(image.shape) == 3:
        gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    else:
        gray_image = image

    return cv2.connectedComponentsWithStats(gray_image, connectivity=8, ltype=cv2.CV_32S) 
Example #2
Source File: tools.py    From ICPR_TextDection with GNU General Public License v3.0 4 votes vote down vote up
def find_connected(score_map, threshold=0.7):
    binary_map = (score_map > threshold).astype(np.uint8)
    connectivity = 8
    output = cv2.connectedComponentsWithStats(binary_map, connectivity=connectivity, ltype=cv2.CV_32S)
    label_map = output[1]
    # show_image(np.asarray(label_map * 100.0, np.uint8))
    return np.max(label_map), label_map 
Example #3
Source File: lanenet_postprocess.py    From lanenet-enet-hnet with Apache License 2.0 4 votes vote down vote up
def _connect_components_analysis(image):
        """

        :param image:
        :return:
        """
        if len(image.shape) == 3:
            gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        else:
            gray_image = image

        return cv2.connectedComponentsWithStats(gray_image, connectivity=8, ltype=cv2.CV_32S) 
Example #4
Source File: mask_morphology.py    From NucleiDetectron with Apache License 2.0 4 votes vote down vote up
def split_mask_erode_dilate(mask, kernel=k_3x3, k=3):
    img_erosion = cv.erode(mask, kernel, iterations=k)
    output = cv.connectedComponentsWithStats(img_erosion, 4, cv.CV_32S)
    if output[0] < 2:
        return [mask], output[1]
    else:
        masks_res = []
        for idx in range(1, output[0]):
            res_m = (output[1] == idx).astype(np.uint8)
            res_m = cv.dilate(res_m, kernel, iterations=k)
            if res_m.sum() > 5:
                masks_res.append(res_m)
        return masks_res, output[1] 
Example #5
Source File: abstract_net.py    From bonnet with GNU General Public License v3.0 3 votes vote down vote up
def obj_histogram(self, mask, label):
    # holders for predicted object and right object (easily calculate histogram)
    predicted = []
    labeled = []

    # get connected components in label for each class
    for i in range(self.num_classes):
      # get binary image for this class
      bin_lbl = np.zeros(label.shape)
      bin_lbl[label == i] = 1
      bin_lbl[label != i] = 0

      # util.im_gray_plt(bin_lbl,'class '+str(i))
      connectivity = 4
      output = cv2.connectedComponentsWithStats(
          bin_lbl.astype(np.uint8), connectivity, cv2.CV_32S)
      num_components = output[0]
      components = output[1]
      stats = output[2]
      centroids = output[3]

      for j in range(1, num_components):  # 0 is background (useless)
        # only process if it has more than 50pix
        if stats[j][cv2.CC_STAT_AREA] > 50:
          # for each component in each class, see the class with the highest percentage of pixels
          # make mask with just this component of this class
          comp_mask = np.zeros(label.shape)
          comp_mask[components == j] = 0
          comp_mask[components != j] = 1
          # mask the prediction
          masked_prediction = np.ma.masked_array(mask, mask=comp_mask)
          # get histogram and get the argmax that is not zero
          class_hist, _ = np.histogram(masked_prediction.compressed(),
                                       bins=self.num_classes, range=[0, self.num_classes])
          max_class = np.argmax(class_hist)
          # print("\nMax class: ",max_class,"  real: ",i)
          # util.im_gray_plt(comp_mask)
          # util.im_block()
          # sum an entry to the containers depending on right or wrong
          predicted.append(max_class)
          labeled.append(i)
    # for idx in range(len(predicted)):
    #   print(predicted[idx],labeled[idx])

    # histogram to count right and wrong objects
    histrange = np.array([[-0.5, self.num_classes - 0.5],
                          [-0.5, self.num_classes - 0.5]], dtype='float64')
    h_now, _, _ = np.histogram2d(np.array(predicted),
                                 np.array(labeled),
                                 bins=self.num_classes,
                                 range=histrange)

    return h_now