Пример #1
0
 def __init__(self,filename,imageL='tmp/inputL.jpg',imageR='tmp/inputR.jpg'):
     print 'model created'
     self.filename = filename
     try: #make sure OpenCV can actually process the images
         self.testL = cv2.pyrDown(cv2.imread(imageL))
         self.testR = cv2.pyrDown(cv2.imread(imageR))
         assert (self.testR.size == self.testL.size)
         self.width,self.height = self.testL.shape[:2] #scales down
         
         
         #convert image to smaller size
         loadedImageL = cv.LoadImage(imageL,cv.CV_LOAD_IMAGE_COLOR)
         destL = cv.CreateImage((self.height,self.width), 8, 3)
         # 8 = bits, 3 = color
         cv.Resize(loadedImageL,destL,interpolation=cv.CV_INTER_AREA)
         cv.SaveImage(imageL, destL)
         
         #convert image to smaller size
         loadedImageR = cv.LoadImage(imageR,cv.CV_LOAD_IMAGE_COLOR)
         destR = cv.CreateImage((self.height,self.width), 8, 3)
         cv.Resize(loadedImageR,destR,interpolation=cv.CV_INTER_AREA)
         cv.SaveImage(imageR, destR)
         
         self.imageL = cv2.pyrDown(cv2.imread(imageL))
         self.imageR = cv2.pyrDown(cv2.imread(imageR))
     except Exception as e:
         print e
         quit()
Пример #2
0
def get_tle_vid_goos(vid_name,divisor):
    cap = cv2.VideoCapture(vid_name)
    time = 0
    tot_time=int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    energies=np.zeros(tot_time,dtype=np.int64)
    cap.set(cv2.CAP_PROP_POS_FRAMES,time)
    ret,frame = cap.read()
    in_frame=np.zeros(frame.shape,dtype=np.int64)
    small_frame=cv2.pyrDown(frame)
    blur_frame=np.zeros(frame.shape,dtype=np.int64)
    goose=cv2.getGaussianKernel(frame.shape[0],frame.shape[0]/divisor)
    goose1=cv2.getGaussianKernel(frame.shape[1],frame.shape[1]/divisor)
    goosq=np.dot(goose,np.transpose(goose1))
    m=1/np.max(goosq)
    center_frame=np.zeros(frame.shape,dtype=np.float64)
    for color in range(3):
        center_frame[:,:,color]=goosq*m
    print tot_time
    while(time<tot_time):
        in_frame[:,:,:]=frame
        small_frame[:,:,:]=cv2.pyrDown(frame)
        blur_frame[:,:,:]=cv2.pyrUp(small_frame)[:frame.shape[0],:frame.shape[1],:]
        tle=np.sum(center_frame*np.abs(np.subtract(in_frame,blur_frame)))
        energies[time]=tle
        if time%50==0:
            print time
        time=time+1
        cap.set(cv2.CAP_PROP_POS_FRAMES,time)
        ret,frame = cap.read()
        
    return energies
Пример #3
0
def downsample_images(img1, img2):
    """Downsamples an image pair."""
    # img1 and img2 are HxWx3 arrays (rows, columns, 3-colour channels)
    img1 = cv2.pyrDown(img1)
    img2 = cv2.pyrDown(img2)

    return img1, img2
Пример #4
0
def vidvol(in_name,outshape,slope,numframes,lev,start_frame,r,skip,margin):
    time=1
    cap=cv2.VideoCapture(in_name)
    ans=np.zeros(outshape,dtype=np.uint8)
    cap.set(cv2.CAP_PROP_POS_FRAMES,start_frame)
    ret,frame=cap.read()
    for l in range(lev):
        frame=cv2.pyrDown(frame)
    y=frame.shape[0]
    x=frame.shape[1]
    ans[:y,margin:x+margin,3]=255
    ans[:y,margin:x+margin,:3]=frame
    framebox=np.zeros((y,x,4),dtype=np.uint8)
    framebox[:,:,3]=255
    while time<numframes:
        cap.set(cv2.CAP_PROP_POS_FRAMES,start_frame+time*skip)
        ret,frame=cap.read()
        for l in range(lev):
            frame=cv2.pyrDown(frame)
        inset=time/slope
        if np.random.rand(1)[0]>.9:
            frame[:,0,:]=0
            frame[:,0,2]=255
            frame[0,:,:]=0
            frame[0,:,2]=255
        framebox[:,:,:3]=frame
        t=time+margin
        ans[inset:y+inset,t:x+t,:]=r*ans[inset:y+inset,t:x+t,:]+(1-r)*framebox[:y,:x,:]
        time=time+1
    ans[inset:y+inset,time:x+time,:]=framebox
    return ans
    def apply(self, img, paramDict = dict()):
        out = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        for k in range(self.n):
            out = cv2.pyrDown(out)
        downUp = cv2.pyrUp(cv2.pyrDown(out))

        return out - downUp
Пример #6
0
def get_best_match_pyramid(img1, img2, sharpen_factor, h_factor, w_factor, window, threshold, step):
    
    # Generate Gaussian pyramid, level 0 is [img1, img2]
    # level 1
    img1_down1 = cv2.pyrDown(img1)
    img2_down1 = cv2.pyrDown(img2)
    
    # level 2
    img1_down2 = cv2.pyrDown(img1_down1)
    img2_down2 = cv2.pyrDown(img2_down1)
    
    # sharpen the smallest images, then find best match
    img1_down2 = sharpen(img1_down2, sharpen_factor)
    img2_down2 = sharpen(img2_down2, sharpen_factor)
    
    # get roi
    img1_down2_roi = task1loop.get_roi(img1_down2, h_factor, w_factor, window, threshold, step)
    img2_down2_roi = task1loop.get_roi(img2_down2, h_factor, w_factor, window, threshold, step)
    
    # get best match
    h1, w1, h2, w2, best_match = task1loop.get_best_match(img1_down2_roi, img2_down2_roi, window, step)
    h1, w1, h2, w2, best_match = get_best_match_new_resolution(img1_down1, img2_down1, h1*2, w1*2, h2*2, w2*2, window, step)
    h1, w1, h2, w2, best_match = get_best_match_new_resolution(img1, img2, h1*2, w1*2, h2*2, w2*2, window, step)
    
    return h1, w1, h2, w2, best_match
Пример #7
0
def drawMatch(img1, img2, pt1, pt2, good = True, scale = 2):
    realscale = 2
    img1 = cv2.pyrDown(img1)
    img2 = cv2.pyrDown(img2)
    if scale == 4:
        realscale = 4
        img1 = cv2.pyrDown(img1)
        img2 = cv2.pyrDown(img2)

    h, w, c = img1.shape
    out = np.zeros((h, w * 2, 3), np.uint8)
    out[:,:w,:] = img1
    out[:,w:,:] = img2

    color = (255, 255, 0) # cyan
    if good == False:
        color = (0, 0, 255)
    elif good == True:
        color = (0, 255, 0)

    p1 = (int(pt1[0] / realscale), int(pt1[1] / realscale))
    p2 = (int(pt2[0] / realscale + w), int(pt2[1] / realscale))
    text = "pt1(%d, %d), pt2(%d, %d)" % (int(pt1[0]), int(pt1[1]), int(pt2[0]), int(pt2[1]))
    cv2.putText(out, text, (20, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0))
    cv2.circle(out, p1, 10, color, 1)
    cv2.circle(out, p2, 10, color, 1)
    cv2.line(out, p1, p2, color, 1)
    cv2.imshow("match", out)
Пример #8
0
def get_image_diff (img1, img2):
	"""
		Function: get_image_diff
		------------------------
		given two images, this finds the eroded/dilated difference 
		between them on a coarse grain.
		NOTE: assumes both are full-size, color
	"""
	#=====[ Step 1: convert to gray	]=====
	img1_gray = cv2.cvtColor (img1, cv2.COLOR_BGR2GRAY)
	img2_gray = cv2.cvtColor (img2, cv2.COLOR_BGR2GRAY)	

	#=====[ Step 2: downsample 	]=====
	img1_small = cv2.pyrDown(cv2.pyrDown(img1_gray))
	img2_small = cv2.pyrDown(cv2.pyrDown(img2_gray))	

	#=====[ Step 3: find differnece	]=====
	difference = img2_small - img1_small

	#=====[ Step 4: erode -> dilate	]=====
	kernel = np.ones ((4, 4), np.uint8)
	difference_ed = cv2.dilate(cv2.erode (difference, kernel), kernel)

	#=====[ Step 5: blow back up	]=====
	return cv2.pyrUp (cv2.pyrUp (difference_ed))
Пример #9
0
    def slow_action(self):
        """
        This is while the workers are running. You may do a lot of work here
        (preferably not more than the time of execution of workers).
        """
        ret, frame = self.cam.read()
        self.current_frame = cv2.resize(frame, dsize=(self.prop_dict['array'][0].shape[1], self.prop_dict['array'][0].shape[0]))
        self.current_frame1 = cv2.pyrDown(self.current_frame)
        self.current_frame2 = cv2.pyrDown(self.current_frame1)
        self.current_frame3 = cv2.resize(self.current_frame2, dsize=(self.prop_dict['array3'][0].shape[1], self.prop_dict['array3'][0].shape[0]))

        im0 = np.hstack((self.prop_dict['array'][0].view(np.ndarray), self.prop_dict['predicted_array'][0].view(np.ndarray), cv2.absdiff(self.prop_dict['predicted_array'][0].view(np.ndarray), self.prop_dict['array'][0].view(np.ndarray))))
        im1 = np.hstack((self.prop_dict['array1'][0].view(np.ndarray), self.prop_dict['predicted_array1'][0].view(np.ndarray), cv2.absdiff(self.prop_dict['predicted_array1'][0].view(np.ndarray), self.prop_dict['array1'][0].view(np.ndarray))))
        im1 = cv2.resize(im1, dsize=(im0.shape[1], im0.shape[0]))
        im2 = np.hstack((self.prop_dict['array2'][0].view(np.ndarray), self.prop_dict['predicted_array2'][0].view(np.ndarray), cv2.absdiff(self.prop_dict['predicted_array2'][0].view(np.ndarray), self.prop_dict['array2'][0].view(np.ndarray))))
        im2 = cv2.resize(im2, dsize=(im0.shape[1], im0.shape[0]))
        im3 = np.hstack((self.prop_dict['array3'][0].view(np.ndarray), self.prop_dict['predicted_array3'][0].view(np.ndarray), cv2.absdiff(self.prop_dict['predicted_array3'][0].view(np.ndarray), self.prop_dict['array3'][0].view(np.ndarray))))
        im3 = cv2.resize(im3, dsize=(im0.shape[1], im0.shape[0]))
        im = np.vstack((im0, im1, im2, im3))
        cv2.imshow("Scale3", im)  # imshow is slow, its better to call it just once
        key = cv2.waitKey(1) & 0xFF
        if key == 27 or self.steps > self.steps_to_run:
            self._running = False
        now = time.time()
        self.steps += 1
        sps = self.steps / (now-self.t_start)
        print ("%3.3f steps per sec" % (sps)) + "\r",
