Exemplo n.º 1
0
    def __recognizeMarkerAreas(self):
        '''
        Recognizes marker area
        '''
        self.__markers = []
        if self._img == None:
            self._gui.status( "No image!", self._gui.msgTypes['debug'] )
            return
        
        # get data
        self.__markers = []
        blocksize = int( str(self._gui.getObj("txtMarkerAreaBlockSize").text()) )
        epsilon = float( str(self._gui.getObj("txtMarkerAreaEpsilon").text()) )
        areaMin = float( str(self._gui.getObj("txtMarkerAreaMin").text()) )/100.0
        areaMax = float( str(self._gui.getObj("txtMarkerAreaMax").text()) )/100.0
        blur = int( str(self._gui.getObj("txtMarkerAreaBlur").text()) )
        
        # check correct blocksize
        if blocksize%2 == 0:
            blocksize -= 1
                
        # get current image and convert
        self.__imgRegArea = copy(self._img)        
        gray = cvtColor(self.__imgRegArea, COLOR_RGB2GRAY)
        imgArea = gray.shape[0]*gray.shape[1]
        
        # threshold and canny
        try:
            gray = medianBlur( gray, blur )
            gray = adaptiveThreshold( gray, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, blocksize, 0 )
        except:
            return
        colorImg = cvtColor(gray, COLOR_GRAY2RGB)
        colorImg2 = cvtColor(gray, COLOR_GRAY2RGB)
        
        # get contours
        contours = getContours( gray, epsilon, retr=RETR_LIST, enConvexHull=False)
        
        # add contours with correct area size
        for cnt in contours:
            cntArea = contourArea( cnt )
            cnt = getBoundingRect(cnt)
            
#             if len(cnt) == 4 and cntArea > imgArea*areaMin and cntArea < imgArea*areaMax:
            if cntArea > imgArea*areaMin and cntArea < imgArea*areaMax:
                self.__markers.append( cnt )
                drawContours( colorImg2, [cnt], -1, (0,255,0), -1 )
                
        self._gui.getObj("lblFoundMarker").setText( "Found marker: " + str(len(self.__markers)) )
        
        # draw contours
        drawContours( colorImg, self.__markers, -1, (255,0,0), -1 )
        self.__imgArea = colorImg        
        self.__imgAreaDetails = colorImg2
Exemplo n.º 2
0
    def __recorgnizeMarker(self, gray, marker, contourPadding=0, th=30, th2=155, epsilon=0.01, cannyDown=30, cannyUp=255):
        '''
        recognize one marker
        '''
        # get minmax of marker and calculate center4
        minX, maxX, minY, maxY = minMax(marker)
        
        dx = (maxX-minX)*0.1
        dy = (maxY-minY)*0.1
        minX -= dx
        maxX += 3*dx
        minY -= dy
        maxY += 3*dy
        
        markerCenter = ( minX+(maxX-minX), minY+(maxY-minY) )
        
        # slice and resize marker
        try:
            sliceImg = gray[minY:maxY, minX:maxX]
            sliceImg = resize( sliceImg, self.__markerSize )
        except:
            return None
        
        # threshold and canny
        imgTh = threshold( sliceImg, th, 255, THRESH_BINARY_INV )[1]                
        canny = Canny( imgTh, cannyDown, cannyUp )
        
        # set threshold image
#         self.__imgID = imgTh

#         bild = Image.fromarray(imgTh)
#         bild.show()
        
        # get contours again
        contours = getContours( canny, epsilon, enConvexHull=False)
        
#         self.__imgID = cvtColor(imgTh, COLOR_GRAY2RGB)
#         drawContours(self.__imgID, contours, -1, (255,0,0))
        
        # get first big contour
        imgArea = sliceImg.shape[0]*sliceImg.shape[1]
        imgAreaMin = imgArea*0.1
        imgAreaMax = imgArea*0.8
        contour = None
        for cnt in contours:
            cntArea = contourArea(cnt)
#             print "contour", cntArea, "min", imgAreaMin, "max", imgAreaMax, "imgArea", imgArea, "ratio", cntArea/imgArea 
            if cntArea > imgAreaMin and cntArea < imgAreaMax:
                contour = cnt
                break;
                
        if contour == None:
