Exemplo n.º 1
0
    def raw1(self, img):
        self.counter += 1
        hBefore, wBefore, _ = img.shape
        img = self.transformer.resize(img, 0, 270, 40, 500)

        contrast = self.transform(img)
        rois = DetectionUtils.detectContours(contrast,
                                             widthLower=self.dim1Lower,
                                             widthUpper=self.dim1Upper,
                                             heightLower=self.dim2Lower,
                                             heigthUpper=self.dim2Upper)
        tracked, _ = self.tracker.track(rois)
        self.numObjects = self.tracker.N
        if self.guiMode:
            for roi in rois:
                ImgUtils.drawRect(roi, img)
                detectedCentroid = ImgUtils.findRoiCentroid(roi)
                ImgUtils.drawCircle(detectedCentroid, img, colour=(255, 0, 0))
            for objectId, centroid in tracked.items():
                ImgUtils.drawCircle((centroid[0], centroid[1]), img)
                ImgUtils.putText(coords=centroid,
                                 text=str(objectId % 1000),
                                 img=img,
                                 colour=(255, 0, 0))

        return img, contrast, []
Exemplo n.º 2
0
    def brick0(self, img):
        # img = img[550:, 350:-400, :]
        img = img[100:, :, :]

        contrast = np.copy(img)
        contrast = self.transform(contrast)
        contours, h = cv2.findContours(contrast, cv2.RETR_EXTERNAL,
                                       cv2.CHAIN_APPROX_SIMPLE)
        rois = []
        for c in contours:
            approx = cv2.approxPolyDP(c, 0.01 * cv2.arcLength(c, True), True)
            x, y, w, h = cv2.boundingRect(c)
            if len(approx) < 1 or w < 130 or h < 60:
                continue
            x1 = x
            x2 = x1 + w
            y1 = y
            y2 = y1 + h
            if y1 < 250 or x2 < 100:
                continue
            rois.append([x1, y1, x2, y2])
            # targetHeight = 130
            # numParts = h // targetHeight
            # if numParts < 1:
            #     rois.append([x1, y1, x2, y2])
            # else:
            #     step = (h % targetHeight)
            #     y = y1
            #     for i in range(0, numParts):
            #         r = [x1, y, x2, y + targetHeight + step]
            #         y += (step + targetHeight)
            #         rois.append(r)

        tracked, _ = self.tracker.track(rois)
        self.numObjects = self.tracker.N
        if self.guiMode:
            for roi in rois:
                ImgUtils.drawRect(roi, img)
                detectedCentroid = ImgUtils.findRoiCentroid(roi)
                ImgUtils.drawCircle(detectedCentroid, img, colour=(255, 0, 0))
            for objectId, centroid in tracked.items():
                ImgUtils.drawCircle((centroid[0], centroid[1]), img)
                ImgUtils.putText(coords=centroid,
                                 text=str(objectId % 1000),
                                 img=img,
                                 colour=(255, 0, 0))

        return img, contrast, []
Exemplo n.º 3
0
    def desample(self, path, batchSize=3):
        for i in range(0, 1000000, batchSize):
            imgs = []
            gmis = []
            gims = []
            for j in range(batchSize):
                imgPath = path+str(i+j)+'.png'
                img = cv2.imread(imgPath)
                if img is None:
                    continue
                img = img[60:240, 60:240, :]
                imgs.append(img)

            for j, img in enumerate(imgs):
                # imgs[0] = ImgUtils.randomImage(red=(80, 240), blue=(10, 100), green=(80, 240), imgLike=img)
                # imgs[1] = ImgUtils.randomImage(red=(0, 120), blue=(0, 120), green=(0, 120), imgLike=img)
                # imgs[2] = ImgUtils.randomImage(red=(0, 255), blue=(0, 255), green=(0, 255), imgLike=img)
                # imgs[3] = ImgUtils.randomImage(red=(120, 255), blue=(120, 255), green=(120, 255), imgLike=img)
                # imgs[4] = ImgUtils.randomImage(red=(150, 170), blue=(30, 50), green=(30, 50), imgLike=img)

                gmi = self.transform(imgs[j])
                gmis.append(gmi)
                ImgUtils.putText(img, str(np.mean(img[:, :, 2])), (10, 50), colour=(200, 0, 255))

            img = ImgUtils.hconcat(imgs, pad=50)
            gmi = ImgUtils.hconcat(gmis, pad=50)
            # Y = ImgUtils.hconcat(imgs, channel=1)
            # gmi[i, j] = (np.random.randint(10, 100), np.random.randint(70, 100), np.random.randint(80, 240))

            while True:
                ImgUtils.show('Img', img, 0, 0)
                ImgUtils.show('Gmi', gmi, 0, 260)
                # ImgUtils.show('Y', Y, 0, 720)

                key = cv2.waitKey(500)
                if key == ord('v'):
                    break
                elif key == ord('q'):
                    return
Exemplo n.º 4
0
    def postbake1(self, img):
        hBefore, wBefore, _ = img.shape
        img = self.transformer.resize(img)
        origImg = np.copy(img)
        contrast = self.transform(img)
        rois, radii = DetectionUtils.houghDetect(contrast,
                                                 radiusMin=self.dim1Lower,
                                                 radiusMax=self.dim1Upper)
        tracked, newRois = self.tracker.track(rois)
        self.numObjects = self.tracker.N

        if self.guiMode:
            for roi in rois:

                ImgUtils.drawRect(roi, img)
                detectedCentroid = ImgUtils.findRoiCentroid(roi)
                ImgUtils.drawCircle(detectedCentroid, img, colour=(255, 0, 0))
                ImgUtils.putText(coords=(roi[0] + 50, roi[1] + 50),
                                 text=str(roi[2] - roi[0]),
                                 img=img,
                                 colour=(255, 255, 0),
                                 fontSize=3)
            for objectId, centroid in tracked.items():
                ImgUtils.drawCircle((centroid[0], centroid[1]), img)
                ImgUtils.putText(coords=centroid,
                                 text=str(objectId % 1000),
                                 img=img,
                                 colour=(255, 0, 0))

        out = []

        for roi in newRois:
            colour = self.colour(origImg[roi[1]:roi[3], roi[0]:roi[2]])
            self.averageColour[0] += colour[0]
            self.averageColour[1] += colour[1]
            self.averageColour[2] += colour[2]
            self.averageSize += roi[3] - roi[1]

        return img, contrast, out