Пример #10
0
def texturemapGridSequence():
    """ Skeleton for texturemapping on a video sequence"""
    fn = 'GridVideos/grid1.mp4'
    cap = cv2.VideoCapture(fn)
    drawContours = True;

    texture = cv2.imread('Images/ITULogo.jpg')
    texture = cv2.pyrDown(texture)


    mTex, nTex, t = texture.shape

    texturePoints = np.array([[0, 0],
                              [0, nTex],
                              [mTex, 0],
                              [mTex, nTex]], dtype=np.float32)

    # load Tracking data
    running, imgOrig = cap.read()
    mI, nI, t = imgOrig.shape

    cv2.imshow("win2", imgOrig)

    pattern_size = (9, 6)

    idx = [0, 8, 45, 53]
    while(running):
    # load Tracking data
        running, imgOrig = cap.read()
        if(running):
            imgOrig = cv2.pyrDown(imgOrig)
            gray = cv2.cvtColor(imgOrig, cv2.COLOR_BGR2GRAY)
            found, corners = cv2.findChessboardCorners(gray, pattern_size)
            if found:
                term = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_COUNT, 30, 0.1)
                cv2.cornerSubPix(gray, corners, (5, 5), (-1, -1), term)
#                 cv2.drawChessboardCorners(imgOrig, pattern_size, corners, found)

                imagePoints = np.array([[corners[0][0][0], corners[0][0][1]],
                                        [corners[8][0][0], corners[8][0][1]],
                                        [corners[45][0][0], corners[45][0][1]],
                                        [corners[53][0][0], corners[53][0][1]]], dtype=np.float32)

                homography = cv2.getPerspectiveTransform(texturePoints, imagePoints)

                overlay = cv2.warpPerspective(texture, homography, (imgOrig.shape[1], imgOrig.shape[0]))

                imgOrig = cv2.addWeighted(imgOrig, 0.5, overlay, 0.5, 0)

                # Nicer overlay, but runs slower
#                 for y, row in enumerate(imgOrig):
#                     for x, color in enumerate(row):
#                         if overlay[y][x][0] != 0 and overlay[y][x][1] != 0 and overlay[y][x][2] != 0:
#                              imgOrig[y][x] = overlay[y][x]

                for t in idx:
                    cv2.circle(imgOrig, (int(corners[t, 0, 0]), int(corners[t, 0, 1])), 10, (255, t, t))
            cv2.imshow("win2", imgOrig)
            cv2.waitKey(1)
Пример #11
0
def doPyrDown(img):
	width,height,channel = img.shape
	assert width%2 == 0 and height%2 == 0 , "error"

	size = width/2,height/2,channel
	out = np.zeros(size,dtype=np.uint8)
	cv2.pyrDown(img,out)
	return out
Пример #12
0
def main():
    displayer = Displayer()

    A = cv2.cvtColor(fetch_image('apple.jpg'), cv2.COLOR_BGR2RGB)
    B = cv2.cvtColor(fetch_image('orange.jpg'), cv2.COLOR_BGR2RGB)

    # generate Gaussian pyramid for A
    G = A.copy()
    gpA = [G]
    for i in xrange(6):
        G = cv2.pyrDown(G)
        gpA.append(G)

    # generate Gaussian pyramid for B
    G = B.copy()
    gpB = [G]
    for i in xrange(6):
        G = cv2.pyrDown(G)
        gpB.append(G)

    # generate Laplacian Pyramid for A
    lpA = [gpA[5]]
    for i in xrange(5,0,-1):
        GE = cv2.pyrUp(gpA[i])
        L = cv2.subtract(gpA[i-1],GE)
        lpA.append(L)

    # generate Laplacian Pyramid for B
    lpB = [gpB[5]]
    for i in xrange(5,0,-1):
        GE = cv2.pyrUp(gpB[i])
        L = cv2.subtract(gpB[i-1],GE)
        lpB.append(L)

    # Now add left and right halves of images in each level
    LS = []
    for la,lb in zip(lpA,lpB):
        rows,cols,dpt = la.shape
        ls = np.hstack((la[:,0:cols/2], lb[:,cols/2:]))
        LS.append(ls)

    # now reconstruct
    ls_ = LS[0]
    for i in xrange(1,6):
        ls_ = cv2.pyrUp(ls_)
        displayer.add_image(ls_, i)
        ls_ = cv2.add(ls_, LS[i])

    # image with direct connecting each half
    real = np.hstack((A[:,:cols/2],B[:,cols/2:]))

    displayer.add_image(ls_, "pyramid")
    #displayer.add_image(real, "stacked")

    #for i in range(len(lpA)):
    #    displayer.add_image(LS[i], i)

    displayer.display()
Пример #13
0
def texturemapGridSequence():
    """ Skeleton for texturemapping on a video sequence"""
    fn = 'data/GridVideos/grid1.mp4'
    cap = cv2.VideoCapture(fn)
    drawContours = True;

    texture = cv2.imread('data/Images/ITULogo.jpg')
    texture = cv2.pyrDown(texture)

    mTex,nTex,t = texture.shape

    # Use the corners of the texture
    srcPoints = [
            (float(0.0),float(0.0)),
            (float(nTex),0),
            (float(nTex),float(mTex)),
            (0,mTex)]
    
    #load Tracking data
    running, imgOrig = cap.read()
    mI,nI,t = imgOrig.shape

    cv2.imshow("win2",imgOrig)

    pattern_size = (9, 6)

    idx = [0,8,53,45]
    while(running):
    #load Tracking data
        running, imgOrig = cap.read()
        if(running):
            imgOrig = cv2.pyrDown(imgOrig)
            gray = cv2.cvtColor(imgOrig,cv2.COLOR_BGR2GRAY)

            m,n = gray.shape

            found, corners = cv2.findChessboardCorners(gray, pattern_size)
            if found:
                term = ( cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_COUNT, 30, 0.1 )
              #  cv2.cornerSubPix(gray, corners, (5, 5), (-1, -1), term)
              #  cv2.drawChessboardCorners(imgOrig, pattern_size, corners, found)
                # Get the points based on the chessboard
                dstPoints = []
                for t in idx:
                    dstPoints.append((int(corners[t,0,0]),int(corners[t,0,1])))
                    #cv2.circle(imgOrig,(int(corners[t,0,0]),int(corners[t,0,1])),10,(255,t,t))
                
                H = SIGBTools.estimateHomography(srcPoints,dstPoints)
                
                overlay = cv2.warpPerspective(texture, H,(n, m))
                
                M = cv2.addWeighted(imgOrig, 0.9, overlay, 0.9,0)
                
                cv2.imshow("win2",M)
            else:
                cv2.imshow("win2",imgOrig)
            cv2.waitKey(1)
def blurDn(input_frame, level):
    """
    Blur and downsampling an image.
    """
    if 1 < level:
        output_frame = blurDn(cv.pyrDown(input_frame), level - 1)
    else:
        output_frame = cv.pyrDown(input_frame)
    return output_frame
Пример #15
0
    def gen_pyr_to_path(self, dataset, batch_size=5000):
        """
            Generate the laplacian pyramids with respect to given subsampling rates and number of
        levels.
            imgs: ndarray
                List of images.
            can_fit: bool
                Whether the datasets can fit the memory or not.
        """

        data_X = dataset
        #data_Y = dataset.y

        pyr_shapes = self.get_pyr_shapes()
        data_size = data_X.shape[0]

        prev_shape = self.img_shape
        h5files, gcols = self.get_h5files(data_size, pyr_shapes)

        if self.preprocess:
            GCN = preprocessing.GlobalContrastNormalizationPyTables()
            h5files_processed, gcols_processed = self.get_h5files_processed(data_size, pyr_shapes)


        for i in xrange(data_size):
            img = data_X[i]
            for l in xrange(self.nlevels):
                img = numpy.reshape(img, newshape=pyr_shapes[l])
                if self.subsampling_rates is not None:
                    next_img = cv2.pyrDown(img, dstsize=pyr_shapes[l+1])

                else:
                    next_img = cv2.pyrDown(img)

                tmp_img = cv2.pyrUp(next_img, dstsize=pyr_shapes[l])
                print 'level', l
                if self.preprocess:
                    temp = img - tmp_img
                    temp = GCN.transform(temp)
                    (w,h) = temp.shape
                    LCN = preprocessing.LeCunLCN((w, h), channels=[0], kernel_size=7)
                    temp = LCN.transform(temp.reshape((1,w,h,1)))
                    h5files_processed[l].root.Data.X[i] = temp.reshape((numpy.prod(pyr_shapes[l])))
                    h5files_processed[l].flush()

                l_img = numpy.reshape(img-tmp_img, newshape=numpy.prod(pyr_shapes[l]))

                h5files[l].root.Data.X[i] = l_img
                img = next_img
                h5files[l].flush()

        for h5file in h5files:
            h5file.close()
        if self.preprocess:
            for h5file in h5files_processed:
                h5file.close()