#             print "\tno contour found"
            return None
        
        # get bounding rectangle
        bb = getBoundingRect(contour)        
    
        # get marker vectors
        v0 = bb[0]
        v3 = bb[3]
        v = (v3[0]-v0[0], v3[1]-v0[1])
        
        if v[0] == 0 and v[1] == 0:
            print "\tzero devision"
            return None
        
        # get marker rotation
        angle = round( getAngleOfVector(v), 2 )  
        center = ( imgTh.shape[1]*0.5, imgTh.shape[0]*0.5 )
        
        # transformate bounding box to center of image
        bb[0] = getTranslateVector(bb[0], center)
        bb[1] = getTranslateVector(bb[1], center)
        bb[2] = getTranslateVector(bb[2], center)
        bb[3] = getTranslateVector(bb[3], center)
        
        # rotate marker
        rot = getRotationMatrix2D( center, angle, 1.0 )
        sliceImg = warpAffine( sliceImg, rot, sliceImg.shape, borderValue=0 )
        
        # rotate bounding box
        angle *= -1
        bb[0] = rotateVector(bb[0], getAngleOfVector(bb[0])+angle)
        bb[1] = rotateVector(bb[1], getAngleOfVector(bb[1])+angle)
        bb[2] = rotateVector(bb[2], getAngleOfVector(bb[2])+angle)
        bb[3] = rotateVector(bb[3], getAngleOfVector(bb[3])+angle)
        
        # transformate bounding box to origin of image
        bb[0] = getTranslateVector(center, bb[0], False)
        bb[1] = getTranslateVector(center, bb[1], False)
        bb[2] = getTranslateVector(center, bb[2], False)
        bb[3] = getTranslateVector(center, bb[3], False)
        
        # get marker id
        minX, maxX, minY, maxY = minMax(bb)      
        
        sliceImg = sliceImg[minY:maxY, minX:maxX]
        markerID = detectMarkerID(sliceImg, minMax(bb), th2, self.__referenceMarker)
        
        # set angle and center of morker
        angle *= -1
        markerID['center'] = markerCenter
        
        markerID['angle'] -= (90+angle)
        if markerID['angle'] > 180:
            markerID['angle'] -= 360

        print "markerID:", markerID
        
        # set image
        sliceImg = cvtColor(sliceImg, COLOR_GRAY2RGB)
#         drawContours( sliceImg, [bb], -1, (0,255,0) )
#         self.__imgIDDetails = sliceImg
                
        return markerID
Exemplo n.º 3
0
    def __recorgnizeMarker(self,
                           gray,
                           marker,
                           contourPadding=0,
                           th=30,
                           th2=155,
                           epsilon=0.01,
                           cannyDown=30,
                           cannyUp=255):
        '''
        recognize one marker
        '''
        # get minmax of marker and calculate center4
        minX, maxX, minY, maxY = minMax(marker)

        dx = (maxX - minX) * 0.1
        dy = (maxY - minY) * 0.1
        minX -= dx
        maxX += 3 * dx
        minY -= dy
        maxY += 3 * dy

        markerCenter = (minX + (maxX - minX), minY + (maxY - minY))

        # slice and resize marker
        try:
            sliceImg = gray[minY:maxY, minX:maxX]
            sliceImg = resize(sliceImg, self.__markerSize)
        except:
            return None

        # threshold and canny
        imgTh = threshold(sliceImg, th, 255, THRESH_BINARY_INV)[1]
        canny = Canny(imgTh, cannyDown, cannyUp)

        # set threshold image
        #         self.__imgID = imgTh

        #         bild = Image.fromarray(imgTh)
        #         bild.show()

        # get contours again
        contours = getContours(canny, epsilon, enConvexHull=False)

        #         self.__imgID = cvtColor(imgTh, COLOR_GRAY2RGB)
        #         drawContours(self.__imgID, contours, -1, (255,0,0))

        # get first big contour
        imgArea = sliceImg.shape[0] * sliceImg.shape[1]
        imgAreaMin = imgArea * 0.1
        imgAreaMax = imgArea * 0.8
        contour = None
        for cnt in contours:
            cntArea = contourArea(cnt)
            #             print "contour", cntArea, "min", imgAreaMin, "max", imgAreaMax, "imgArea", imgArea, "ratio", cntArea/imgArea
            if cntArea > imgAreaMin and cntArea < imgAreaMax:
                contour = cnt
                break

        if contour == None:
            #             print "\tno contour found"
            return None

        # get bounding rectangle
        bb = getBoundingRect(contour)

        # get marker vectors
        v0 = bb[0]
        v3 = bb[3]
        v = (v3[0] - v0[0], v3[1] - v0[1])

        if v[0] == 0 and v[1] == 0:
            print "\tzero devision"
            return None

        # get marker rotation
        angle = round(getAngleOfVector(v), 2)
        center = (imgTh.shape[1] * 0.5, imgTh.shape[0] * 0.5)

        # transformate bounding box to center of image
        bb[0] = getTranslateVector(bb[0], center)
        bb[1] = getTranslateVector(bb[1], center)
        bb[2] = getTranslateVector(bb[2], center)
        bb[3] = getTranslateVector(bb[3], center)

        # rotate marker
        rot = getRotationMatrix2D(center, angle, 1.0)
        sliceImg = warpAffine(sliceImg, rot, sliceImg.shape, borderValue=0)

        # rotate bounding box
        angle *= -1
        bb[0] = rotateVector(bb[0], getAngleOfVector(bb[0]) + angle)
        bb[1] = rotateVector(bb[1], getAngleOfVector(bb[1]) + angle)
        bb[2] = rotateVector(bb[2], getAngleOfVector(bb[2]) + angle)
        bb[3] = rotateVector(bb[3], getAngleOfVector(bb[3]) + angle)

        # transformate bounding box to origin of image
        bb[0] = getTranslateVector(center, bb[0], False)
        bb[1] = getTranslateVector(center, bb[1], False)
        bb[2] = getTranslateVector(center, bb[2], False)
        bb[3] = getTranslateVector(center, bb[3], False)

        # get marker id
        minX, maxX, minY, maxY = minMax(bb)

        sliceImg = sliceImg[minY:maxY, minX:maxX]
        markerID = detectMarkerID(sliceImg, minMax(bb), th2,
                                  self.__referenceMarker)

        # set angle and center of morker
        angle *= -1
        markerID['center'] = markerCenter

        markerID['angle'] -= (90 + angle)
        if markerID['angle'] > 180:
            markerID['angle'] -= 360

        print "markerID:", markerID

        # set image
        sliceImg = cvtColor(sliceImg, COLOR_GRAY2RGB)
        #         drawContours( sliceImg, [bb], -1, (0,255,0) )
        #         self.__imgIDDetails = sliceImg

        return markerID
