def preprocess(camera, f, imgs):
    img = cam.image(camera, f)
    ###img object contains four data fields: rgb, red, rbr, and cm
    img.undistort(rgb=True)
    ###undistortion
    if img.red is None:
        return
    imgs += [img.red[500:1500, 500:1500]]
Beispiel #2
0
def preprocess(camera, f):
    img = cam.image(camera, f)
    ###img object contains four data fields: rgb, red, rbr, and cm
    img.undistort(camera, rgb=True)
    ###undistortion
    if img.red is None:
        return
    ims = Image.fromarray(img.rgb)
    ims.save(outpath + camID + '/' + os.path.basename(f)[:-4] + '.png', "PNG")
def motion(args):
    camera, day = args

    ymd = day[:8]
    flist = sorted(
        glob.glob(inpath + camera.camID + '/' + ymd + '/' + camera.camID +
                  '_' + day + '*jpg'))
    if len(flist) <= 0:
        return None

    q = deque()
    fh = open(outpath + camera.camID + '/' + camera.camID + '.' + ymd + '.txt',
              'w')
    for f in flist:
        #         print("Start preprocessing ", f[-23:])
        img = cam.image(camera, f)
        ###img object contains four data fields: rgb, red, rbr, and cm
        img.undistort(camera, rgb=True)
        ###undistortion
        #         print("Undistortion completed ", f[-23:])
        if img.rgb is None:
            continue
        q.append(img)
        if len(q) <= 1:
            continue

        ####len(q) is always 2 beyond this point
        if (q[-1].time - q[-2].time).seconds >= MAX_INTERVAL:
            q.popleft()
            q.popleft()
            continue

        r1 = q[-2].rgb[..., 0].astype(np.float32)
        r1[r1 <= 0] = np.nan
        r2 = q[-1].rgb[..., 0].astype(np.float32)
        r2[r2 <= 0] = np.nan
        err0 = r2 - r1
        err0 -= np.nanmean(err0)

        cam.cloud_mask(camera, q[-1], q[-2])
        ###one-layer cloud masking
        fh.write(f[-10:-4], ', ', np.sum(q[-1].cm))
        #         q[-1].dump_img(tmpfs+f[-18:-10]+'/'+f[-23:-4]+'.msk');
        q.popleft()
Beispiel #4
0
def preprocess(camera,f):    
    img=cam.image(camera,f);  ###img object contains four data fields: rgb, red, rbr, and cm 
    img.undistort(rgb=True);  ###undistortion
    if img.rgb is None:
        return
#     plt.imshow(img.rbr,vmin=-0.7,vmax=0.2); plt.show();
    
    
    mask=(img.rgb[:,:,2]>0) & ((img.rgb[:,:,2]<76))  ####all other cameras
#     mask=(img.rgb[:,:,2]>0) & ((img.rgb[:,:,2]<80) | ((img.rgb[:,:,1]<img.rgb[:,:,0]-5) & (img.rgb[:,:,1]<img.rgb[:,:,2]-5)))  ####HD5A

    mask=morphology.binary_closing(mask,np.ones((9,9)))
    mask=remove_small_objects(mask, min_size=15, connectivity=4)
    mask=morphology.binary_dilation(mask,np.ones((21,21))) 
    mask=morphology.binary_closing(mask,np.ones((17,17))) 
    mask=remove_small_objects(mask, min_size=1000, connectivity=4)

    fig,ax=plt.subplots(2,2,sharex=True,sharey=True);     
    ax[0,0].imshow(img.rgb); ax[0,1].imshow(img.rbr,vmin=-0.2,vmax=0.1); 
    ax[1,0].imshow(mask); ax[1,1].imshow(img.rgb[:,:,2]) 