Пример #16
0
def get_white_spots(img):

    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    lr = cv2.pyrDown(cv2.pyrDown(cv2.pyrDown(gray)))

    ret, thresh = cv2.threshold(lr,200,255,cv2.THRESH_BINARY)
    kernel = np.ones((3,3),np.uint8)
    opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel, iterations = 1)
    
    return get_clusters(opening)
Пример #17
0
    def calc(self):
        img_left = cv2.pyrDown(cv2.imread(path.join(FOLDER, 'aloeL.jpg')))
        img_right = cv2.pyrDown(cv2.imread(path.join(FOLDER, 'aloeR.jpg')))

        img_left = cv2.cvtColor(img_left, cv2.COLOR_BGR2RGB)
        img_right = cv2.cvtColor(img_right, cv2.COLOR_BGR2RGB)

        stereo_parameters = dict(
            SADWindowSize=5,
            numDisparities=192,
            preFilterCap=4,
            minDisparity=-24,
            uniquenessRatio=1,
            speckleWindowSize=150,
            speckleRange=2,
            disp12MaxDiff=10,
            fullDP=False,
            P1=600,
            P2=2400)

        stereo = cv2.StereoSGBM(**stereo_parameters)
        disparity = stereo.compute(img_left, img_right).astype(np.float32) / 16

        h, w = img_left.shape[:2]
        ygrid, xgrid = np.mgrid[:h, :w]
        ygrid = ygrid.astype(np.float32)
        xgrid = xgrid.astype(np.float32)

        Bf = w * 0.8
        x = (xgrid - w * 0.5)
        y = (ygrid - h * 0.5)
        d = (disparity + 1e-6)
        z = (Bf / d).ravel()
        x = (x / d).ravel()
        y = -(y / d).ravel()

        mask = (z > 0) & (z < 30)
        points = np.c_[x, y, z][mask]
        colors = img_left.reshape(-1, 3)[mask]

        poly = tvtk.PolyData()
        poly.points = points
        poly.verts = np.arange(len(points)).reshape(-1, 1)
        poly.point_data.scalars = colors.astype(np.uint8)
        mapper = tvtk.PolyDataMapper()
        mapper.set_input_data(poly)
        actor = tvtk.Actor(mapper=mapper)
        self.scene.add_actor(actor)
        self.scene.camera.position = (0, 20, -60)
        self.scene.camera.view_up = 0, 1, 0
        self.scene.render()

        self.axes.clear()
        self.axes.imshow(disparity)
        self.figure.canvas.draw()
Пример #18
0
def pre(img):
    img_shift = cv2.pyrDown(img)
    img_shift = cv2.pyrDown(img_shift)

    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (4, 4))
    img_shift = cv2.morphologyEx(img_shift, cv2.MORPH_OPEN, kernel)
    img_shift = cv2.morphologyEx(img_shift, cv2.MORPH_CLOSE, kernel)
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2, 2))
    img_shift = cv2.morphologyEx(img_shift, cv2.MORPH_OPEN, kernel)

    return img_shift
Пример #19
0
def merge_image(im, bw):
    """
    Merge the black and white image into the large color image.
    """
    bw = cv2.pyrDown(bw)
    bw = cv2.pyrDown(bw)
    newbw = cv2.merge([bw, bw, bw])
    sh = newbw.shape
    x = sh[0]
    y = sh[1]
    im[0:x, 0:y] = newbw
Пример #20
0
def Laplacian_Pyramid_Blending_with_mask(A, B, m, num_levels=6):
    # assume mask is float32 [0,1]

    # generate Gaussian pyramid for A,B and mask
    GA = A.copy()
    GB = B.copy()
    GM = m.copy()
    gpA = [GA]
    gpB = [GB]
    gpM = [GM]
    for i in xrange(num_levels):
        GA = cv2.pyrDown(GA)
        GB = cv2.pyrDown(GB)
        GM = cv2.pyrDown(GM)
        gpA.append(np.float32(GA))
        gpB.append(np.float32(GB))
        gpM.append(np.float32(GM))

    # generate Laplacian Pyramids for A,B and masks
    lpA = [gpA[num_levels - 1]
          ]  # the bottom of the Lap-pyr holds the last (smallest) Gauss level
    lpB = [gpB[num_levels - 1]]
    gpMr = [gpM[num_levels - 1]]
    for i in xrange(num_levels - 1, 0, -1):
        # Laplacian: subtarct upscaled version of lower level from current level
        # to get the high frequencies
        LA = np.subtract(gpA[i - 1],
                         cv2.pyrUp(
                             gpA[i],
                             dstsize=(gpA[i - 1].shape[1],
                                      gpA[i - 1].shape[0])))

        LB = np.subtract(gpB[i - 1],
                         cv2.pyrUp(
                             gpB[i],
                             dstsize=(gpB[i - 1].shape[1],
                                      gpB[i - 1].shape[0])))
        lpA.append(LA)
        lpB.append(LB)
        gpMr.append(gpM[i - 1])  # also reverse the masks

    # Now blend images according to mask in each level
    LS = []
    for la, lb, gm in zip(lpA, lpB, gpMr):
        ls = la * gm + lb * (1.0 - gm)
        LS.append(ls)

    # now reconstruct
    ls_ = LS[0]
    for i in xrange(1, num_levels):
        ls_ = cv2.resize(cv2.pyrUp(ls_), dsize=(LS[i].shape[1], LS[i].shape[0]))
        ls_ = cv2.add(ls_, LS[i].astype('float32'))

    return ls_
Пример #21
0
 def start(self):
     """
     This will be called right before the simulation starts
     """
     self.t_start = time.time()
     self.steps = 0
     ret, frame = self.cam.read()
     self.current_frame = cv2.resize(frame, dsize=(self.prop_dict['array'][0].shape[1], self.prop_dict['array'][0].shape[0]))
     self.current_frame1 = cv2.pyrDown(self.current_frame)
     self.current_frame2 = cv2.pyrDown(self.current_frame1)
     self.current_frame3 = cv2.resize(self.current_frame2, dsize=(self.prop_dict['array3'][0].shape[1], self.prop_dict['array3'][0].shape[0]))
Пример #22
0
def down(train,he,wi) :
    img = train[0].reshape(he,wi)
    hep,wip = cv2.pyrDown(img).shape
    output = np.zeros((trainN,hep*wip))
    for i,row in enumerate(train):
        img = row.reshape(he,wi)
        cv2.pyrDown(img).reshape(1,hep*wip)  
        output[i] = cv2.pyrDown(img).reshape(1,hep*wip)  
        
    
    return output,hep,wip
def texturemapGridSequence():

    cap = cv2.VideoCapture("GridVideos/grid1.avi")

    texture = cv2.imread("Images/ITULogo.jpg")
    texture = cv2.pyrDown(texture)

    # find texture corners (start top left follow clockwise)
    mTex, nTex, t = texture.shape
    textureCorners = [(0.0, 0.0), (float(mTex), 0.0), (float(mTex), float(nTex)), (0.0, float(nTex))]

    running, imgOrig = cap.read()

    pattern_size = (9, 6)

    idx = [0, 8, 45, 53]
    while running:

        imgOrig = cv2.pyrDown(imgOrig)
        h, w, d = imgOrig.shape
        gray = cv2.cvtColor(imgOrig, cv2.COLOR_BGR2GRAY)
        found, corners = cv2.findChessboardCorners(gray, pattern_size)
        if found:
            term = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_COUNT, 30, 0.1)
            cv2.cornerSubPix(gray, corners, (5, 5), (-1, -1), term)
            #            cv2.drawChessboardCorners(imgOrig, pattern_size, corners, found)

            #            for t in idx:
            #                cv2.circle(imgOrig,(int(corners[t,0,0]),int(corners[t,0,1])),10,(255,t,t))

            # found image chessboard corners (start top left follow clockwise)
            chessCorners = [
                (corners[0, 0, 0], corners[0, 0, 1]),
                (corners[8, 0, 0], corners[8, 0, 1]),
                (corners[45, 0, 0], corners[45, 0, 1]),
                (corners[53, 0, 0], corners[53, 0, 1]),
            ]

            # Convert to openCV format
            ip1 = np.array([[x, y] for (x, y) in textureCorners])
            ip2 = np.array([[x, y] for (x, y) in chessCorners])

            # find homography
        #            H,mask = cv2.findHomography(ip1, ip2)

        # do the same as for the simple texture (add the images weighted)
        #            overlay = cv2.warpPerspective(texture, H,(w, h))
        #            imgOrig = cv2.addWeighted(imgOrig, 0.5, overlay, 0.5,0)

        cv2.imshow("win2", imgOrig)
        cv2.waitKey(1)
        running, imgOrig = cap.read()
    return None
Пример #24
0
def buildLaplacianPyramid(I, nLevels= -1, minSize = 16, useStack = False):
    if nLevels == -1:
        nLevels = getNlevels(I,minSize)

    pyramid = nLevels*[None]
    pyramid[0] = I
    if len(pyramid[0].shape) < 3:
        pyramid[0].shape += (1,)
    # All levels have the same resolution
    if useStack:
        # Gaussian pyramid
        for i in range(nLevels-1):
            srcSz = pyramid[i].shape[0:2]
            newSz = tuple([a/2 for a in pyramid[i].shape[0:2]])
            newSz = (newSz[1],newSz[0])
            pyramid[i+1] = cv2.pyrDown(pyramid[i])
            if len(pyramid[i+1].shape) < 3:
                pyramid[i+1].shape += (1,)

        # Make a stack
        for lvl in range(0,nLevels-1):
            for i in range(nLevels-1,lvl,-1):
                newSz = pyramid[i-1].shape[0:2]
                up = cv2.pyrUp(pyramid[i],dstsize=(newSz[1],newSz[0]))
                if len(up.shape) < 3:
                    up.shape += (1,)
                pyramid[i] = np.array(up)

        lapl = nLevels*[None]
        lapl[nLevels-1] = np.copy(pyramid[nLevels-1])
        for i in range(0,nLevels-1):
            lapl[i] = pyramid[i].astype(np.float32) - pyramid[i+1].astype(np.float32)
        pyramid = lapl

    else:
        for i in range(nLevels-1):
            srcSz = pyramid[i].shape[0:2]
            newSz = tuple([a/2 for a in pyramid[i].shape[0:2]])
            newSz = (newSz[1],newSz[0])
            pyramid[i+1] = cv2.pyrDown(pyramid[i])
            if len(pyramid[i+1].shape) < 3:
                pyramid[i+1].shape += (1,)

        for i in range(nLevels-1):
            newSz = pyramid[i].shape[0:2]
            up = cv2.pyrUp(pyramid[i+1],dstsize=(newSz[1],newSz[0])).astype(np.float32)
            if len(up.shape) < 3:
                up.shape += (1,)
            pyramid[i] = pyramid[i].astype(np.float32) - up



    return pyramid
