def __merge(self, img_even, img_odd, row_col):
     if row_col == 0:
         img_even = cv2.transpose(img_even)
         img_odd = cv2.transpose(img_odd)
     img = array([eo for e in zip(img_even, img_odd) for eo in e])  # merge two matrix
     if row_col == 0:
         img = cv2.transpose(img)
     return img
 def __split(self, origin_img, row_col):
     img = deepcopy(origin_img)
     if row_col == 0:
         img = cv2.transpose(img)
     if len(img) % 2 == 1: # number of lines is odd
         img = r_[img, [img[-1]]]    # copy the last line 
     img_even = img[0::2]
     img_odd = img[1::2]
     if row_col == 0:
         img_even = cv2.transpose(img_even)
         img_odd = cv2.transpose(img_odd)
     return img_even, img_odd
Example #3
0
    def _cv2_trim(self) -> bool:
        """
        Remove black borders from a cv2 image array.

        This method is a f*****g waste of time as most sources are already
        properly cropped. We need to use it because of a few shitty WEB sources.
        F*****g unbelievable.

        :param cv2_image: cv2 image array
        """
        logger.info("Trying to remove black borders with cv2")
        og_w, og_h = self._cv2.shape[1], self._cv2.shape[0]
        logger.debug("Original dimensions: %dx%d", og_w, og_h)
        og_quotient = og_w / og_h

        first_img = _remove_lateral_cv2(self._cv2)

        tmp_img = cv2.transpose(first_img)
        tmp_img = cv2.flip(tmp_img, flipCode=1)

        if tmp_img is None:
            raise exceptions.InvalidRequest("Possible all-black image found")

        final = _remove_lateral_cv2(tmp_img)

        out = cv2.transpose(final)

        final_img = cv2.flip(out, flipCode=0)
        if final_img is None:
            raise exceptions.InvalidRequest("Possible all-black image found")

        new_w, new_h = final_img.shape[1], final_img.shape[0]

        logger.debug("New dimensions: %dx%d", new_w, new_h)
        new_quotient = new_w / new_h

        if abs(new_quotient - og_quotient) > 0.9:
            logger.info("Possible bad quotient found: %s -> %s", og_quotient,
                        new_quotient)
            return False

        width_percent = (100 / og_w) * new_w
        height_percent = (100 / og_h) * new_h

        if any(percent <= 65 for percent in (width_percent, height_percent)):
            logger.info("Possible bad trim found: %s -> %s", width_percent,
                        height_percent)
            return False

        self._cv2 = final_img
        return True
Example #4
0
    def _cv2_trim(self) -> bool:
        """
        Remove black borders from a cv2 image array.

        :param cv2_image: cv2 image array
        """
        logger.info("Trying to remove black borders with cv2")
        og_w, og_h = self.cv2.shape[1], self.cv2.shape[0]
        logger.debug("Original dimensions: %dx%d", og_w, og_h)
        og_quotient = og_w / og_h

        first_img = _remove_lateral_cv2(self.cv2)

        tmp_img = cv2.transpose(first_img)
        tmp_img = cv2.flip(tmp_img, flipCode=1)

        if tmp_img is None:
            raise exceptions.NothingFound("Possible all-black image found")

        final = _remove_lateral_cv2(tmp_img)

        out = cv2.transpose(final)

        final_img = cv2.flip(out, flipCode=0)

        new_w, new_h = final_img.shape[1], final_img.shape[0]

        logger.debug("New dimensions: %dx%d", new_w, new_h)
        new_quotient = new_w / new_h

        if abs(new_quotient - og_quotient) > 0.9:
            logger.info("Possible bad quotient found: %s -> %s", og_quotient,
                        new_quotient)
            return False

        width_percent = (100 / og_w) * new_w
        height_percent = (100 / og_h) * new_h

        if any(percent <= 65 for percent in (width_percent, height_percent)):
            logger.info("Possible bad trim found: %s -> %s", width_percent,
                        height_percent)
            return False

        self.cv2 = final_img
        return True
Example #5
0
def MyHitMiss(imgSrc: np.ndarray, hit: np.ndarray,
              miss: np.ndarray) -> np.ndarray:
    imgDst1 = Erode(imgSrc, hit)
    imgDst2 = Not(Dilate(imgSrc, cv.transpose(miss)))
    imgDst = np.empty(imgSrc.shape, dtype=np.uint8)
    h, w = imgSrc.shape
    for (i, L1, L2) in zip(range(h), imgDst1, imgDst2):
        for (j, item1, item2) in zip(range(w), L1, L2):
            imgDst[i, j] = min(item1, item2)

    return imgDst
def preprocess(img, imgSize, dataAugmentation=False):
    "put img into target img of size imgSize, transpose for TF and normalize gray-values"

    # there are damaged files in IAM dataset - just use black image instead
    if img is None:
        img = np.zeros([imgSize[1], imgSize[0]])

    # increase dataset size by applying random stretches to the images
    if dataAugmentation:
        stretch = (random.random() - 0.5)  # -0.5 .. +0.5
        wStretched = max(int(img.shape[1] * (1 + stretch)),
                         1)  # random width, but at least 1
        img = cv2.resize(
            img, (wStretched,
                  img.shape[0]))  # stretch horizontally by factor 0.5 .. 1.5

    # create target image and copy sample image into it
    (wt, ht) = imgSize
    (h, w) = img.shape
    fx = w / wt
    fy = h / ht
    f = max(fx, fy)
    newSize = (max(min(wt, int(w / f)), 1), max(
        min(ht, int(h / f)),
        1))  # scale according to f (result at least 1 and at most wt or ht)
    img = cv2.resize(img, newSize)
    target = np.ones([ht, wt]) * 255
    target[0:newSize[1], 0:newSize[0]] = img

    # transpose for TF
    img = cv2.transpose(target)

    # normalize
    (m, s) = cv2.meanStdDev(img)
    m = m[0][0]
    s = s[0][0]
    img = img - m
    img = img / s if s > 0 else img
    return img