#     plt.figure(); plt.hist(img.rbr[img.rbr>-1],bins=100);
    plt.show()

    np.save(camID+'_mask',mask);   
            TimeStamp = os.path.basename(file)[-18:-4]
            TxtFile = open(TxtFileName, 'a')
            TxtFile.write("#############################\n")
            TxtFile.write("Working at time step %s\n" % TimeStamp)
            TxtFile.write("#############################\n")
            TxtFile.close()
            print("Working at time step %s\n" % TimeStamp)

            ResultTable = ResultTable.append({'TimeStamp': TimeStamp},
                                             ignore_index=True)
            ResultTable = ResultTable.append({'Qlen': len(q)},
                                             ignore_index=True)

            img = cam.image(
                camera, file
            )  ###img object contains four data fields: rgb, red, rbr, and cm

            img.undistort(rgb=True)
            if img.rbr is None:
                TxtFile = open(TxtFileName, 'a')
                TxtFile.write(
                    "Image rbr is None. Getting into the next loop\n")
                TxtFile.close()
                q.clear()
                err.clear()
                fft.clear()
                Tq.clear()

                continue
def height_computing(args):
    imager, neighbors, Date, TimeStamp, DocumFilename = args

    f = inpath + imager.camID + '/' + Date + '/' + imager.camID + '_' + Date + TimeStamp + '.jpg'

    if not os.path.exists(f):
        return -1

    img = cam.image(imager, f)
    img.undistort(imager, rgb=True)

    if img.red is not None:
        img.cloud_mask(imager)
    else:
        #    if img.red is None or img.layers <= 0:
        if WRITE:
            Document = open(DocumFilename, 'a')
            Document.write("img.red is None or img.layers <=0")
            Document.close()
        return -1

    h = [[]] * img.layers

    for neighbor in neighbors:
        f_nb = f.replace(imager.camID, neighbor.camID)

        img_nb = cam.image(neighbor, f_nb)
        img_nb.undistort(neighbor, rgb=True)
        if img_nb.red is not None:
            img_nb.cloud_mask(neighbor)
        else:
            if PRINT:
                print("img_nb.red is None")
            continue

        distance = 6367e3 * geo.distance_sphere(img.lat, img.lon, img_nb.lat,
                                                img_nb.lon)
        for ih in range(img.layers):
            if (len(h[ih])) >= 1:
                continue
            res = cam.cloud_height(img, img_nb, layer=ih, distance=distance)

            if len(res) >= 1 and res[0] < 30 * distance and res[
                    0] > 0.5 * distance:
                h[ih] = res[0]
                if PRINT:
                    print("with camera " + imager.camID + "neighbor " +
                          neighbor.camID)
                    print("the cloud height is")
                    print(res[0])
                return res[0]

    #  if len(h) >= img.layers:
    #      if PRINT:
    #          print("with camera " + imager.camID + " neighbor " + neighbor.camID)
    #          print("len(h) = " + str(len(h)))
    #          print("img.layers =" + str(img.layers))
    #      break

    if (len(h[0]) == 0):
        if PRINT:
            print("len(h[0]) == 0")
    return -1
def preprocess(camera,f,q,err,fft,convolver,flag):    
    img=cam.image(camera,f);  ###img object contains four data fields: rgb, red, rbr, and cm 
    img.undistort(rgb=True);  ###undistortion
    if img.red is None:
        return
#     ims = Image.fromarray(img.rgb); ims.save(outpath+camID+'/'+os.path.basename(f), "PNG"); continue
    img.cloud_mask();    ###one-layer cloud masking
    q.append(img)       
    if len(q)<=1: 
        return
    ####len(q) is always 2 beyond this point
    if (q[-1].time-q[-2].time).seconds>=MAX_INTERVAL:
        q.popleft();
        return;
#####cloud motion for the dominant layer    
#     im1=q[-2].red.copy().astype(np.float32); im2=q[-1].red.copy().astype(np.float32); 
#     vy,vx,max_corr = cam.cloud_motion(im1,im2,mask1=im1>5,mask2=np.abs(im1-im2)>15, ratio=0.7, threads=4) 
#     print(camera.camID+', ', f[-18:-4]+',  first layer2:',max_corr,vy,vx) 
    if convolver is None:
        shape=(camera.nx,camera.ny)
        convolver = mncc.Convolver(shape, shape, threads=4, dtype=np.float32)  # 
    for ii in range(len(fft)-2,0):
        im=q[ii].red.astype(np.float32);
        mask = im>0; #im[~mask]=0        
        fft.append(convolver.FFT(im,mask,reverse=flag[0]>0));
        flag[0] *= -1
    vy,vx,max_corr = cam.cloud_motion_fft(convolver,fft[-2],fft[-1],ratio=0.8); 
    if vx is None or vy is None: #####invalid cloud motion
        q.popleft(); fft.popleft();
        return
    vy*=flag[0]; vx*=flag[0]; 
    fft.popleft();
    print(camera.camID+', ', f[-18:-4]+',  first layer:',max_corr,vy,vx) 