def problem_7(imgA,imgB):
    #Another solution using image pyramids
    #For Gaussain Pyramids
    diffLevels_A=imgA.copy()
    GaussPyr_A=[]
    GaussPyr_A.append(diffLevels_A)         # Gaussian Pyramids for imgA
    for itr in range(4):
        diffLevels_A=cv2.pyrDown(diffLevels_A)
        GaussPyr_A.append(diffLevels_A)
    diffLevels_B=imgB.copy()                # Gaussian Pyramids for imgB
    GaussPyr_B=[];GaussPyr_B.append(diffLevels_B)
    for itr in range(4):
        diffLevels_B=cv2.pyrDown(diffLevels_B)
        GaussPyr_B.append(diffLevels_B)

    #For Laplacian Pyramids   (Laplacian pyramids will be appended from lowest resolution to highest resolution)
    LaplacePyr_A=[GaussPyr_A[3]]   #Since we start building the Laplacian pyramids from the bottom
    for itr in range(3,0,-1):
        temp_A=cv2.pyrUp(GaussPyr_A[itr])
        d=(GaussPyr_A[itr-1].shape[0],GaussPyr_A[itr-1].shape[1],3)
        temp_A=np.resize(temp_A,d)
        LDiff=cv2.subtract(GaussPyr_A[itr-1],temp_A)      #Bcoz "GaussPyr_A[itr-1]" has a higher resolution than "GaussPyr_A[itr]"
        LaplacePyr_A.append(LDiff)

    LaplacePyr_B=[GaussPyr_B[3]]
    for itr in range(3,0,-1):
        temp_B=cv2.pyrUp(GaussPyr_B[itr])
        d=(GaussPyr_B[itr-1].shape[0],GaussPyr_B[itr-1].shape[1],3)
        temp_B=np.resize(temp_B,d)
        LDiff=cv2.subtract(GaussPyr_B[itr-1],temp_B)
        LaplacePyr_B.append(LDiff)

    #Blending the two Laplacian Pyramids (all resolution levels)
    Blend=[]
    #Note: Blend will have pyramids blended from lower to higher resolution
    for LapA,LapB in zip(LaplacePyr_A,LaplacePyr_B):
        Lr,Lc,dimension=LapA.shape
        temp=np.hstack((LapA[:,0:Lc/2],LapB[:,Lc/2:]))
        # Laplacian pyramid at each level is blended. This will help reconstruction of image
        Blend.append(temp)

    #Reconstructing the Image from the pyramids (Laplcian to Gaussian)
    final_temp=Blend[0]
    for itr in range(1,4):
        final_temp=cv2.pyrUp(final_temp)
        d=(Blend[itr].shape[0],Blend[itr].shape[1],3)
        final_temp=np.resize(final_temp,d)
        final_temp=cv2.add(final_temp,Blend[itr])       #L[i]=G[i]-G[i-1]..diff of gaussian..So, G[i]=L[i]+G[i-1]

    final_img=np.hstack((imgA[:,0:Lc/2],imgB[:,Lc/2:]))
    cv2.imshow("Final Blended Image",final_temp)
    cv2.imwrite("P_7.jpg",final_temp)
    cv2.waitKey(0)
Пример #26
0
def blend(image1, image2, mask):
    # generate Gaussian pyramid for image 1
    G = image1.astype(np.float32)
    gpA = [G]
    for i in xrange(6):
        G = cv2.pyrDown(G)
        gpA.append(G.astype(np.float32))

    # generate Gaussian pyramid for image 2
    G = image2.astype(np.float32)
    gpB = [G]
    for i in xrange(6):
        G = cv2.pyrDown(G)
        gpB.append(G.astype(np.float32))

    # generate Gaussian pyramid for mask
    G = mask.astype(np.float32)
    gpM = [G]
    for i in xrange(6):
        G = cv2.pyrDown(G)
        gpM.append(G.astype(np.float32))

    # generate Laplacian Pyramid for image 1
    lpA = [gpA[5]]
    for i in xrange(5,0,-1):
        rows,cols = gpA[i-1].shape[:2]
        GE = cv2.pyrUp(gpA[i])[:rows,:cols]
        L = cv2.subtract(gpA[i-1],GE)
        lpA.append(L)

    # generate Laplacian Pyramid for image 2
    lpB = [gpB[5]]
    for i in xrange(5,0,-1):
        rows,cols = gpB[i-1].shape[:2]
        GE = cv2.pyrUp(gpB[i])[:rows,:cols]
        L = cv2.subtract(gpB[i-1],GE)
        lpB.append(L)

    # Now add the images with mask
    LS = []
    length = len(lpA)
    for i in range(length):
        LS.append(lpB[i]*gpM[length-i-1] + lpA[i]*(1-gpM[length-i-1]))

    # now reconstruct
    ls_ = LS[0]
    for i in xrange(1,6):
        rows,cols = LS[i].shape[:2]
        ls_ = cv2.pyrUp(ls_)[:rows,:cols]
        ls_ = cv2.add(ls_, LS[i])
    ls_ = np.clip(ls_, 0, 255)
    return ls_.astype(np.uint8)
Пример #27
0
def textureOnGrid():
    texture = cv2.imread('Images/ITULogo.jpg')
    texture = cv2.pyrDown(texture)
    m,n,d = texture.shape

    fn = "GridVideos/grid1.mp4"
    sequence = cv2.VideoCapture(fn)
    running, frame = sequence.read()
    pattern_size = (9, 6)
    idx = [0,8,53,45]
    frame_no = 1
    failed = 0
    while running:
        running, frame = sequence.read()
        frame_no += 1
        if not running:
            print "FAILED: ", failed

        frame = cv2.pyrDown(frame)
        gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
        gray = sharpen(gray)
        found, corners = cv2.findChessboardCorners(gray, pattern_size)

        #Define corner points of texture
        ip1 = np.array([[0.,0.],[float(n),0.],[float(n),float(m)],[0.,float(m)]])

        if not found:
            failed += 1
            print "GEMMER LIGE"
            cv2.imwrite("failed_frame_{}.png".format(frame_no), frame)
            continue

        r = []

        c = [(0,0,0),(75,75,75),(125,125,125),(255,255,255)]
        for i,t in enumerate(idx):
            r.append([float(corners[t,0,0]),float(corners[t,0,1])])
            cv2.circle(frame,(int(corners[t,0,0]),int(corners[t,0,1])),10,c[i])

        ip2 = np.array(r)

        h_t_s,mask = cv2.findHomography(ip1, ip2)

        #texture map
        h,w,d = frame.shape
        warped_texture = cv2.warpPerspective(texture, h_t_s,(w, h))
        result = cv2.addWeighted(frame, .6, warped_texture, .4, 50)

        #display
        cv2.imshow("Texture Mapping", result)
        cv2.waitKey(1)
Пример #28
0
def laplacian_pyramid_blending(img_in1, img_in2):

    # Write laplacian pyramid blending codes here

    img_in1 = img_in1[:, :img_in1.shape[0]]
    img_in2 = img_in2[:img_in1.shape[0], :img_in1.shape[0]]

    # generate Gaussian pyramid for A
    G = img_in1.copy()
    gpA = [G]
    for i in xrange(6):
        G = cv2.pyrDown(G)
        gpA.append(G)

    # generate Gaussian pyramid for B
    G = img_in2.copy()
    gpB = [G]
    for i in xrange(6):
        G = cv2.pyrDown(G)
        gpB.append(G)

    # generate Laplacian Pyramid for A
    lpA = [gpA[5]]
    for i in xrange(5, 0, -1):
        GE = cv2.pyrUp(gpA[i])
        L = cv2.subtract(gpA[i - 1], GE)
        lpA.append(L)

    # generate Laplacian Pyramid for B
    lpB = [gpB[5]]
    for i in xrange(5, 0, -1):
        GE = cv2.pyrUp(gpB[i])
        L = cv2.subtract(gpB[i - 1], GE)
        lpB.append(L)

    # Now add left and right halves of images in each level
    LS = []
    for la, lb in zip(lpA, lpB):
        rows, cols, dpt = la.shape
        ls = np.hstack((la[:, 0:cols / 2], lb[:, cols / 2:]))
        LS.append(ls)

    # now reconstruct
    ls_ = LS[0]
    for i in xrange(1, 6):
        ls_ = cv2.pyrUp(ls_)
        ls_ = cv2.add(ls_, LS[i])

    img_out = ls_  # Blending result
    return True, img_out
def laplacian_smoothness_feature(img):
    """
    Wang15 f18
    :param img:
    :return:
    """
    img1 = even_border(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY))
    img2 = even_border(cv2.pyrDown(img1))
    img3 = cv2.pyrDown(img2)
    l2 = np.abs(img2 - cv2.pyrUp(img3))
    # cv2.namedWindow('tmp', cv2.WINDOW_NORMAL)
    # cv2.imshow('tmp', l2)
    # cv2.waitKey()
    return np.sum(l2)/(l2.shape[0]*l2.shape[1])
