Пример #1
0
    def __init__(self, resize):

        self.image = MainCVClass(resize).image
        self.accum_edged = np.zeros(self.image.shape[:2], dtype="uint8")

        for chan in cv2.split(self.image):
            chan = cv2.medianBlur(chan, 11)
            edged = cv2.Canny(chan, 50, 200)
            self.accum_edged = cv2.bitwise_or(self.accum_edged, edged)

        cv2.imshow('edged map', self.accum_edged)
        cv2.waitKey(0)
Пример #2
0
    def __init__(self, resize):

        self.image = MainCVClass(resize).image

        self.boundaries = [([5, 5, 100], [80, 80, 200]),
                           ([86, 31, 4], [220, 88, 50]),
                           ([25, 146, 190], [62, 174, 250]),
                           ([103, 86, 65], [145, 133, 128]),
                           ([10, 30, 60], [140, 180, 250])]
Пример #3
0
    def __init__(self, resize):

        self.image = MainCVClass(resize).image
        self.gray = []
        self.current_contour_gray = []
        self.current_contour_color = []
        self.edged = []

        self.ANSWER_KEY = {0: 1, 1: 4, 2: 0, 3: 3, 4: 1}
Пример #4
0
    def __init__(self, resize):

        self.image = MainCVClass(resize).image

        cv2.imshow('image', self.image)
        cv2.waitKey(0)
Пример #5
0
    def __init__(self, resize):

        self.image = MainCVClass(resize).image
Пример #6
0
class OpenCVTest:
    def __init__(self, resize):

        self.image = MainCVClass(resize).image
        self.accum_edged = np.zeros(self.image.shape[:2], dtype="uint8")

        for chan in cv2.split(self.image):
            chan = cv2.medianBlur(chan, 11)
            edged = cv2.Canny(chan, 50, 200)
            self.accum_edged = cv2.bitwise_or(self.accum_edged, edged)

        cv2.imshow('edged map', self.accum_edged)
        cv2.waitKey(0)

    def sort_contours(self, contours, method="left-to-right"):
        reverse = False
        i = 0

        if method == "right-to-left" or method == "bottom-to-top":
            reverse = True

        if method == "top-to-bottom" or method == "left-to-right":
            i = 1

        bounding_boxes = [cv2.boundingRect(contour) for contour in contours]
        (contours, bounding_boxes) = zip(*sorted(zip(contours, bounding_boxes),
                                                 key=lambda b: b[1][i],
                                                 reverse=reverse))

        return (contours, bounding_boxes)

    def draw_contour(self, image, i, c):

        M = cv2.moments(c)
        cX = int(M["m10"] / M["m00"])
        cY = int(M["m01"] / M["m00"])

        cv2.putText(image, "#{}".format(i + 1), (cX - 20, cY),
                    cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255, 255, 255), 2)

        return image

    def sorting_contours(self):

        contours = cv2.findContours(self.accum_edged.copy(), cv2.RETR_EXTERNAL,
                                    cv2.CHAIN_APPROX_SIMPLE)
        contours = imutils.grab_contours(contours)
        contours = sorted(contours, key=cv2.contourArea, reverse=True)[:5]
        orig = self.image.copy()

        for (i, c) in enumerate(contours):
            orig = self.draw_contour(orig, i, c)

        cv2.imshow("unsorted", orig)
        cv2.waitKey(0)

        (contours,
         bounding_boxes) = self.sort_contours(contours,
                                              method=self.args["method"])

        for (i, c) in enumerate(contours):
            self.draw_contour(self.image, i, c)

        cv2.imshow("sorted", self.image)
        cv2.waitKey(0)