#     plt.figure(); plt.imshow(q[-2].red); plt.colorbar(); plt.show();   
    return;

    q[-2].v+=[[vy,vx]]     
    red1=st.shift_2d(q[-1].rgb[:,:,0].astype(np.float32),-vx,-vy); red1[red1<=0]=np.nan
    red2=q[-2].rgb[:,:,0].astype(np.float32); red2[red2<=0]=np.nan #red2-=np.nanmean(red2-q[-1].rgb[:,:,0])
    er=red2-red1;   ###difference image after cloud motion adjustment
    er[(red1==0)|(red2==0)]=np.nan;
    a=er.copy(); a[a>0]=0; er-=st.rolling_mean2(a,500);
    if len(err) <= 0:
        err += [-st.shift_2d(er,vx,vy)] 
        return
    err_2=err[0].copy();  err[0] = (-st.shift_2d(er,vx,vy))
#     if vy**2+vx**2>=50**2:  ######The motion of the dominant layer is fast, likely low clouds. Do NOT trigger the second layer algorithm 
#        return 

#####process the secondary layer 
    ert=er+err_2;    
#     cm2=(er>15) | (er_p>15); cm2=remove_small_objects(cm2, min_size=500, connectivity=4);  
    scale=red2/np.nanmean(red2); nopen=max(5,int(np.sqrt(vx**2+vy**2)/3))
    cm2=(ert>15*scale) & (q[-2].cm); 
    cm2=morphology.binary_opening(cm2,np.ones((nopen,nopen)))
    cm2=remove_small_objects(cm2, min_size=500, connectivity=4);  
#    sec_layer=np.sum(cm2)/len(cm2.ravel())  ###the amount of clouds in secondary layer
    sec_layer=np.sum(cm2)/np.sum(q[-2].cm)  ###the amount of clouds in secondary layer
    if sec_layer<5e-2:   ###too few pixels in second layer, ignore the second cloud layer
        print('Second layer is small:', sec_layer*100, '%')
        return

#####cloud motion for the secondary layer   
    mask2=np.abs(err_2)>5;
    mask2=remove_small_objects(mask2, min_size=500, connectivity=4)
    mask2=filters.maximum_filter(mask2,20) 
    vy,vx,max_corr = cam.cloud_motion(err[0],err_2,mask1=None,mask2=mask2, ratio=None, threads=4) 
    if vx is None or vy is None:
        return
    q[-2].v+=[[vy,vx]]
    print(camera.camID+', ', f[-18:-4]+',  second layer:',max_corr,vy,vx) 
    
    if np.abs(vy-q[-2].v[0][0])+np.abs(vx-q[-2].v[0][1])>10:   
#     if np.abs(vy)+np.abs(vx)>0:
    #####obtain the mask for secondar cloud layer using a watershed-like algorithm    
        mred=q[-2].rgb[:,:,0].astype(np.float32)-st.fill_by_mean2(q[-2].rgb[:,:,0],200,mask=~cm2)
        mrbr=q[-2].rbr-st.fill_by_mean2(q[-2].rbr,200,mask=~cm2)
        merr=st.rolling_mean2(ert,200,ignore=np.nan); var_err=(st.rolling_mean2(ert**2,200,ignore=np.nan)-merr**2)
    #     mk=(np.abs(q[-2].rgb[:,:,0].astype(np.float32)-mred)<3) & ((total_err)>-2) & (np.abs(q[-2].rbr-mrbr)<0.05)
        mk=(np.abs(mred)<3) & (ert>-15) & (np.abs(mrbr)<0.05) & (var_err>20*20)
        cm2=morphology.binary_opening(mk|cm2,np.ones((nopen,nopen)))  ####remove line objects produced by cloud deformation
        cm2=remove_small_objects(cm2, min_size=500, connectivity=4)
        q[-2].layers=2; q[-2].cm[cm2]=2;  #####update the cloud mask with secondary cloud layer 
                