Пример #30
0
def detectMarker(filename):
    global click_pos

    points = [None] * 4
    cv2.namedWindow("img")
    cv2.setMouseCallback("img", click)
    img = cv2.imread(filename)
    img_big = cv2.pyrUp(img)
    img_small = cv2.pyrDown(img)
    img_small = cv2.pyrDown(img_small)
    img_small_orig = np.copy(img_small)

    big_width = 50
    big_height = 50
    while True:
        cv2.imshow("img", img_small)
        while click_pos is None:
            c = cv2.waitKey(1)
            if c == 27:
                exit()
            elif c == 13:
                return points

        x_o = 4 * click_pos[0]
        y_o = 4 * click_pos[1]
        click_pos = None

        x = np.max(2 * x_o - big_width, 0)
        y = np.max(2 * y_o - big_height, 0)

        dx = 2 * big_width
        dy = 2 * big_height

        detail = np.array(img_big[y : y + dy, x : x + dx, :])
        cv2.imshow("img2", detail)
        cv2.setMouseCallback("img2", click)
        while click_pos is None:
            c = cv2.waitKey(1)
            if c == 27:
                exit()
            elif c == 13:
                return points

        xp = (x + click_pos[0]) / 2.0 + 5
        yp = (y + click_pos[1]) / 2.0
        click_pos = None

        update_points(points, (xp, yp))
        print(points)
        img_small = draw_points(img_small_orig, points, 4)
Пример #31
0
def predict(src_root):
    """ Inference the whole src root directory """
    # src_root = "./demo/img"
    dst_root = "./result"
    label_map_path = "./labelmap/label.txt"
    if not os.path.isdir(dst_root):
        os.mkdir(dst_root)

    images = os.listdir(src_root)
    output_file = os.path.join(dst_root, "output_result.txt")
    result_file = open(output_file, "a")

    label_map_file = open(label_map_path)
    label_map = {}
    for line_number, label in enumerate(label_map_file.readlines()):
        label_map[line_number] = label[:-1]
        line_number += 1
    label_map_file.close()

    res = []
    for image in images:
        image_path = os.path.join(src_root, image)
        start = datetime.datetime.now()
        with tf.gfile.FastGFile(image_path, 'rb') as image_raw:
            image_file = image_raw.read()
            if 'jpg' or 'jpeg' in image_path:
                input_0 = decode_image(image_file)
                # print(input_0.shape)
                # print('this is a jpeg')
            elif 'png' in image_path:
                input_0 = decode_png(image_file)
                # print('this is a png')
            elif 'webp' in image_path:
                # print('this is a webp')
                input_0 = webp.load_image(image_file, 'RGBA')
            else:
                print('wrong file format')
                continue

            while input_0.shape[0] < 331 or input_0.shape[1] < 331:
                input_0 = cv2.pyrUp(input_0)
            while input_0.shape[0] >= 662 and input_0.shape[1] >= 662:
                input_0 = cv2.pyrDown(input_0)

            image_height = input_0.shape[0]
            # print(image_height)
            image_width = input_0.shape[1]
            # print(image_width)
            image_height_center = int(image_height/2)
            image_width_center = int(image_width/2)

            tl_crop = input_0[0:331, 0:331]
            tr_crop = input_0[0:331, image_width-331:image_width]
            bl_crop = input_0[image_height-331:image_height, 0:331]
            br_crop = input_0[image_height-331:image_height, image_width-331:image_width]
            center_crop = input_0[image_height_center - 165: image_height_center + 166, image_width_center - 165: image_width_center + 166]

            input_concat = np.asarray([tl_crop, tr_crop, bl_crop, br_crop, center_crop])
            # print(input_concat.shape)
            input_batch = input_concat.reshape(-1, 331, 331, 3)

        predictions = diagnose_image(inference_session, input_batch)
        # print(predictions)
        overall_result = np.argmax(np.sum(predictions, axis=0))

        # write result file
        result_file.write(image_path + "\n")
        result_file.write(str(overall_result) + "\n")

        # save img to the classified folder
        image_origin = cv2.imread(os.path.join(src_root, image))
        save_path = "save/"+str(overall_result)
        os.mkdir(save_path)
        save_file_path = os.path.join(save_path, image)
        if os.path.exists(save_file_path):
            save_file_path = os.path.join(save_path, image.split('.')[0]+"_dup"+image.split('.')[:-1])
        cv2.imwrite(os.path.join(save_path, image),image_origin)
        print("Image saved.")

        end = datetime.datetime.now()
        #print(image_path)
        print(overall_result, label_map[overall_result])
        print("Time cost: ", end - start, "\n")
        res.append([image_path,label_map[overall_result]])

    result_file.close()

    return res
Пример #32
0
import cv2
import numpy as np

large = cv2.imread('A.jpg')
rgb = cv2.pyrDown(large)
small = cv2.cvtColor(rgb, cv2.COLOR_BGR2GRAY)

kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
grad = cv2.morphologyEx(small, cv2.MORPH_GRADIENT, kernel)

_, bw = cv2.threshold(grad, 0.0, 255.0, cv2.THRESH_BINARY | cv2.THRESH_OTSU)

kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9, 1))
connected = cv2.morphologyEx(bw, cv2.MORPH_CLOSE, kernel)
# using RETR_EXTERNAL instead of RETR_CCOMP
contours, hierarchy = cv2.findContours(connected.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
#For opencv 3+ comment the previous line and uncomment the following line
#_, contours, hierarchy = cv2.findContours(connected.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

mask = np.zeros(bw.shape, dtype=np.uint8)

for idx in range(len(contours)):
    x, y, w, h = cv2.boundingRect(contours[idx])
    mask[y:y+h, x:x+w] = 0
    cv2.drawContours(mask, contours, idx, (255, 255, 255), -1)
    r = float(cv2.countNonZero(mask[y:y+h, x:x+w])) / (w * h)

    if r > 0.45 and w > 8 and h > 8:
        cv2.rectangle(rgb, (x, y), (x+w-1, y+h-1), (0, 255, 0), 2)

cv2.imshow('rects', rgb)
Пример #33
0
import cv2
import numpy as np

num_down = 2  # number of downsamples
num_bilateral = 7  # number of bilateral filtering step

img_rgb = cv2.imread("Screenshot from 2020-08-07 19-11-11.png")
print(img_rgb.shape)  # prints dimensions

img_rgb = cv2.resize(img_rgb, (800, 800))

img_color = img_rgb

for _ in range(num_down):
    img_color = cv2.pyrDown(img_color)

for _ in range(num_bilateral):
    img_color = cv2.bilateralFilter(img_color, d=9, sigmaColor=9, sigmaSpace=7)

for _ in range(num_down):
    img_color = cv2.pyrUp(img_color)

img_grey = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2GRAY)
img_blur = cv2.medianBlur(img_grey, 7)

img_edge = cv2.adaptiveThreshold(img_blur,
                                 255,
                                 cv2.ADAPTIVE_THRESH_MEAN_C,
                                 cv2.THRESH_BINARY,
                                 blockSize=9,
                                 C=2)
Пример #34
0
'''
拉普拉斯金字塔
Li = Gi - pyrUp(pyrDown(Gi))
Gi 原始图像
Li 第i层拉普拉斯金字塔图像
'''
import cv2 as cv

o = cv.imread('lena256.bmp', cv.IMREAD_GRAYSCALE)

#第0层拉普拉斯金字塔图像
od = cv.pyrDown(o)
odu = cv.pyrUp(od)
lappyr0 = o - odu

#第1层拉普拉斯金字塔图像
o1 = od
od1 = cv.pyrDown(o1)
od1u = cv.pyrUp(od1)
lappyr1 = o1 - od1u

cv.imshow('original', o)
cv.imshow('lappyr0', lappyr0)
cv.imshow('lappyr1', lappyr1)

cv.waitKey()
cv.destroyAllWindows()
Пример #35
0
import cv2
import numpy as np

A = cv2.imread('3.jpg')
B = cv2.imread('4.jpg')

G = A.copy()
gpA = [G]
for i in range(6):
    G = cv2.pyrDown(G)
    gpA.append(G)

G = B.copy()
gpB = [G]
for i in range(6):
    G = cv2.pyrDown(G)
    gpB.append(G)

# generate Laplacian Pyramid for A
lpA = [gpA[5]]
for i in range(5, 0, -1):
    GE = cv2.pyrUp(gpA[i])
    L = cv2.subtract(gpA[i - 1], GE)
    lpA.append(L)

# generate Laplacian Pyramid for B
lpB = [gpB[5]]
for i in range(5, 0, -1):
    GE = cv2.pyrUp(gpB[i])
    L = cv2.subtract(gpB[i - 1], GE)
    lpB.append(L)
Пример #36
0
import cv2
import numpy as np

# read and scale down image
image_file = "../MantecaDock/Images/dockCollapseZmeshp05NB.png"
image_file = "../MantecaRoom1/Images/room1collapseZTp25meshp1_ChrisMessedUp.png"
# read and scale down image
img = cv2.pyrDown(cv2.imread(image_file, 0))
img = cv2.imread(image_file, cv2.IMREAD_UNCHANGED)

# threshold image
ret, threshed_img = cv2.threshold(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), 127,
                                  255, cv2.THRESH_BINARY)
# find contours and get the external one
image, contours, hier = cv2.findContours(threshed_img, cv2.RETR_TREE,
                                         cv2.CHAIN_APPROX_SIMPLE)

