Beispiel #1
0
import numpy as np
import cv2, math
from matplotlib import pyplot as plt
import utils

cap = cv2.VideoCapture(0)

while True:
    success, img = cap.read()

    imgcont, conts, _ = utils.getContours(img,
                                          cThr=[150, 175],
                                          showCanny=False,
                                          filter=4,
                                          draw=False)  #Selecting the White box

    if len(conts) != 0:
        biggest = conts[0][2]
        imgWarp = utils.warpImg(img, biggest, 350,
                                350)  #warping the Image to Region of Interest

        imgCont2, cont2, imgThre = utils.getContours(
            imgWarp, cThr=[50, 50], showCanny=False, filter=7,
            draw=False)  #Finding the Countours of Arrow

        if len(cont2) != 0:

            tip = tuple(cont2[0][2][0][0])
            #tip2=tuple(cont2[0][2][3][0])
            M = cv2.moments(cont2[0][2])
            cX = int(M["m10"] / M["m00"])  #Finding the centroid of the arrow
Beispiel #2
0
            print("Can't receive frame. Exiting...")
            break

        frame_counter += 1
        # If the last frame is reached, reset the capture and the frame_counter
        if frame_counter == cap.get(cv2.CAP_PROP_FRAME_COUNT):
            frame_counter = 0  # Or whatever as long as it is the same as next line
            cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
            for dl in toDrawLists:
                dl.clear()
            for bl in ballPathsToDraw:
                bl.clear()
        imgRaw = cv2.resize(imgRaw, (0, 0), None, 0.7, 0.7)

        deskArea = None
        img, contours = utils.getContours(imgRaw, showCanny=debugMode, draw=debugMode)

        if len(contours) != 0:
            contourMax = contours[0]
            approxCorners = contourMax[2]
            deskArea = approxCorners
            imgWarp, newMatrix = utils.warpImg(img, approxCorners, wBirdseye, hBirdseye,
                                               prevMatrix=warpMatrix, prevWeight=curMatrixWeight)

            img, redPos = detection.detectBall(img, deskArea, 'red')
            img, yellowPos = detection.detectBall(img, deskArea, 'yellow')
            img, whitePos = detection.detectBall(img, deskArea, 'white')
            img, greenPos = detection.detectBall(img, deskArea, 'green')
            ballPositions = [redPos, yellowPos, whitePos, greenPos]
            for i in range(len(ballPositions)):
                if ballPositions[i] is not None:
Beispiel #3
0
webcam = True
path = '1.jpg'
cap = cv2.VideoCapture(0)
cap.set(10, 160)
cap.set(3, 1920)
cap.set(4, 1080)
scale = 3
wP = 210 * scale
hP = 297 * scale
###################################