Пример #7
0
class OpenCVTest():
    def __init__(self, resize):
        self.image = MainCVClass(resize).image

    def show_image(self):

        cv2.imshow("Image", self.image)
        cv2.waitKey(0)

    def turn_image_to_gray(self, show):

        gray = cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY)

        if show:
            cv2.imshow("Gray", gray)
            cv2.waitKey(0)
        else:
            return gray

    def edge_datection(self):

        edged = cv2.Canny(self.turn_image_to_gray(False), 30, 150)

        cv2.imshow("edged", edged)
        cv2.waitKey(0)
        return edged

    def thresholding(self):
        thresh = cv2.threshold(self.turn_image_to_gray(False), 225, 255,
                               cv2.THRESH_BINARY_INV)[1]
        cv2.imshow("Thresh", thresh)
        cv2.waitKey(0)
        return thresh

    def find_contours(self):
        cnts = cv2.findContours(self.edge_datection().copy(),
                                cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        cnts = imutils.grab_contours(cnts)
        output = self.image.copy()

        for c in cnts:
            cv2.drawContours(output, [c], -1, (240, 0, 159), 3)

        text = "founded {} objects".format(len(cnts))
        cv2.putText(output, text, (10, 25), cv2.FONT_HERSHEY_SIMPLEX, 0.7,
                    (240, 0, 159), 2)

        cv2.imshow("Countours", output)
        cv2.waitKey(0)

    def erosion(self):

        mask = self.thresholding().copy()
        mask = cv2.erode(mask, None, iterations=5)
        cv2.imshow("Eroded", mask)
        cv2.waitKey(0)

    def dilation(self):

        mask = self.thresholding().copy()
        mask = cv2.dilate(mask, None, iterations=5)
        cv2.imshow("Eroded", mask)
        cv2.waitKey(0)

    def masking(self):

        mask = self.thresholding().copy()
        output = cv2.bitwise_and(self.image, self.image, mask=mask)
        cv2.imshow("Output", output)
        cv2.waitKey(0)
Пример #8
0
 def __init__(self, resize):
     self.image = MainCVClass(resize).image
     self.ratio = self.image.shape[0] / 500
     self.orig = self.image.copy()
Пример #9
0
class DocScan:
    def __init__(self, resize):
        self.image = MainCVClass(resize).image
        self.ratio = self.image.shape[0] / 500
        self.orig = self.image.copy()

    def get_edget(self):

        gray = cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY)
        gray = cv2.GaussianBlur(gray, (5, 5), 0)

        th3 = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                    cv2.THRESH_BINARY, 11, 2)

        # th3 = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]

        edged = cv2.Canny(th3, 80, 200)

        edged_for_watching = imutils.resize(edged.copy(), height=500)

        # cv2.imshow('gray', gray)
        cv2.imshow('th3', imutils.resize(th3, height=1000))
        cv2.imshow('edged', imutils.resize(edged, height=1000))
        cv2.waitKey(0)

        return th3

    def get_contours(self):

        contours = cv2.findContours(self.get_edget().copy(), cv2.RETR_LIST,
                                    cv2.CHAIN_APPROX_SIMPLE)
        contours = imutils.grab_contours(contours)
        contours = sorted(contours, key=cv2.contourArea, reverse=True)[:5]

        screen_contour = np.array([])

        for contour in contours:

            peri = cv2.arcLength(contour, True)
            approx = cv2.approxPolyDP(contour, 0.02 * peri, True)

            print(len(approx))

            if len(approx) == 4:
                screen_contour = approx
                cv2.drawContours(self.image, [screen_contour], -1, (0, 255, 0),
                                 2)
                # break

        # current_contour = cv2.boundingRect(screen_contour)
        # print(current_contour)
        # current_contour = self.image[current_contour[0]:current_contour[1], current_contour[2]:current_contour[3]]
        # current_contour = imutils.resize(current_contour, height=1000)

        # print(self.image.shape)
        # M = cv2.getPerspectiveTransform(screen_contour, np.float32([0, 0], []))
        # current_contour = (self.image, M)

        current_contour = imutils.resize(four_point_transform(
            self.image, screen_contour.reshape(4, 2)),
                                         height=1000)

        gray_current_contour = cv2.cvtColor(current_contour,
                                            cv2.COLOR_BGR2GRAY)
        gray_current_contour = cv2.GaussianBlur(gray_current_contour, (5, 5),
                                                0)
        gray_current_contour = cv2.Canny(gray_current_contour, 70, 200)

        cv2.imshow('outline', imutils.resize(self.image, height=1000))
        cv2.imshow('current_contour', current_contour)
        cv2.imshow('gray_current_contour', gray_current_contour)
        cv2.waitKey(0)

        return screen_contour

    def rotate_image(self):

        warped = four_point_transform(self.orig,
                                      self.get_contours().reshape(4, 2))

        warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY)
        T = threshold_local(warped, 11, offset=10, method='gaussian')
        warped = (warped > T).astype('uint8') * 255

        cv2.imshow('warped', imutils.resize(warped, height=1000))
        cv2.waitKey(0)