# with each contour, draw boundingRect in green
# a minAreaRect in red and
# a minEnclosingCircle in blue
# for cnt in contours:
#     # calculate epsilon base on contour's perimeter
#     # contour's perimeter is returned by cv2.arcLength
#     epsilon = 0.01 * cv2.arcLength(cnt, True)
#     # get approx polygons
#     approx = cv2.approxPolyDP(cnt, epsilon, True)
#     # draw approx polygons
#     cv2.drawContours(img, [approx], -1, (0, 255, 0), 1)
#
#     # hull is convex shape as a polygon
#     hull = cv2.convexHull(cnt)
Пример #37
0
    def get_images(self, imgpath):
        # Pick random clone, crypt or fufi
        u01 = np.random.uniform()
        if u01 < self.cpfr_frac[0]:
            img, mask = self.all_svs_opened[imgpath].fetch_clone(
                prop_displ=0.45)
        elif u01 < np.sum(self.cpfr_frac[0:2]):
            img, mask = self.all_svs_opened[imgpath].fetch_partial(
                prop_displ=0.45)
        elif u01 < np.sum(self.cpfr_frac[0:3]):
            img, mask = self.all_svs_opened[imgpath].fetch_fufi(
                prop_displ=0.45)
        else:
            img, mask = self.all_svs_opened[imgpath].fetch_rndmtile()

        if self.dilate_masks == True:
            n_dil = int(5 / self.um_per_pixel)  # if mpp is one or less than 1
            # dilate if desired
            for i in range(mask.shape[2]):
                mask[:, :, i] = cv2.morphologyEx(mask[:, :, i].copy(),
                                                 cv2.MORPH_DILATE,
                                                 st_3,
                                                 iterations=n_dil)

        if self.aug == True:
            composition = A.Compose([
                A.HorizontalFlip(),
                A.VerticalFlip(),
                A.Rotate(border_mode=cv2.BORDER_CONSTANT),
                A.OneOf([
                    A.ElasticTransform(alpha=1000,
                                       sigma=30,
                                       alpha_affine=30,
                                       border_mode=cv2.BORDER_CONSTANT,
                                       p=1),
                    A.GridDistortion(border_mode=cv2.BORDER_CONSTANT, p=1),
                ],
                        p=0.5),
                A.CLAHE(p=0.2),
                A.HueSaturationValue(hue_shift_limit=12,
                                     sat_shift_limit=12,
                                     val_shift_limit=12,
                                     p=0.3),
                A.RandomBrightnessContrast(p=0.3),
                A.Posterize(p=0.1, num_bits=4),
                A.OneOf([
                    A.JpegCompression(p=1),
                    A.MedianBlur(p=1),
                    A.Blur(p=1),
                    A.GlassBlur(p=1, max_delta=2, sigma=0.4),
                    A.IAASharpen(p=1)
                ],
                        p=0.3)
            ],
                                    p=1)
            transformed = composition(image=img, mask=mask)
            img, mask = transformed['image'], transformed['mask']
        mask_list = [mask[:, :, ii] for ii in range(mask.shape[2])]

        if self.stride_bool:
            mask_list = [cv2.pyrDown(mask_ii.copy()) for mask_ii in mask_list]

        mask_list = [
            cv2.threshold(mask_ii, 120, 255, cv2.THRESH_BINARY)[1]
            for mask_ii in mask_list
        ]

        ## convert to floating point space, normalize and mask non-used clones
        img = img.astype(np.float32) / 255
        mask_list = [mask_ii.astype(np.float32) / 255 for mask_ii in mask_list]
        if self.normalize:
            img = (img - self.norm_mean) / self.norm_std

        return img, np.stack(mask_list, axis=2)
Пример #38
0
    if (y2 - y1) == 0:
        return 90.0  #90度认为平行于x轴
    else:
        angle = abs(math.degrees(math.atan(
            (x2 - x1) / (y2 - y1))))  #这里我是做的对y的夹角
        #print angle
        return angle


cap = cv2.VideoCapture(choose)
#设置腐蚀
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
while (True):
    start = time()
    ret, image = cap.read()
    image = cv2.pyrDown(image)
    #image = cv2.imread("318.jpg")
    #cv2.imshow("h",image)
    #转换lab空间,并且对图像进行预处理:灰度腐蚀
    lab = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)
    #cv2.imshow("L*a*b*", lab)
    #lab1 = eroded = cv2.erode(lab,kernel)
    #L,A,B = cv2.split(lab1,mv=None)

    #cv2.imwrite("C.jpeg",lab1)
    # 像素的提取,特征提取
    img = Image.fromarray(lab)
    data = []
    #img = image.open(f)
    row, col = img.size
    for i in range(row):
    def pyramidsTest(self, img):
        lower_reso = cv2.pyrDown(img)
        higher_reso2 = cv2.pyrUp(lower_reso)
        cv2.imshow("pyram", higher_reso2)

        cv2.waitKey(0)
Пример #40
0
img = cv.imread("lena.jpg")

'''
lower_res = cv.pyrDown(img)
lr_2 = cv.pyrUp(lower_res)

cv.imshow("Orig",img)
cv.imshow("lower_res",lower_res)
cv.imshow("higher_res",lr_2)
'''

layer = img.copy()
gp = [layer]

for i in range(5):
	curr = cv.pyrDown(gp[-1])
	gp.append(curr)
	#cv.imshow(str(i),curr)

layer = gp[len(gp)-1]
cv.imshow("Upper level Gauss pyramid",layer)

lp = [layer]

for i in range(5,0,-1):
	extended = cv.pyrUp(gp[i])
	laplacian = cv.subtract(gp[i-1],extended)
	cv.imshow(str(i),laplacian)

cv.waitKey(0)
cv.destroyAllWindows()
import cv2
mv=20
val=[]
cap= cv2.VideoCapture('1.mp4')
while cap.isOpened():
    ret,frame = cap.read()

    rects=[]
    full = frame.copy()

    print(frame.shape)
    frame = cv2.pyrDown(frame)
    frame = cv2.pyrDown(frame)
    frame = cv2.pyrDown(frame)
    import numpy as np
    kernel = np.ones((5,5))
    mask = cv2.inRange(frame,(170,170,170),(255,255,255))
    erode = cv2.erode(mask,kernel)
    dilate = cv2.dilate(erode,kernel)
    mask = dilate
    cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[-2]
    cntsSorted = sorted(cnts, key=lambda x: cv2.contourArea(x))
    remove = []
    for i in range(len(cntsSorted)):
        x,y,w,h = cv2.boundingRect(cntsSorted[i])
        if((x+h/2 < 0.2*frame.shape[1]) or (x+h/2 > 0.8*frame.shape[1]) or (y+w/2 < 0.2*frame.shape[0]) or (y+w/2 > 0.8*frame.shape[0])):
            remove.append(i)
    for i in range(1,len(remove)-1):
        del cntsSorted[remove[-i]]
    if(len(cntsSorted)>0 and len(remove)>0):
        del cntsSorted[remove[0]]
import cv2

image = cv2.imread("images/shiki.jpg")

smaller = cv2.pyrDown(image)
larger = cv2.pyrUp(smaller)

cv2.imshow("Original", image)
cv2.imshow("Smaller", smaller)
cv2.imshow("Larger", larger)

cv2.waitKey(0)
cv2.destroyAllWindows()
Пример #43
0
        fn = sys.argv[1]
        print('loading %s ...' % fn)
        img = cv2.imread(fn)
        if img is None:
            print('Failed to load fn:', fn)
            sys.exit(1)

    else:
        sz = 2048
        print('generating %dx%d procedural image ...' % (sz, sz))
        img = np.zeros((sz, sz), np.uint8)
        track = np.cumsum(np.random.rand(500000, 2) - 0.5, axis=0)
        track = np.int32(track * 10 + (sz / 2, sz / 2))
        cv2.polylines(img, [track], 0, 255, 1, cv2.CV_AA)

    small = img
    for i in xrange(3):
        small = cv2.pyrDown(small)

    def onmouse(event, x, y, flags, param):
        h, w = img.shape[:2]
        h1, w1 = small.shape[:2]
        x, y = 1.0 * x * h / h1, 1.0 * y * h / h1
        zoom = cv2.getRectSubPix(img, (1200, 900), (x + 0.5, y + 0.5))
        cv2.imshow('zoom', zoom)

    cv2.imshow('original', small)
    cv2.setMouseCallback('original', onmouse)
    cv2.waitKey()
cv2.destroyAllWindows()
Пример #44
0
import cv2
import numpy as np

# read and scale down image
# wget https://bigsnarf.files.wordpress.com/2017/05/hammer.png
img = cv2.pyrDown(cv2.imread('emptyBoard.jpg', cv2.IMREAD_UNCHANGED))

# threshold image
ret, threshed_img = cv2.threshold(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), 127,
                                  255, cv2.THRESH_BINARY)
# find contours and get the external one
image, contours, hier = cv2.findContours(threshed_img, cv2.RETR_TREE,
                                         cv2.CHAIN_APPROX_SIMPLE)

# with each contour, draw boundingRect in green
# a minAreaRect in red and
# a minEnclosingCircle in blue
for c in contours:
    # get the bounding rect
    x, y, w, h = cv2.boundingRect(c)
    # draw a green rectangle to visualize the bounding rect
    #cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)

    # get the min area rect
    rect = cv2.minAreaRect(c)
    box = cv2.boxPoints(rect)
    # convert all coordinates floating point values to int
    box = np.int0(box)
    # draw a red 'nghien' rectangle
    #cv2.drawContours(img, [box], 0, (0, 0, 255))
image1 = cv2.imread('daj.jpg')
# Convert the training image to RGB
training_image = cv2.cvtColor(image1, cv2.COLOR_BGR2RGB)

# plt.figure(figsize=(18,18))
plt.imshow(training_image)
plt.show()

# Convert the training image to gray scale
training_gray = cv2.cvtColor(training_image, cv2.COLOR_RGB2GRAY)

plt.imshow(training_gray)
plt.show()

# Create test image by adding Scale Invariance and Rotational Invariance
test_image = cv2.pyrDown(training_image)
test_image = cv2.pyrDown(test_image)
num_rows, num_cols = test_image.shape[:2]

rotation_matrix = cv2.getRotationMatrix2D((num_cols / 2, num_rows / 2), 30, 1)
test_image = cv2.warpAffine(test_image, rotation_matrix, (num_cols, num_rows))

test_gray = cv2.cvtColor(test_image, cv2.COLOR_RGB2GRAY)

# Display traning image and testing image
fx, plots = plt.subplots(1, 2, figsize=(20, 10))

plots[0].set_title("Training Image")
plots[0].imshow(training_image)