while True:
    if webcam: success, img = cap.read()
    else: img = cv2.imread(path)

    imgContours, conts = utils.getContours(img, minArea=50000, filter=4)
    if len(conts) != 0:
        biggest = conts[0][2]
        #print(biggest)
        imgWarp = utils.warpImg(img, biggest, wP, hP)
        imgContours2, conts2 = utils.getContours(imgWarp,
                                                 minArea=2000,
                                                 filter=4,
                                                 cThr=[50, 50],
                                                 draw=False)
        if len(conts) != 0:
            for obj in conts2:
                cv2.polylines(imgContours2, [obj[2]], True, (0, 255, 0), 2)
                nPoints = utils.reorder(obj[2])
                nW = round((utils.findDis(nPoints[0][0] // scale,
                                          nPoints[1][0] // scale) / 10), 1)
Beispiel #4
0
import cv2
import numpy as np
import utils

webcam = False
cap = cv2.VideoCapture(0)
img = cv2.imread('battery.jpg')
cap.set(10, 160)
cap.set(3, 1920)
cap.set(4, 1080)
while True:
    if webcam:
        success, img = cap.read()
    else:
        img = cv2.imread('battery.jpg')
        img = cv2.resize(img, (0, 0), None, 0.5, 0.5)

    img, conts = utils.getContours(img, showC=True, minArea=50000, filter=4)

    if len(conts) != 0:
        biggest = conts[0][2]
        print(biggest)

    img = cv2.resize(img, (0, 0), None, 0.5, 0.5)
    cv2.imshow('image', img)
    cv2.waitKey(1)

cv2.destroyAllWindows()
Beispiel #5
0
scale = 4

warpMatrix = np.zeros((3, 3))
curWeight = 0

while cap.isOpened():
    if stream:
        success, img = cap.read()
    else:
        success, img = True, cv2.imread(path)

    if not success:
        print("Can't receive frame. Exiting...")
        break

    img, contours = utils.getContours(img, showCanny=True, draw=True)
    if len(contours) != 0:
        contourMax = contours[0]
        approxCorners = contourMax[2]
        imgWarp, newMatrix = utils.warpImg(img,
                                           approxCorners,
                                           wTable * scale,
                                           hTable * scale,
                                           prevMatrix=warpMatrix,
                                           prevWeight=curWeight)
        warpMatrix = newMatrix
        curWeight += 1
        cv2.imshow('Warped Table', imgWarp)

    img = cv2.resize(img, (0, 0), None, 0.7, 0.7)
    cv2.imshow('Original', img)
Beispiel #6
0
def Ellipse(sciimg,
            mskimg=None,
            ps=None,
            E=None,
            img=False,
            pick=1,
            extrapolate=0.,
            tol=1,
            sclip=True,
            minCt=10,
            pfsam=0.5,
            cen=None,
            rcmin=5.):
    def tomin(x, y, P, T, X=None):
        #T gives the limits of the angle range
        #X gives the limits of the center range
        x0, y0, q, theta, c0 = np.array(P).astype(float)
        X = [np.min(x), np.max(x), np.min(y), np.max(y)] if X is None else X
        if q > 1. or q < 0.: return 1e100
        if c0 > 5 or c0 < -1.9: return 1e100
        if x0 > X[1] or x0 < X[0]: return 1e100
        if y0 > X[3] or y0 < X[2]: return 1e100
        if theta > T[1] or theta < T[0]: return 1e100
        r, th = gell(x, y, x0, y0, q, theta, c0, reth=True)
        if (np.max(th) - np.min(th)) / np.pi < 1.7: return 1e100
        rm = np.median(r)
        # if rm>5*(np.max([np.max(x)-np.min(x),np.max(y)-np.min(y)])): return 1e100
        return np.sum((r - np.median(r))**2)

    def tomin2(x, y, P, T, X=None):
        #Add th limit, add X limit, add rm limit (?),
        x0, y0, q, theta = np.array(P[:-1]).astype(float)
        a = P[-1]
        if (np.abs(np.array(a)) > 0.1).any(): return 1e100
        if q > 1. or q < 0.: return 1e100
        if x0 > np.max(x) or x0 < np.min(x): return 1e100
        if y0 > np.max(y) or y0 < np.min(y): return 1e100
        if theta > T[1] or theta < T[0]: return 1e100
        r = gella4(x, y, x0, y0, q, theta, a)
        return np.sum((r - np.median(r))**2)

    # sciimg is a 2D array
    # pick is 0 for elliptical profile, 1 for c0 profile, and 2 for a4 profile
    # retfun returns a linear interpolation function of the profiles
    # Extrapolate is the sky level until which one wants to extrapolate after ellipse cannot measure it anymore
    if E is None:
        E = [0, sciimg.shape[1], 0, sciimg.shape[0]]
    xx, yy = np.meshgrid(
        UU.middlebin(np.linspace(E[0], E[1], sciimg.shape[1] + 1)),
        UU.middlebin(np.linspace(E[2], E[3], sciimg.shape[0] + 1)))
    dx = xx[0][1] - xx[0][0]
    dy = yy[1][0] - yy[0][0]
    if mskimg is None:
        mskimg = np.ones(sciimg.shape)
    # pit=UU.getWP(sciimg[mskimg==1],sciimg[mskimg==1])
    whtmin = np.abs(np.min(sciimg[mskimg == 1]))
    pit = UU.getWP(sciimg[mskimg == 1], sciimg[mskimg == 1] + whtmin)
    y = {}  # Summary info [ps,<x0>,<y0>,<q>,c0,a4,<theta>]
    Ctout = []
    # for key in ['ps','x0','y0','q','theta','x0_m','y0_m','q_m','theta_m','c0','a4','val','r1','r2','r3','DL']: #,'Ct'
    for key in [
            'ps', 'x0', 'y0', 'q', 'theta', 'val', 'r', 'DL', 'rc2', 'ellval'
    ]:
        y[key] = []
    if pick == 1:
        y['c0'] = []
    elif pick == 2:
        y['a4'] = []
        y['a'] = []
    if ps is None:
        #Finding the points based on the pit curve
        def safepit(x):
            if x < 0: return effpit(0)
            elif x > 100: return effpit(100)
            else: return np.arcsinh(effpit(x))  #CHANGED Log10 for arcsinh!!

        ##Finding the gradient##
        #To find a better description of the gradient, first I simplify y
        idxs = (pit.x > 0) & (pit.x <= 100)
        effy, b = np.unique(np.round(np.arcsinh(pit.y[idxs]), 3),
                            return_inverse=True)  #CHANGED Log10 for arcsinh!!
        effx = np.array(
            [np.min(pit.x[idxs][b == i]) for i in range(len(effy))])
        effpit = interp1d(
            effx, [np.max(pit.y[idxs][b == i]) for i in range(len(effy))],
            bounds_error=False,
            fill_value='extrapolate'
        )  #pit is affected by the points removed above, it is very rare but it happens
        #Analytic approximation of the log10 pit curve
        # ly=np.polyfit(np.append([effx[-1]+i*(effx[-1]-effx[-2]) for i in range(5)],effx),np.append([effy[-1]-i*(effy[-2]-effy[-1]) for i in range(5)],effy),10)
        # gr=np.poly1d(np.polyder(ly))
        effpitash = lambda x: np.arcsinh(effpit(x))
        gr = lambda x: derivative(effpitash, x)

        ps = [float(effx[-1])]
        # gdown=lambda a,x:np.abs(safepit(x-a*gr(x))-(safepit(x)+a*gr(x)))
        gdown = lambda a, x: np.abs(
            safepit(x - a * gr(x)) -
            (safepit(x) + a * gr(x))) if a > 0 else 1e100
        pk = 1.
        # tol=1
        while ps[-1] > 0:
            # print ps[-1]
            # efftol=tol/gr(ps[-1]) #absolute value
            # efftol=tol #gradient times value
            # efftol=tol/gr(ps[-1])/safepit(ps[-1]) #relative to current value
            efftol = tol / gr(
                ps[-1]) / ps[-1]  #relative to current value - nani?
            # if gr(ps[-1])<0:ps.append(pit.x[pit.x<ps[-1]][-1])
            # print ps[-1]
            # print np.min(effx)
            # print effx[effx<ps[-1]]
            # print '##############################################'
            if gr(ps[-1]) < 0:
                tap = 0. if len(
                    effx[effx < ps[-1]]) == 0 else effx[effx < ps[-1]][-1]
                ps.append(tap)
            else:
                x1 = minimize(lambda x: np.abs(gdown(x, ps[-1]) - efftol),
                              pk,
                              tol=1e-3,
                              method='Nelder-Mead')
                pk = x1['x'][0]
                ps.append(ps[-1] - x1['x'][0] * gr(ps[-1]))
            # print pk,ps[-1]
        ############################
        # plt.clf()
        # plt.plot(pit.x,np.log10(pit.y))
        # plt.plot(pit.x,np.poly1d(ly)(pit.x))
        # plt.scatter(pit.x,np.log10(pit.y),c=gr(pit.x))
        # plt.scatter(ps,poly1d(ly)(ps),c='red')
        ############################
        ps = 100 - np.array(ps)
        # ps=np.append(interp1d(range(len(ps)),ps)(np.arange(0,len(ps)-1,1./pssamp)),ps[-1])
        ps = ps[ps <= 100]
    # print len(ps)
    # return
    pCt = None
    px0 = None
    pctf1 = None
    minmax = lambda x: np.max(x) - np.min(x)
    scieff = np.zeros(sciimg.shape) + sciimg
    scieff[
        mskimg ==
        0] = 0.  #TRYING ZEROS HERE. It might be better than nans, but I am not sure yet.
    for pi, p in enumerate(ps):
        # print pi
        if p == ps[0]:
            effp1 = (p + ps[1]) / 2
            effp2 = (
                (100. - effpit.x[effpit.x.searchsorted(100. - p)]) +
                np.min([(100. - effpit.x[effpit.x.searchsorted(100. - p) - 1]),
                        ps[pi + 1]])) / 2
            effp = np.min([effp1, effp2])
        else:
            if 100 - p in effpit.x and pi + 1 != len(ps):
                effp = (
                    (100. - effpit.x[effpit.x.searchsorted(100. - p)]) +
                    np.min([(100. - effpit.x[effpit.x.searchsorted(100. - p) -
                                             1]), ps[pi + 1]])) / 2
            else:
                effp = p
        Ct = UU.getContours(scieff, effpit(100 - effp), E)
        Ct = [np.array([x for x in C if not np.isnan(x).any()])
              for C in Ct]  #Remove nans
        Ct = [C for C in Ct if len(C) != 0]  #Remove nans
        Ct = [
            C for C in Ct
            if np.min(C[:, 0]) > E[0] + 1. and np.min(C[:, 1]) > E[2] +
            1. and np.max(C[:, 0]) < E[1] - 1 and np.min(C[:, 0]) < E[3] - 1
        ]  # only Contours away from border
        if pCt is not None:
            Ct = [
                C for C in Ct
                if Path(C).contains_points([np.median(pCt, 0)]).all()
            ]  # Should contain the previous one
        if len(Ct) == 0:
            print 'Finishing here, you hit the background'
            break
        if cen is not None and p == ps[0]:
            #Sometimes there is more than 1 contour to choose from at the beggining, right now the pick is random...
            Ct = [
                C for C in Ct
                if np.sqrt(np.sum((np.median(C, 0) -
                                   (np.array(cen) + 0.5))**2)) < np.sqrt(2)
            ]
        ict = np.argmax([len(C) for C in Ct])  #picking the longest contour
        Cteff = Ct[ict]

        #If too few points in the contours, weird fits can happen, so I will have a minimum.
        if len(Cteff) < minCt:
            iz = int(float(minCt) / (len(Cteff) - 1)) + 1
            nx = np.array([
                Cteff[:-1, 0] + i * (Cteff[1:, 0] - Cteff[:-1, 0]) / iz
                for i in range(iz)
            ]).T.ravel()
            ny = np.array([
                Cteff[:-1, 1] + i * (Cteff[1:, 1] - Cteff[:-1, 1]) / iz
                for i in range(iz)
            ]).T.ravel()
            Cteff = np.array([nx, ny]).T
        #It is of no purpose to interpolate within the same pixels
        if pCt is not None and pfsam != 0:
            idxc = np.unique(np.array([(Cteff[:, 0] / pfsam).astype("int"),
                                       (Cteff[:, 1] / pfsam).astype("int")]).T,
                             axis=0)
            idxp = np.unique(np.array([(pCt[:, 0] / pfsam).astype("int"),
                                       (pCt[:, 1] / pfsam).astype("int")]).T,
                             axis=0)
            if np.all(idxc == idxp):
                # print idxc,idxp
                continue
        pCt = Cteff

        #First guess
        if px0 is None:
            x00 = np.median(Cteff[:, 0])
            y00 = np.median(Cteff[:, 1])
            t0 = []
            t1 = []
            t2 = []
            for theta in np.linspace(-np.pi / 2, np.pi / 2, 100):
                theta = theta - np.pi / 2
                M1 = np.array([(Cteff[:, 0] - x00), (Cteff[:, 1] - y00)])
                c = np.array([[np.cos(theta), np.sin(theta)],
                              [-np.sin(theta), np.cos(theta)]])
                M2 = np.dot(c, M1)
                t0.append(theta)
                # t1.append(np.max([np.max(M2[0,:])-np.min(M2[0,:]),np.max(M2[1,:])-np.min(M2[1,:])]))
                t1.append(np.max(M2[0, :]) - np.min(M2[0, :]))
                t2.append((np.max(M2[1, :]) - np.min(M2[1, :])) /
                          (np.max(M2[0, :]) - np.min(M2[0, :])))
            ############
            q0 = t2[np.argmax(t1)] if t2[np.argmax(t1)] < 1. else 1.
            et0 = t0[np.argmax(t1)]
        else:
            x00, y00, q0, et0 = px0
        ###########
        #Sigma clipping
        if sclip and len(Cteff) > 10:
            thr = np.arctan2((Cteff[:, 1].ravel() - y00),
                             (Cteff[:, 0].ravel() - x00) * q0)
            rr = gell(Cteff[:, 0], Cteff[:, 1], x00, y00, q0, et0, 0.)
            ly = np.polyfit(thr, rr, 5)
            rf = rr - np.poly1d(ly)(thr)
            rf = (rf - np.median(rf)) / np.std(rf)
            idxs = np.where(np.abs(rf) > 3)[0]
            grps = np.split(idxs, np.where(np.diff(idxs) != 1)[0] + 1)
            ctmsk = np.ones(Cteff[:, 0].shape).astype(bool)
            for g in grps:
                if len(g) == 0: continue
                g0 = np.min(g) - np.argmin(
                    (rf[:np.min(g)] > 1)[::-1]) if np.min(g) != 0 else 0
                g1 = np.max(g) + np.argmin(
                    (rf[np.max(g):] > 1
                     )) if np.max(g) != len(Cteff) - 1 else len(Cteff) - 1
                ctmsk[g0:g1] = 0
            Cteff = Cteff[ctmsk, :]
        ###########
        X = None if pctf1 is None else [
            np.min(pctf1[0]),
            np.max(pctf1[0]),
            np.min(pctf1[1]),
            np.max(pctf1[1])
        ]
        x2a = minimize(lambda x: tomin(Cteff[:, 0], Cteff[:, 1], [
            x[0], x[1], x[2], x[3], 0.
        ], [et0 - np.pi / 2, et0 + np.pi / 2], X), [x00, y00, q0, et0],
                       method='Powell',
                       options={'maxiter': 100000})
        x2b = minimize(lambda x: tomin(Cteff[:, 0], Cteff[:, 1], [
            x[0], x[1], x[2], x[3], 0.
        ], [x2a['x'][3] - np.pi / 2, x2a['x'][3] + np.pi / 2], X),
                       x2a['x'],
                       method='Nelder-Mead',
                       options={'maxiter': 100000})
        if pick == 0:
            xf = x2b
            r, th = gell(Cteff[:, 0],
                         Cteff[:, 1],
                         x2b['x'][0],
                         x2b['x'][1],
                         x2b['x'][2],
                         x2b['x'][3],
                         0.,
                         reth=True)
        elif pick == 1:
            x3a = minimize(lambda x: tomin(Cteff[:, 0], Cteff[:, 1], [
                x[0], x[1], x[2], x[3], x[4]
            ], [x2b['x'][3] - np.pi / 2, x2b['x'][3] + np.pi / 2], X),
                           np.append(x2b['x'], [0.]),
                           method='Powell',
                           options={'maxiter': 100000})
            xf = minimize(lambda x: tomin(Cteff[:, 0], Cteff[:, 1], [
                x[0], x[1], x[2], x[3], x[4]
            ], [x3a['x'][3] - np.pi / 2, x3a['x'][3] + np.pi / 2], X),
                          np.append(x3a['x'], [0.]),
                          method='Nelder-Mead',
                          options={'maxiter': 100000})
            r, th = gell(Cteff[:, 0],
                         Cteff[:, 1],
                         xf['x'][0],
                         xf['x'][1],
                         xf['x'][2],
                         xf['x'][3],
                         xf['x'][4],
                         reth=True)
        elif pick == 2:
            x3a = minimize(lambda x: tomin2(Cteff[:, 0], Cteff[:, 1], [
                x[0], x[1], x[2], x[3], [0., x[4], x[5], 0.]
            ], [x2b['x'][3] - np.pi / 2, x2b['x'][3] + np.pi / 2], X),
                           np.append(x2b['x'], [0., 0.]),
                           method='Powell',
                           options={'maxiter': 100000})
            xf = minimize(lambda x: tomin2(Cteff[:, 0], Cteff[:, 1], [
                x[0], x[1], x[2], x[3], [0., x[4], x[5], 0.]
            ], [x3a['x'][3] - np.pi / 2, x3a['x'][3] + np.pi / 2], X),
                          x3a['x'],
                          method='Nelder-Mead',
                          options={'maxiter': 100000})
            r, th = gella4(Cteff[:, 0],
                           Cteff[:, 1],
                           xf['x'][0],
                           xf['x'][1],
                           xf['x'][2],
                           xf['x'][3], [0., xf['x'][4], xf['x'][5], 0.],
                           reth=True)
        #TESTING
        # if xf['fun']/len(Cteff[:,0])>rcmin:
        #     print 'Very Bad fit, lets stop here...'
        #     continue
        #     break
        # print np.abs(np.sum(np.diff(th/np.pi))-2)
        # print xf
        thd = np.diff(th / np.pi)
        if np.abs(np.sum(thd[np.abs(thd) < 0.8]) -
                  2) > 0.5 or xf['fun'] > 1e99:
            print 'This fit is probably bad, continue'
            # break
            continue
        #ENDTEST
        px0 = [xf['x'][0], xf['x'][1], xf['x'][2], xf['x'][3]]
        y['ps'].append(p)
        y['x0'].append(xf['x'][0])
        y['y0'].append(xf['x'][1])
        y['q'].append(xf['x'][2])
        tt = xf['x'][3]
        y['theta'].append(tt)
        if pick == 1:
            y['c0'].append(xf['x'][4])
        elif pick == 2:
            y['a4'].append(xf['x'][5])
            y['a'].append([0., xf['x'][4], xf['x'][5], 0.])
        y['val'].append(effpit(100 - p))
        y['r'].append(np.median(r))
        y['DL'].append(x2b['fun'] / xf['fun'])
        y['rc2'].append(xf['fun'] / len(Cteff[:, 0]))
        Ctout.append(Cteff)

        #A goodness of fit:
        if pick == 0:
            Ctf1 = getell(np.median(r), xf['x'][0], xf['x'][1], xf['x'][2],
                          xf['x'][3], 0.)
        elif pick == 1:
            Ctf1 = getell(np.median(r), xf['x'][0], xf['x'][1], xf['x'][2],
                          xf['x'][3], xf['x'][4])
        elif pick == 2:
            Ctf1 = getella4(np.median(r), xf['x'][0], xf['x'][1], xf['x'][2],
                            xf['x'][3], [0., xf['x'][4], xf['x'][5], 0.])
        pctf1 = Ctf1
        idxx = np.array((Ctf1[0]) / dx).astype("int")
        idxx = idxx[(idxx < E[1]) | (idxx >= E[0])]
        idxy = np.array((Ctf1[1]) / dy).astype("int")
        # idxy[(idxy>=E.shape[0]) | (idxy<0)]=0
        idxy = idxy[(idxy < E[3]) | (idxy >= E[2])]
        y['ellval'].append(np.percentile(scieff[idxy, idxx],
                                         [15.9, 50., 84.1]))
        if img:
            # if pick==0:
            #     ri=gell(xx,yy,xf['x'][0],xf['x'][1],xf['x'][2],xf['x'][3],0.)
            #     Ctf1=getell(np.median(r),xf['x'][0],xf['x'][1],xf['x'][2],xf['x'][3],0.)
            # elif pick==1:
            #     ri=gell(xx,yy,xf['x'][0],xf['x'][1],xf['x'][2],xf['x'][3],xf['x'][4])
            #     Ctf1=getell(np.median(r),xf['x'][0],xf['x'][1],xf['x'][2],xf['x'][3],xf['x'][4])
            # elif pick==2:
            #     ri=gella4(xx,yy,xf['x'][0],xf['x'][1],xf['x'][2],xf['x'][3],[0.,xf['x'][4],xf['x'][5],0.])
            #     Ctf1=getella4(np.median(r),xf['x'][0],xf['x'][1],xf['x'][2],xf['x'][3],[0.,xf['x'][4],xf['x'][5],0.])
            plt.imshow(np.arcsinh(sciimg), extent=E, origin='lower')
            plt.plot(Cteff[:, 0], Cteff[:, 1], color='red', lw=2)
            plt.plot(Ctf1[0], Ctf1[1], c='black', lw=1.)

    y['val0'] = np.max(sciimg[mskimg == 1])
    keys = ['r', 'val', 'x0', 'y0', 'theta', 'q', 'ellval']
    if pick == 1: keys.append('c0')
    elif pick == 2:
        keys.append('a4')
        keys.append('a')
    if extrapolate is not None:
        #Extrapolate after I cannot measure ellipse
        #Rollback first
        ir = -1
        #TEST###
        # while True:
        #     if pick==0:
        #         Ctf1=getell(y['r'][ir],y['x0'][ir-1],y['y0'][ir-1],y['q'][ir-1],y['theta'][ir-1],0.)
        #     elif pick==1:
        #         Ctf1=getell(y['r'][ir],y['x0'][ir-1],y['y0'][ir-1],y['q'][ir-1],y['theta'][ir-1],y['c0'][ir-1])
        #     elif pick==2:
        #         Ctf1=getella4(y['r'][ir],y['x0'][ir-1],y['y0'][ir-1],y['q'][ir-1],y['theta'][ir-1],y['a'][ir-1])
        #     idxx=np.array((Ctf1[0])/dx).astype("int")
        #     idxx=idxx[(idxx<E[1]) | (idxx>=E[0])]
        #     idxy=np.array((Ctf1[1])/dy).astype("int")
        #     # idxy[(idxy>=E.shape[0]) | (idxy<0)]=0
        #     idxy=idxy[(idxy<E[3]) | (idxy>=E[2])]
        #     if np.diff(np.percentile(scieff[idxy,idxx],[15.9,84.1]))-np.diff(y['ellval'][ir][[0,2]])>0:break
        #     ir-=1
        # print ir
        ########
        if pick == 0:
            rl = gell(xx, yy, y['x0'][ir], y['y0'][ir], y['q'][ir],
                      y['theta'][ir], 0.)
        if pick == 1:
            rl = gell(xx, yy, y['x0'][ir], y['y0'][ir], y['q'][ir],
                      y['theta'][ir], y['c0'][ir])
        if pick == 2:
            rl = gella4(xx, yy, y['x0'][ir], y['y0'][ir], y['q'][ir],
                        y['theta'][ir], y['a'][ir])
        dr = np.max(
            [np.abs(y['r'][ir - 1] - y['r'][ir]), 2 * (xx[0][1] - xx[0][0])])
        rmax = y['r'][ir] + dr
        pvalm = 1e100
        pvn = 1e100
        while True:
            # valm=np.median(sciimg[(mskimg==1) & (rl<rmax) & (rl>rmax-dr)]) #Minimize
            n = len(
                np.where((mskimg == 1) & (rl < rmax) & (rl > rmax - dr))[0])
            if n == 0:
                print "something is wrong here, aborting extrapolation"
                break
            valm = np.percentile(sciimg[(mskimg == 1) & (rl < rmax) &
                                        (rl > rmax - dr)],
                                 [15.9, 50., 84.1])  #Minimize
            if valm[1] > extrapolate and (np.sqrt(valm[1]) / n -
                                          pvn) / pvn < 1.:
                for key in keys:
                    if key == 'r': y[key].append(rmax - dr / 2)
                    elif key == 'val': y[key].append(valm[1])
                    elif key == 'ellval': y[key].append(valm)
                    else: y[key].append(y[key][-1])
            else:
                valm = lambda rmax: np.abs(
                    np.median(sciimg[
                        (mskimg == 1) & (rl < rmax) &
                        (rl > rmax - dr)]) - extrapolate)  #Minimize
                xv = minimize(
                    lambda x: valm(x)
                    if valm(x) == valm(x) and x >= y['r'][-1] else 1e100,
                    [rmax],
                    method='Nelder-Mead')
                if valm(xv['x'][0]) == valm(xv['x'][0]):
                    for key in keys:
                        if key == 'r': y[key].append(xv['x'][0] - dr / 2)
                        elif key == 'val': y[key].append(valm(xv['x'][0]))
                        elif key == 'ellval':
                            y[key].append(
                                np.percentile(
                                    sciimg[(mskimg == 1) & (rl < xv['x'][0]) &
                                           (rl > xv['x'][0] - dr)],
                                    [15.9, 50., 84.1]))
                        else:
                            y[key].append(y[key][-1])
                break
            pvalm = valm[1]
            pvn = np.sqrt(valm[1]) / n
            rmax += dr
    for key in y:
        y[key] = np.array(y[key])
    #Transform theta to common angle
    tt = np.median(y['theta'])
    y['theta'][y['theta'] > tt + np.pi / 2] -= np.pi
    y['theta'][y['theta'] < tt - np.pi / 2] += np.pi
    rett = [y, Ctout, eeint(y)]
    # if Cts: rett.append(Ctout)
    # ellarr2=
    # rett.append(ellarr2)
    return rett
Beispiel #7
0
sc = 2
wp = 210 * sc
hp = 297 * sc

# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output1.avi', -1, fourcc, 20.0, (640, 480))

while True:
    if webcam:
        # img_arr = np.array(bytearray(urllib.request.urlopen(URL).read()), dtype=np.uint8)
        # img = cv2.imdecode(img_arr, -1)
        success, img = cap.read()
    else:
        img = cv2.imread(path)
    imgCnt, finalCnt = utils.getContours(img, min_area=50000, filter=4)

    if len(finalCnt) != 0:
        biggest = finalCnt[0][2]
        #print(biggest)
        imgWrap = utils.warpImg(img, biggest, wp, hp)

        imgCnt2, finalCnt2 = utils.getContours(imgWrap,
                                               min_area=200,
                                               filter=4,
                                               draw=False)

        if len(finalCnt) != 0:
            for obj in finalCnt2:
                cv2.polylines(imgCnt2, [obj[2]], True, (0, 0, 255), 2)
                nPoints = utils.reorder(obj[2])
path = 'cards.jpg'
ImageWidth = 500
ImageHeight = 500
cap = cv2.VideoCapture(0)
cap.set(10, 160)
cap.set(3, 1920)
cap.set(4, 1080)
scale = 3
wp = 210 * scale
hp = 297 * scale

while True:
    if webcam: success, img = cap.read()
    else: img = cv2.imread(path)
    img, conts = utils.getContours(img,
                                   showCanny=True,
                                   minArea=50000,
                                   filter=4)

    if len(conts) != 0:
        biggest = conts[0][2]
        imgWrap = utils.warpImg(img, biggest, wp, hp)
        imgContours2, conts2 = utils.getContours(imgWrap,
                                                 minArea=2000,
                                                 filter=4,
                                                 cThr=[50, 50],
                                                 draw=False)

        if len(conts) != 0:
            for obj in conts2:
                cv2.polylines(imgContours2, [obj[2]], True, (0, 255, 0), 2)
                nPoints = utils.reorder(obj[2])
import numpy as np
import utils

webcam = False
path = 'img.jpeg'
cap = cv2.VideoCapture(0)
cap.set(10, 160)
cap.set(3, 1920)
cap.set(4, 1080)
scale = 3
wP = 210 * scale
hP = 297 * scale
while True:
    if webcam: success, img = cap.read()
    else: img = cv2.imread(path)
    img, conts = utils.getContours(img, draw=True, minArea=50000, filter=4)
    if len(conts) != 0:
        biggest = conts[0][2]
        # print (biggest)
        imgWarp = utils.warp(img, biggest, wP, hP)
        cv2.imshow('A4', imgWarp)
        img2, conts2 = utils.getContours(imgWarp,
                                         draw=False,
                                         cThreshold=[50, 50],
                                         minArea=2000,
                                         filter=4)
        if (len(conts2) != 0):
            for obj in conts2:
                cv2.polylines(img2, [obj[2]], True, (0, 255, 0), 2)
                newPoints = utils.reorder(obj[2])
                nW = round((utils.findDistance(newPoints[0][0] // scale,
Beispiel #10
0
from sensor_msgs.msg import Image
from std_msgs.msg import String
import utils

cap = cv2.VideoCapture(0)

while True:
    pub = rospy.Publisher("Image", Image, queue_size=10)
    pub2 = rospy.Publisher("Output", String, queue_size=1)
    rospy.init_node('arrow_publisher', anonymous=True)
    rate = rospy.Rate(10)
    success, img = cap.read()

    imgcont, conts, _ = utils.getContours(img,
                                          cThr=[150, 175],
                                          showCanny=False,
                                          filter=4,
                                          draw=False)
    if len(conts) != 0:
        biggest = conts[0][2]
        imgWarp = utils.warpImg(img, biggest, 350,
                                350)  #warping the Image to Region of Interest

        imgCont2, cont2, imgThre = utils.getContours(imgWarp,
                                                     cThr=[50, 50],
                                                     showCanny=False,
                                                     filter=7,
                                                     draw=False)
        if len(cont2) != 0:
            c = utils.direction(imgWarp)
            cv2.imshow("cut", imgThre)