Exemplo n.º 4
0
    def __recognizeMarkerAreas(self):
        '''
        Recognizes marker area
        '''
        self.__markers = []
        if self._img == None:
            self._gui.status("No image!", self._gui.msgTypes['debug'])
            return

        # get data
        self.__markers = []
        blocksize = int(str(self._gui.getObj("txtMarkerAreaBlockSize").text()))
        epsilon = float(str(self._gui.getObj("txtMarkerAreaEpsilon").text()))
        areaMin = float(str(
            self._gui.getObj("txtMarkerAreaMin").text())) / 100.0
        areaMax = float(str(
            self._gui.getObj("txtMarkerAreaMax").text())) / 100.0
        blur = int(str(self._gui.getObj("txtMarkerAreaBlur").text()))

        # check correct blocksize
        if blocksize % 2 == 0:
            blocksize -= 1

        # get current image and convert
        self.__imgRegArea = copy(self._img)
        gray = cvtColor(self.__imgRegArea, COLOR_RGB2GRAY)
        imgArea = gray.shape[0] * gray.shape[1]

        # threshold and canny
        try:
            gray = medianBlur(gray, blur)
            gray = adaptiveThreshold(gray, 255, ADAPTIVE_THRESH_MEAN_C,
                                     THRESH_BINARY, blocksize, 0)
        except:
            return
        colorImg = cvtColor(gray, COLOR_GRAY2RGB)
        colorImg2 = cvtColor(gray, COLOR_GRAY2RGB)

        # get contours
        contours = getContours(gray,
                               epsilon,
                               retr=RETR_LIST,
                               enConvexHull=False)

        # add contours with correct area size
        for cnt in contours:
            cntArea = contourArea(cnt)
            cnt = getBoundingRect(cnt)

            #             if len(cnt) == 4 and cntArea > imgArea*areaMin and cntArea < imgArea*areaMax:
            if cntArea > imgArea * areaMin and cntArea < imgArea * areaMax:
                self.__markers.append(cnt)
                drawContours(colorImg2, [cnt], -1, (0, 255, 0), -1)

        self._gui.getObj("lblFoundMarker").setText("Found marker: " +
                                                   str(len(self.__markers)))

        # draw contours
        drawContours(colorImg, self.__markers, -1, (255, 0, 0), -1)
        self.__imgArea = colorImg
        self.__imgAreaDetails = colorImg2