plots[1].set_title("Testing Image")
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 30 22:19:37 2017

@author: Mohnish_Devadiga
"""

import numpy as np
import cv2

# Let's take a look at our digits dataset
image = cv2.imread('digits.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
small = cv2.pyrDown(image)
cv2.imshow('Digits Image', small)
cv2.waitKey(0)
cv2.destroyAllWindows()

# Split the image to 5000 cells, each 20x20 size
# This gives us a 4-dim array: 50 x 100 x 20 x 20
cells = [np.hsplit(row, 100) for row in np.vsplit(gray, 50)]

# Convert the List data type to Numpy Array of shape (50,100,20,20)
x = np.array(cells)
print("The shape of our cells array: " + str(x.shape))

# Split the full data set into two segments
# One will be used fro Training the model, the other as a test data set
train = x[:, :70].reshape(-1, 400).astype(np.float32)  # Size = (3500,400)
test = x[:, 70:100].reshape(-1, 400).astype(np.float32)  # Size = (1500,400)
Пример #47
0
# https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/py_pyramids/py_pyramids.html#py-pyramids

import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('selfie.jpg', 0)
lower_reso = img
for i in range(4):
    lower_reso = cv2.pyrDown(lower_reso)
    plt.subplot(1, 4, i + 1), plt.imshow(lower_reso, cmap='gray')
    plt.title("pyramid level " + str(i + 1) + " "), plt.xticks([]), plt.yticks(
        [])
plt.show()

# can be used for image blendign (looks a bit complex)
Пример #48
0
orange = cv2.imread('orange.jpg')
print(apple.shape)
print(orange.shape)

# apple_orange = np.hstack((apple[:,:256],orange[:,256:]))

# cv2.imshow('apple',apple)
# cv2.imshow('orange',orange)
# cv2.imshow("Apple and Orange",apple_orange)

# Generate Gaussian pyramids for apple
apple_copy = apple.copy()
gp_apple = [apple_copy]

for i in range(6):
    apple_copy = cv2.pyrDown(apple_copy)
    gp_apple.append(apple_copy)

# Generate Laplacian Pyramid for apple
apple_copy = gp_apple[5]
lp_apple = [apple_copy]

for i in range(5, 0, -1):
    gaussian_expanded = cv2.pyrUp(gp_apple[i])
    laplacian = cv2.subtract(gp_apple[i - 1], gaussian_expanded)
    lp_apple.append(laplacian)

# Generate Gaussian pyramids for orange
orange_copy = orange.copy()
gp_orange = [orange_copy]
Пример #49
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 25 12:05:10 2018

@author: osboxes
"""

import cv2

img = cv2.imread('images/input/messi5.jpg')
lower_reso = cv2.pyrDown(higher_reso)
higher_reso2 = cv2.pyrUp(lower_reso)
Пример #50
0
    print img1.shape
    r, c, ch = img1.shape
    clr1 = cv2.pyrDown(cv2.imread('./images/stacked1.png', 0))
    clr2 = cv2.pyrDown(cv2.imread('./images/stacked2.png', 0))
    for r, pt1, pt2 in zip(lines, pts1, pts2):
        color = tuple(np.random.randint(0, 255, 3).tolist())
        x0, y0 = map(int, [0, -r[2] / r[1]])
        x1, y1 = map(int, [c, -(r[2] + r[0] * c) / r[1]])
        clr1 = cv2.line(clr1, (x0, y0), (x1, y1), color, 1)
        clr1 = cv2.circle(clr1, tuple(pt1), 5, color, -1)
        clr2 = cv2.circle(clr2, tuple(pt2), 5, color, -1)
        return clr1, clr2


img1 = to_uint8(
    cv2.pyrDown(cv2.imread('./images/stacked1.png',
                           cv2.COLOR_BGR2GRAY)))  #queryimage # left image
img2 = to_uint8(
    cv2.pyrDown(cv2.imread('./images/stacked2.png',
                           cv2.COLOR_BGR2GRAY)))  #trainimage # right image

sift = cv2.xfeatures2d.SIFT_create()

# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(img1, None)
kp2, des2 = sift.detectAndCompute(img2, None)

# FLANN parameters
FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
search_params = dict(checks=50)
Пример #51
0
    for i in range(0, n):
        area += (X[j] + X[i]) * (Y[j] - Y[i])
        j = i
    return int(abs(area / 2.0))