#         fig,ax=plt.subplots(2,2, sharex=True,sharey=True);  ####visualize the cloud masking results
#         ax[0,0].imshow(q[-2].rgb); ax[0,1].imshow(q[-2].cm)
#         ax[1,0].imshow(st.shift_2d(q[-1].rgb,-vx,-vy))  
#         ax[1,1].imshow(er,vmin=-25,vmax=25); plt.show();          
    return;
def preprocess(args):
    camera, day = args

    ymd = day[:8]
    flist = sorted(
        glob.glob(inpath + camera.camID + '/' + ymd + '/' + camera.camID +
                  '_' + day + '*jpg'))
    if len(flist) <= 0:
        return None

    q = deque()
    fft = deque()
    ###queues
    flag = [-1]
    shape = (camera.nx, camera.ny)
    convolver = mncc.Convolver(shape, shape, threads=4, dtype=np.float32)  #
    for f in flist:
        #         print("Start preprocessing ", f[-23:])
        img = cam.image(camera, f)
        ###img object contains four data fields: rgb, red, rbr, and cm
        img.undistort(camera, rgb=True)
        ###undistortion
        #         print("Undistortion completed ", f[-23:])
        if img.rgb is None:
            continue
        img.cloud_mask(camera)
        ###one-layer cloud masking
        #         print("Cloud masking completed ", f[-23:])

        q.append(img)
        if len(q) <= 1:
            img.dump_img(tmpfs + f[-23:-4] + '.pkl')
            continue
        ####len(q) is always 2 beyond this point
        if (q[-1].time - q[-2].time).seconds >= MAX_INTERVAL:
            img.dump_img(tmpfs + f[-23:-4] + '.pkl')
            q.popleft()
            continue
    #####cloud motion for the dominant layer
        for ii in range(len(fft) - 2, 0):
            im = q[ii].red.astype(np.float32)
            mask = im > 0
            #im[~mask]=0
            fft.append(convolver.FFT(im, mask, reverse=flag[0] > 0))
            flag[0] *= -1
        vy, vx, max_corr = cam.cloud_motion_fft(convolver,
                                                fft[-2],
                                                fft[-1],
                                                ratio=0.8)
        if vx is None or vy is None:  #####invalid cloud motion
            img.dump_img(tmpfs + f[-23:-4] + '.pkl')
            q.popleft()
            fft.popleft()
            continue
        vy *= flag[0]
        vx *= flag[0]
        img.v += [[vy, vx]]
        img.dump_img(tmpfs + f[-23:-4] + '.pkl')
        print(camera.camID + ', ', f[-18:-4] + ',  first layer:', len(fft),
              max_corr, vy, vx)

        if SAVE_FIG:
            fig, ax = plt.subplots(2,
                                   2,
                                   figsize=(12, 6),
                                   sharex=True,
                                   sharey=True)
            ax[0, 0].imshow(q[-2].rgb)
            ax[0, 1].imshow(q[-1].rgb)
            ax[1, 0].imshow(q[-2].cm)
            ax[1, 1].imshow(q[-1].cm)
            plt.tight_layout()
            plt.show()

        fft.popleft()
        q.popleft()
def mask_motion(args):
    camera, day = args
    ymd = day[:8]
    flist = sorted(
        glob.glob(inpath + camera.camID + '/' + ymd + '/' + camera.camID +
                  '_' + day + '*jpg'))
    if len(flist) <= 0:
        return

    q = deque()
    for f in flist:
        print('Processing', f)
        if (~REPROCESS) and os.path.isfile(tmpfs + f[-18:-10] + '/' +
                                           f[-23:-4] + '.pkl'):
            continue

        img = cam.image(camera, f)
        ###img object contains four data fields: rgb, red, rbr, and cm
        img.undistort(camera, rgb=True)
        ###undistortion
        if img.rgb is None:
            continue
        q.append(img)

        if len(q) <= 1:
            continue
        ####len(q) is always 2 beyond this point

        if (q[-1].time - q[-2].time).seconds >= MAX_INTERVAL:
            q.popleft()
            q.popleft()
            continue

        r1 = q[-2].red.astype(np.float32)
        r1[r1 <= 0] = np.nan
        r2 = q[-1].red.astype(np.float32)
        r2[r2 <= 0] = np.nan
        err0 = r2 - r1

        dif = np.abs(err0)
        dif = st.rolling_mean2(dif, 20)
        semi_static = (abs(dif) < 10) & (r1 - 127 > 100)
        semi_static = morphology.binary_closing(semi_static, np.ones((10, 10)))
        semi_static = remove_small_objects(semi_static,
                                           min_size=200,
                                           in_place=True)
        q[-1].rgb[semi_static] = 0
        r2[semi_static] = np.nan

        cam.cloud_mask(camera, q[-1], q[-2])
        ###one-layer cloud masking
        if (q[-1].cm is None):
            q.popleft()
            continue
        if (np.sum(
            (q[-1].cm > 0)) < 2e-2 * img.nx * img.ny):  ######cloud free case
            q[-1].layers = 0
        else:
            dilated_cm = morphology.binary_dilation(q[-1].cm, np.ones(
                (15, 15)))
            dilated_cm &= (r2 > 0)
            vy, vx, max_corr = cam.cloud_motion(r1,
                                                r2,
                                                mask1=r1 > 0,
                                                mask2=dilated_cm,
                                                ratio=0.7,
                                                threads=4)
            if np.isnan(vy):
                q[-1].layers = 0
            else:
                q[-1].v += [[vy, vx]]
                q[-1].layers = 1

        q[-1].dump_img(tmpfs + f[-18:-10] + '/' + f[-23:-4] + '.pkl')
        q.popleft()
Beispiel #10
0
    q = deque()
    err = deque()
    fft = deque()
    for i, f in enumerate(flist):
        ####visualize the cloud mask
        if len(q) >= 2:
            fig, ax = plt.subplots(1, 2, sharex=True, sharey=True)
            ax[0].imshow(q[-2].rgb)
            ax[0].axis('off')  #####original
            ax[1].imshow(q[-2].cm)
            ax[1].axis('off')  ####cloud mask
            fig.savefig(dest + os.path.basename(f))
            plt.close(fig)
            q.popleft()

        img = cam.image(camera, f)
        ###img object contains four data fields: rgb, red, rbr, and cm
        img.undistort(rgb=True)
        ###undistortion
        if img.rbr is None:
            q.clear()
            err.clear()
            fft.clear()
            continue
#     ims = Image.fromarray(img.rgb); ims.save(outpath+camID+'/'+os.path.basename(f), "PNG"); continue
        img.cloud_mask()
        ###cloud masking
        q.append(img)
        if len(q) <= 1: continue

        #####cloud motion for the dominant layer
    Document.write(Date + '\n')
    Document.close()

    flist = sorted(
        glob.glob(inpath + camCenterID + '/' + Date + '/' + camCenterID + '_' +
                  Date + str(hour) + '*jpg'))

    if len(flist) == 0:
        if PRINT:
            print("Warning: File list is empty")
        continue
    try:
        for f in flist:
            TimeStamp = os.path.basename(f)[-10:-4]

            img = cam.image(cameraCenterObject, f)
            img.undistort(cameraCenterObject, rgb=True)
            if img.rgb is None:
                continue
            img.cloud_mask(cameraCenterObject)

            cloudMask = img.cm
            cloudFraction = (cloudMask > 0).sum() / (cloudMask.shape[0] *
                                                     cloudMask.shape[1])

            q.append(img)

            if len(q) <= 1:
                continue

            if (q[-1].time - q[-2].time).seconds >= MAX_INTERVAL:
Beispiel #12
0
def motion(args):
    camera,day=args  
    
    ymd=day[:8]
    flist = sorted(glob.glob(inpath+camera.camID+'/'+ymd+'/'+camera.camID+'_'+day+'*jpg'))
    if len(flist)<=0:
        return None

    q=deque();      
    for f in flist:
#         print("Start preprocessing ", f[-23:])
        img=cam.image(camera,f);  ###img object contains four data fields: rgb, red, rbr, and cm 
        img.undistort(camera,rgb=True);  ###undistortion
#         print("Undistortion completed ", f[-23:])
        if img.rgb is None:
            continue
        q.append(img)  
#         gray_img = cv2.cvtColor(img.rgb, cv2.COLOR_BGR2GRAY)
#         cv2.imwrite(tmpfs+f[-23:],gray_img);

        if len(q)<=1: 
            continue
        ####len(q) is always 2 beyond this point
        if (q[-1].time-q[-2].time).seconds>=MAX_INTERVAL:
            q.popleft(); q.popleft();
            continue;
        
        cam.cloud_mask(camera,q[-1],q[-2]); ###one-layer cloud masking        
        
        r1=q[-2].red.astype(np.float32); r1[r1<=0]=np.nan
        r2=q[-1].red.astype(np.float32); r2[r2<=0]=np.nan
        err0 = r2-r1;
        
        dilated_cm=morphology.binary_dilation(q[-1].cm,np.ones((15,15))); dilated_cm &= (r2>0)
        vy,vx,max_corr = cam.cloud_motion(r1,r2,mask1=r1>0,mask2=dilated_cm, ratio=0.7, threads=4);
        if vy is None:
            q.popleft(); 
            continue
        q[-1].v += [[vy,vx]]; q[-1].layers+=1;        
        
        err = r2-st.shift2(r1,-vx,-vy);  err[(r2==0) | (st.shift2(r1,-vx,-vy)==0)]=np.nan; 
        print(f[-23:],', first layer:',vy,vx,max_corr)

        mask2=st.rolling_mean2(np.abs(err)-np.abs(err0),40)<0
#         mask2 = (np.abs(err)-np.abs(err0)<0) #| (np.abs(err)<1);
        mask2=remove_small_objects(mask2,min_size=900, in_place=True)
        mask2=morphology.binary_dilation(mask2,np.ones((15,15)))
        mask2 = (~mask2) & (r2>0) & (np.abs(r2-127)<30) & (np.abs(err)>=1) #& (q[-1].cm>0)
#         print(np.sum(mask2 & (q[-1].cm>0))/(img.nx*img.ny))
        if np.sum(mask2 & (q[-1].cm>0))>2e-2*img.nx*img.ny:
            vy,vx,max_corr = cam.cloud_motion(r1,r2,mask1=r1>0,mask2=mask2, ratio=0.7, threads=4);
            if vy is None:
                q.popleft(); 
                continue
            vdist = np.sqrt((vy-q[-1].v[-1][0])**2+(vx-q[-1].v[-1][1])**2)
            if vdist>=5 and np.abs(vy)+np.abs(vx)>2.5 and vdist>0.3*np.sqrt(q[-1].v[-1][0]**2+q[-1].v[-1][1]**2):
                score1=np.nanmean(np.abs(err[mask2])); 
                err2=r2-st.shift2(r1,-vx,-vy); err2[(r2==0) | (st.shift2(r1,-vx,-vy)==0)]=np.nan;  
                score2=np.nanmean(np.abs(err2[mask2]));
                print("Score 1 and score 2: ", score1,score2);
                if score2<score1:
                    q[-1].v += [[vy,vx]]; q[-1].layers=2;
                    dif=st.rolling_mean2(np.abs(err)-np.abs(err2),40)>0
                    dif=remove_small_objects(dif,min_size=300, in_place=True)
                    q[-1].cm[dif & (q[-1].cm>0)]=q[-1].layers; 
                    print(f[-23:],', second layer:',vy,vx,max_corr)
       
        q[-1].dump_img(tmpfs+f[-18:-10]+'/'+f[-23:-4]+'.pkl');

        if SAVE_FIG:
            fig,ax=plt.subplots(2,2,figsize=(9,9),sharex=True,sharey=True);
            ax[0,0].imshow(mask2); ax[0,1].imshow(q[-1].red); 
            ax[1,0].imshow(q[-1].cm); ax[1,1].imshow(q[-1].rgb);
            plt.tight_layout(); 
	    plt.show();     
           # fig.savefig(outpath+ymdhms); 
        q.popleft();