AREA_THRESHOLD_MIN = 0.05
AREA_THRESHOLD_MAX = 1.00
IMG_DIRECTORY = "drive-download/"
DESTINATION = "z/"
img_to_boxes = dict()
box_sizes = list()
error_log = list()
for file in os.listdir(IMG_DIRECTORY):
    try:
        img = cv2.pyrDown(cv2.imread(IMG_DIRECTORY + file, cv2.IMREAD_UNCHANGED))
        height, width = img.shape[0], img.shape[1]
        img_area = height * width

        ret, threshed_img = cv2.threshold(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), 127, 255, cv2.THRESH_BINARY)
        contours, hier = cv2.findContours(threshed_img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

        bounding_boxes = list()
        for c in contours:
            vertices = np.int0(cv2.boxPoints(cv2.minAreaRect(c)))
            transposed = np.transpose(vertices)
            area = poly_area(transposed[0], transposed[1], len(vertices))
            if AREA_THRESHOLD_MIN < (area / img_area) < AREA_THRESHOLD_MAX:
                bounding_boxes.append(vertices)
                box_sizes.append(area / img_area)
            # cv2.drawContours(img, [vertices], 0, (0, 0, 255))
Пример #52
0
import cv2

img = cv2.imread(r"1.jpg")
img_down = cv2.pyrDown(img)
img_up = cv2.pyrUp(img_down)
img_new = cv2.subtract(img, img_up)
#为了更容易看清楚,做了个提高对比度的操作
img_new = cv2.convertScaleAbs(img_new, alpha=10, beta=0)
cv2.imshow("img_LP", img_new)
cv2.waitKey(0)
Пример #53
0
        imgs = torch.tensor(imgs)
        rams = torch.tensor(rams, dtype=torch.float)
        return imgs, rams


if __name__ == "__main__":
    img = cv2.imread('testimg.jpg')
    print(img)
    print(img.shape)

    im = np.array(Image.fromarray(img).convert('L'))
    print(im)
    print(im.shape)
    cv2.imwrite("a.jpg", im)

    i = cv2.pyrDown(im)
    print(i)
    print(i.shape) # (105*80)
    cv2.imwrite("b.jpg", i)
    h, w = i.shape
    r = i[0:85, 0:w]
    print(r)
    cv2.imwrite("r.jpg", r) #(85*80)
    net = Net()
    print(net)


    img = Image.open('testimg.jpg').convert('L')
    i = T.Resize([110, 84])(img)
    i = i.crop((0, 0, 84, 84))
    i.save("t.jpg")
Пример #54
0
import cv2
import numpy as np

img = cv2.imread('/home/oops/Desktop/OpenCV/images/input.jpg')

smaller = cv2.pyrDown(img)
larger = cv2.pyrUp(smaller)

cv2.imshow('original', img)
cv2.imshow('smaller', smaller)
cv2.imshow('larger', larger)

cv2.waitKey()
cv2.destroyAllWindows()
Пример #55
0
def predict_image(input_path, output_path, predict_func):
    img = cv2.imread(input_path, cv2.IMREAD_GRAYSCALE)

    if cfg.wld:
        img = wld(img)

    diff_list = []
    recover_list = []
    for scale in cfg.inf_scales:
        img_scale = np.copy(img)
        if scale >= 2:
            img_scale = cv2.pyrDown(img_scale)
        if scale >= 4:
            img_scale = cv2.pyrDown(img_scale)

        h, w = img_scale.shape[:2]
        diff = np.zeros((h, w))
        recover = np.zeros((h, w))

        cur_h = 0
        while cur_h < h - cfg.overlap:
            end_h = cur_h + cfg.img_size - cfg.overlap
            start_h = cur_h - cfg.overlap

            if start_h < 0:
                end_h = end_h - start_h
                start_h = 0

            if end_h > h:
                start_h = start_h - (end_h - h)
                end_h = h

            cur_w = 0
            while cur_w < w - cfg.overlap:
                end_w = cur_w + cfg.img_size - cfg.overlap
                start_w = cur_w - cfg.overlap

                if start_w < 0:
                    end_w = end_w - start_w
                    start_w = 0

                if end_w > w:
                    start_w = start_w - (end_w - w)
                    end_w = w

                sub_img = img_scale[start_h:end_h, start_w:end_w]


                sub_img = np.expand_dims(np.expand_dims(sub_img, axis=-1), axis=0)
        
                predictions = predict_func(sub_img)
                sub_img_input = predictions[0][0,:,:,0]
                sub_img_pred = predictions[1][0,:,:,0]
                sub_img_diff = np.abs(sub_img_input - sub_img_pred)

                recover[start_h+cfg.overlap:end_h-cfg.overlap,start_w+cfg.overlap:end_w-cfg.overlap] = \
                    sub_img_pred[cfg.overlap:-cfg.overlap,cfg.overlap:-cfg.overlap]
                diff[start_h+cfg.overlap:end_h-cfg.overlap,start_w+cfg.overlap:end_w-cfg.overlap] = \
                    sub_img_diff[cfg.overlap:-cfg.overlap,cfg.overlap:-cfg.overlap]

                cur_w = end_w - cfg.overlap

            cur_h = end_h - cfg.overlap

        if scale >= 2:
            diff = cv2.pyrUp(diff)
            recover = cv2.pyrUp(recover)
        if scale >= 4:
            diff = cv2.pyrUp(diff)
            recover = cv2.pyrUp(recover)

        diff_list.append(diff)
        recover_list.append(recover)

        misc.imsave('diff_%d.jpg' % scale, diff)
        misc.imsave('recover_%d.jpg' % scale, recover)


    f = open('th_info.pkl', 'rb')
    th_info = pickle.load(f)

    output_list = []
    for scale_idx, scale in enumerate(cfg.inf_scales):
        info = th_info[scale]
        diff = diff_list[scale_idx]
        th = info['mean'] + 2 * info['std']
        layer_output = (diff > th).astype(np.int)
        output_list.append(layer_output)
        
    output_1 = output_list[0] & output_list[1]
    output_2 = output_list[1] & output_list[2]
    output = output_1 | output_2

    misc.imsave(output_path, output)

    '''
Пример #56
0
import cv2
img = cv2.imread('messi5.jpg')
lower = cv2.pyrDown(img)
print(lower.shape)
cv2.imshow('dst', img)
cv2.destroyAllWindows()
Пример #57
0
        bottom_right = (top_left[0] + w, top_left[1] + h)
        #display in original image
        cv.rectangle(original, top_left, bottom_right, (0, 0, 255), 2)
        cv.imshow(meth, original)


def subtractAndSaturate(targetM, m1, m2):
    sub_output = np.int16(targetM) - np.int16(m1) - np.int16(m2)
    sub_output[sub_output < 0] = 0
    sub_output[sub_output > 0] = 255
    sub_output = np.uint8(sub_output)
    return sub_output


#MAIN
img = cv.pyrDown(
    cv.imread('/home/silje/Pictures/man-mountain.jpg', cv.IMREAD_UNCHANGED))

blue, green, red = bgrMatrices(img)

red_only = subtractAndSaturate(red, green, blue)

#FINDING CONTOURS
ret, threshed_img = cv.threshold(cv.cvtColor(img, cv.COLOR_BGR2GRAY), 10, 255,
                                 cv.THRESH_BINARY_INV)
contours_th, hier = cv.findContours(threshed_img, cv.RETR_TREE,
                                    cv.CHAIN_APPROX_SIMPLE)
contours_red, hier = cv.findContours(red_only, cv.RETR_TREE,
                                     cv.CHAIN_APPROX_SIMPLE)

#finding ROI
temp = findAndReturnROI(contours_th, threshed_img)
    def _applyEulerianVideoMagnification(self):

        timestamp = timeit.default_timer()

        if self._useGrayOverlay:
            smallImage = cv2.cvtColor(self._image,
                                      cv2.COLOR_BGR2GRAY).astype(numpy.float32)
        else:
            smallImage = self._image.astype(numpy.float32)

        # Downsample the image using a pyramid technique.
        i = 0
        while i < self._numPyramidLevels:
            smallImage = cv2.pyrDown(smallImage)
            i += 1
        if self._useLaplacianPyramid:
            smallImage[:] -= \
                cv2.pyrUp(cv2.pyrDown(smallImage))

        historyLength = len(self._historyTimestamps)

        if historyLength < self._maxHistoryLength - 1:

            # Append the new image and timestamp to the
            # history.
            self._history[historyLength] = smallImage
            self._historyTimestamps.append(timestamp)

            # The history is still not full, so wait.
            return

        if historyLength == self._maxHistoryLength - 1:
            # Append the new image and timestamp to the
            # history.
            self._history[historyLength] = smallImage
            self._historyTimestamps.append(timestamp)
        else:
            # Drop the oldest image and timestamp from the
            # history and append the new ones.
            self._history[:-1] = self._history[1:]
            self._historyTimestamps.popleft()
            self._history[-1] = smallImage
            self._historyTimestamps.append(timestamp)

        # The history is full, so process it.

        # Find the average length of time per frame.
        startTime = self._historyTimestamps[0]
        endTime = self._historyTimestamps[-1]
        timeElapsed = endTime - startTime
        timePerFrame = \
                timeElapsed / self._maxHistoryLength
        fps = 1.0 / timePerFrame
        wx.CallAfter(self._fpsStaticText.SetLabel, 'FPS:  %.1f' % fps)

        # Apply the temporal bandpass filter.
        fftResult = fft(self._history, axis=0, threads=self._numFFTThreads)
        frequencies = fftfreq(self._maxHistoryLength, d=timePerFrame)
        lowBound = (numpy.abs(frequencies - self._minHz)).argmin()
        highBound = (numpy.abs(frequencies - self._maxHz)).argmin()
        fftResult[:lowBound] = 0j
        fftResult[highBound:-highBound] = 0j
        fftResult[-lowBound:] = 0j
        ifftResult = ifft(fftResult, axis=0, threads=self._numIFFTThreads)

        # Amplify the result and overlay it on the
        # original image.
        overlay = numpy.real(ifftResult[-1]) * \
                          self._amplification
        i = 0
        while i < self._numPyramidLevels:
            overlay = cv2.pyrUp(overlay)
            i += 1
        if self._useGrayOverlay:
            overlay = cv2.cvtColor(overlay, cv2.COLOR_GRAY2BGR)
        cv2.add(self._image, overlay, self._image, dtype=cv2.CV_8U)
#higher_reso2 = cv2.pyrUp(lower_reso)#结果会比原图模糊很多
#cv2.imshow('higher_reso2', higher_reso2)

#拉普拉斯金字塔图像看起来像边界图,其中很多像素都是0,经常被用在图像压缩中
#图像金字塔的一个应用是图像融合,图像缝合中需要将两幅图叠在一起,但是由于连接区域图像像素的不连续性,使得效果开起来很差
# 图像金字塔可以实现无缝连接
# 1.读入两幅图像
img_a = cv2.imread('0001.jpg')
img_b = cv2.imread('0002.jpg')
# 2.构建两幅图像的高斯金字塔
temp_a = img_a.copy()
gp_a = [temp_a]  #若原图是1024*1024
temp_b = img_b.copy()
gp_b = [temp_b]
for index in range(6):
    temp_a = cv2.pyrDown(temp_a)
    gp_a.append(temp_a)
    temp_b = cv2.pyrDown(temp_b)
    gp_b.append(temp_b)
#1024*1024, 512*512, 256*256, 128*128, 64*64, 32*32, 16*16
# 3.根据高斯金字塔计算拉普拉斯金字塔
lp_a = [gp_a[5]]
lp_b = [gp_b[5]]  #32*32
for index in range(5, 0, -1):
    temp = cv2.pyrUp(gp_a[index])  #index=5[64*64]要和index=4的做subtract
    diff = cv2.subtract(gp_a[index - 1], temp)  #图像gp_a[index-1]与temp相减
    lp_a.append(diff)  #32*32, 64*64, 128*128, 256*256, 512*512, 1024*1024
    temp = cv2.pyrUp(gp_b[index])
    diff = cv2.subtract(gp_b[index - 1], temp)
    lp_b.append(diff)  #32*32, 64*64, 128*128, 256*256, 512*512, 1024*1024
# 4.拉普拉斯的每一层进行图像融合
Пример #60
0
def predict_tfrecord(filename):
    '''
        read label file
    '''
    label_map_path = "./labelmap/label.txt"
    label_map_file = open(label_map_path)
    label_map = {}
    for line_number, label in enumerate(label_map_file.readlines()):
        label_map[line_number] = label[:-1]
        line_number += 1
    label_map_file.close()
    
    # read tfrecord file
    num_files=get_num_of_files_in_tfrecord(filename)
    filename_queue = tf.train.string_input_producer([filename])

    reader = tf.TFRecordReader()
    _, serialized_example = reader.read(filename_queue)
    features = tf.parse_single_example(serialized_example,features = {
        'image/encoded': tf.FixedLenFeature([], tf.string),
        'image/class/label': tf.FixedLenFeature([], tf.string),
        'image/filepath': tf.FixedLenFeature([], tf.string),})

    image_data=tf.cast(features['image/encoded'], tf.string)
    image_data=tf.image.decode_jpeg(image_data)

    label = tf.cast(features['image/class/label'], tf.string)

    filepath = tf.cast(features['image/filepath'], tf.string)


    with tf.Session() as sess:
        init_op = tf.initialize_all_variables()
        sess.run(init_op)
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(coord=coord)
        image_data_list=[]
        # label_list=[]
        # filepath_list=[]
        res = []
        for i in range(num_files):
            start = datetime.datetime.now()
            [input_0, _label,_filepath] = sess.run([image_data, label,filepath])
            # _label=str(_label,encoding='utf-8')
            _filepath=str(_filepath,encoding='utf-8')
            
            while input_0.shape[0] < 331 or input_0.shape[1] < 331:
                input_0 = cv2.pyrUp(input_0)
            while input_0.shape[0] >= 662 and input_0.shape[1] >= 662:
                input_0 = cv2.pyrDown(input_0)

            image_height = input_0.shape[0]
            print(image_height)
            image_width = input_0.shape[1]
            print(image_width)
            image_height_center = int(image_height/2)
            image_width_center = int(image_width/2)

            tl_crop = input_0[0:331, 0:331]
            tr_crop = input_0[0:331, image_width-331:image_width]
            bl_crop = input_0[image_height-331:image_height, 0:331]
            br_crop = input_0[image_height-331:image_height, image_width-331:image_width]
            center_crop = input_0[image_height_center - 165: image_height_center + 166, image_width_center - 165: image_width_center + 166]

            input_concat = np.asarray([tl_crop, tr_crop, bl_crop, br_crop, center_crop])
            # print(input_concat.shape)
            input_batch = input_concat.reshape(-1, 331, 331, 3)

            predictions = diagnose_image(inference_session, input_batch)
            # print(predictions)
            overall_result = np.argmax(np.sum(predictions, axis=0))

            # save img to the classified folder
            '''
            image_origin = cv2.imread(os.path.join(src_root, image))
            save_path = "save/"+str(overall_result)
            os.mkdir(save_path)
            save_file_path = os.path.join(save_path, image)
            if os.path.exists(save_file_path):
                save_file_path = os.path.join(save_path, image.split('.')[0]+"_dup"+image.split('.')[:-1])
            cv2.imwrite(os.path.join(save_path, image),image_origin)
            print("Image saved.")
            '''
            end = datetime.datetime.now()
            #print(image_path)
            print(overall_result, label_map[overall_result])
            print("Time cost: ", end - start, "\n")
            res.append([_filepath,label_map[overall_result]])

    return res