def align_img(image_name, loss_fn=sum_of_squared_diff, max_disp = 15, big=False, name=None, hist_eq = False):
    b,g,r = get_bgr(image_name)

    if hist_eq == True:
        b, g, r = exposure.equalize_hist(b), exposure.equalize_hist(g), exposure.equalize_hist(r)

    print("Aligning green and blue: ")
    ag = align(g, b, loss_fn=loss_fn, max_disp = max_disp, big=big, name=name)
    print("Aligning blue and red: ")
    ar = align(r, b, loss_fn=loss_fn, max_disp = max_disp, big=big, name=name)
    # create a color image
    im_out = np.dstack([ar, ag, b])
    plt.show()

    # save the image
    iname = image_name.split('.')
    iname[-1] = 'jpg'
    image_name = '.'.join(iname)
    fname = 'out_' + image_name
    skio.imsave(fname, im_out)

    # display the image
    skio.imshow(im_out)
    plt.show()
    skio.show()
    def save_segmented_image(self, filepath_image, modality='t1c', show=False):
        '''
        Creates an image of original brain with segmentation overlay and save it in ./predictions
        INPUT   (1) str 'filepath_image': filepath to test image for segmentation, including file extension
                (2) str 'modality': imaging modality to use as background. defaults to t1c. options: (flair, t1, t1c, t2)
                (3) bool 'show': If true, shows output image. defaults to False.
        OUTPUT  (1) if show is True, shows image of segmentation results
                (2) if show is false, returns segmented image.
        '''
        modes = {'flair': 0, 't1': 1, 't1c': 2, 't2': 3}

        segmentation = self.predict_image(filepath_image, show=False)
        print 'segmentation = ' + str(segmentation)
        img_mask = np.pad(segmentation, (16, 16), mode='edge')
        ones = np.argwhere(img_mask == 1)
        twos = np.argwhere(img_mask == 2)
        threes = np.argwhere(img_mask == 3)
        fours = np.argwhere(img_mask == 4)

        test_im = io.imread(filepath_image)
        test_back = test_im.reshape(5, 216, 160)[modes[modality]]
        # overlay = mark_boundaries(test_back, img_mask)
        gray_img = img_as_float(test_back)

        # adjust gamma of image
        image = adjust_gamma(color.gray2rgb(gray_img), 0.65)
        sliced_image = image.copy()
        red_multiplier = [1, 0.2, 0.2]
        yellow_multiplier = [1, 1, 0.25]
        green_multiplier = [0.35, 0.75, 0.25]
        blue_multiplier = [0, 0.25, 0.9]

        print str(len(ones))
        print str(len(twos))
        print str(len(threes))
        print str(len(fours))

        # change colors of segmented classes
        for i in xrange(len(ones)):
            sliced_image[ones[i][0]][ones[i][1]] = red_multiplier
        for i in xrange(len(twos)):
            sliced_image[twos[i][0]][twos[i][1]] = green_multiplier
        for i in xrange(len(threes)):
            sliced_image[threes[i][0]][threes[i][1]] = blue_multiplier
        for i in xrange(len(fours)):
            sliced_image[fours[i][0]][fours[i][1]] = yellow_multiplier
        #if show=True show the prediction
        if show:
            print 'Showing...'
            io.imshow(sliced_image)
            plt.show()
        #save the prediction
        print 'Saving...'
        try:
            mkdir_p('./predictions/')
            io.imsave('./predictions/' + os.path.basename(filepath_image) + '.png', sliced_image)
            print 'prediction saved.'
        except:
            io.imsave('./predictions/' + os.path.basename(filepath_image) + '.png', sliced_image)
            print 'prediction saved.'
示例#3
0
def hsi_equalize_hist():
    image=data.astronaut()
    h=color.rgb2hsv(image)
    h[:,:,2]=exposure.equalize_hist(h[:,:,2])
    image_equal=color.hsv2rgb(h)
    io.imshow(image_equal)
    io.imsave('astronautequal.png',image_equal)
示例#4
0
def PreprocessImage(path, show_img=False):
    # load image
    img = io.imread(path)
    print("Original Image Shape: ", img.shape)
    # we crop image from center
    short_egde = min(img.shape[:2])
    yy = int((img.shape[0] - short_egde) / 2)
    xx = int((img.shape[1] - short_egde) / 2)
    crop_img = img[yy : yy + short_egde, xx : xx + short_egde]
    # resize to 224, 224
    resized_img = transform.resize(crop_img, (224, 224))
    if show_img:
        io.imshow(resized_img)
    # convert to numpy.ndarray
    sample = np.asarray(resized_img) * 256
    # swap channel from RGB to BGR
    sample = sample[:, :, [2,1,0]]
    # swap axes to make image from (224, 224, 4) to (3, 224, 224)
    sample = np.swapaxes(sample, 0, 2)
    sample = np.swapaxes(sample, 1, 2)

    # sub mean
    normed_img = sample - mean_img
    normed_img.resize(1, 3, 224, 224)
    return normed_img
def threshold_image(image, threshold=0):
	"""
	This function takes out any values in an image's RGB matrix that are
	below the threshold value.

	Inputs:
	- image: a matrix describing an image with only one channel represented.
	- threshold: a value, between 0 and 1, for which if an image matrix's
				 value is below, will be set to 0, and if above, will be 
				 set to 1.

				 If the threshold is set to 0, then an Otsu thresholding will
				 be returned.

	Outputs:
	- thresholded_image: a matrix representation of the thresholded image.
						 this is essentially a black and white image.
	- thresh: the threshold value

	To screen: the black-and-white image representation.
	- 
	"""
	if threshold == 0:
		thresh = threshold_otsu(image)

	if threshold != 0:
		thresh = threshold

	thresholded_image = closing(image > thresh, square(3), out=None)
	imshow(thresholded_image)

	return thresholded_image, thresh
示例#6
0
文件: na3.py 项目: ja999/sem5
def main():
  imgs = MultiImage(data_dir + '/multipage.tif')

  for a, i in zip(range(0, 4), [1, 9, 7, 8]):
    fig = plt.figure()
    ax = fig.add_axes([-0.1, -0.1, 1.2, 1.2])
    # ax.set_axis_off()
    im = data.imread('samolot0' + str(i) + '.jpg', as_grey = True)
    im = invert(im)
    im = process(im)
    out = np.ones_like(im)
    io.imshow(out)
    contours = measure.find_contours(im, 0.9)
    for n, contour in enumerate(contours):
      plt.plot(contour[:, 1], contour[:, 0], linewidth=2, color = 'white')
    plt.savefig(str(a) + '.jpg', bbox_inches = 0, frameon = False)

  fig = plt.figure()
  grid = AxesGrid(fig, rect = (1, 1, 1), nrows_ncols = (2, 2), axes_pad = 0.1)

  for i in range(0, 4):
    frame = data.imread(str(i) + '.jpg')
    grid[i].imshow(frame)
    grid[i].set_xticks([])
    grid[i].set_yticks([])

  plt.savefig('na3.jpg')
    def transform(self, func, params, sub_dir=None, img_ind=None):
        """
        Takes a function and apply to every img_arr in self.img_arr.
        Have to option to transform one as  a test case

        :param sub_dir: The index for the image
        :param img_ind: The index of the category of images
        """
        # Apply to one test case
        if sub_dir is not None and img_ind is not None:
            sub_dir_ind = self.label_map[sub_dir]
            img_arr = self.img_lst2[sub_dir_ind][img_ind]
            img_arr = func(img_arr, **params).astype(float)
            io.imshow(img_arr)
            plt.show()
        # Apply the function and parameters to all the images
        elif isinstance(sub_dir, list):
            if len(sub_dir) == 1:
                sub_dir_ind = self.label_map[sub_dir[0]]
                new_img_lst2 = []
                for img_arr in self.img_lst2[sub_dir_ind]:
                    new_img_lst2.append(func(img_arr, **params).astype(float))
                self.img_lst2[sub_dir_ind] = new_img_lst2
            else:
                for dir in sub_dir:
                    sub_dir_ind = self.label_map[dir]
                    new_img_lst2 = []
                    for img_arr in self.img_lst2[sub_dir_ind]:
                        new_img_lst2.append(func(img_arr, **params).astype(float))
                    self.img_lst2[sub_dir_ind] = new_img_lst2
        else:
            new_img_lst2 = []
            for img_lst in self.img_lst2:
                new_img_lst2.append([func(img_arr, **params).astype(float) for img_arr in img_lst])
            self.img_lst2 = new_img_lst2
def detectOpticDisc(image):
    kernel = octagon(10, 10)
    thresh = threshold_otsu(image[:,:,1])
    binary = image > thresh
    print binary.dtype
    luminance = convertToHLS(image)[:,:,2]
    t = threshold_otsu(luminance)
    t = erosion(luminance, kernel)
    
    
    labels = segmentation.slic(image[:,:,1], n_segments = 3)
    out = color.label2rgb(labels, image[:,:,1], kind='avg')
    skio.imshow(out)
    
    x, y = computeCentroid(t)
    print x, y
    rows, cols, _ = image.shape
    p1 = closing(image[:,:,1],kernel)
    p2 = opening(p1, kernel)
    p3 = reconstruction(p2, p1, 'dilation')
    p3 = p3.astype(np.uint8)
    #g = dilation(p3, kernel)-erosion(p3, kernel)
    #g = rank.gradient(p3, disk(5))
    g = cv2.morphologyEx(p3, cv2.MORPH_GRADIENT, kernel)
    #markers = rank.gradient(p3, disk(5)) < 10
    markers = drawCircle(rows, cols, x, y, 85)
    #markers = ndimage.label(markers)[0]
    #skio.imshow(markers)
    g = g.astype(np.uint8)
    #g = cv2.cvtColor(g, cv2.COLOR_GRAY2RGB)
    w = watershed(g, markers)
    print np.max(w), np.min(w)
    w = w.astype(np.uint8)
    #skio.imshow(w)
    return w
def doit(filename):
    dat = io.imread(filename)
    dat = rgb2gray(dat)

    blobs = blob_dog(dat, max_sigma=12, threshold=.25)
    blobs[:, 2] = blobs[:, 2] * (2 ** 0.5)

    io.imshow(dat)
    for blob in blobs:
        y, x, r = blob
        c = plt.Circle((x, y), r, color='r', linewidth=2, fill=False)
        plt.gca().add_patch(c)
    plt.show()

    print (blobs[:,2].size)

    r = (blobs[:,2])
    print (r)
    print (area_of_circle(r))
    avg_area = np.mean(area_of_circle(r))
    print (avg_area)
    
    #Distance from average x 
    avg_x = np.mean(blobs[:,0])
    x = (blobs[:,0])
    x_dist = (x-avg_x)
    print (x_dist)
    
    #Distance from average y
    avg_y = np.mean(blobs[:,1])
    y = (blobs[:,1])
    y_dist = (y-avg_y)
    print (y_dist)
    
    return blobs # for potential further processing
def draw_window(frame):
    # setup initial location of window
    r,h,c,w = 250,90,400,125  # simply hardcoded the values
    track_window = (c,r,w,h)    

    # set up the ROI for tracking
    roi = frame[r:r+h, c:c+w]
    hsv_roi =  cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
    mask = cv2.inRange(hsv_roi, np.array((0., 60.,32.)), np.array((180.,255.,255.)))
    roi_hist = cv2.calcHist([hsv_roi],[0],mask,[180],[0,180])
    cv2.normalize(roi_hist,roi_hist,0,255,cv2.NORM_MINMAX)    

    # Setup the termination criteria, either 10 iteration or move by atleast 1 pt
    term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 )

    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    dst = cv2.calcBackProject([hsv],[0],roi_hist,[0,180],1)

    # apply meanshift to get the new location
    ret, track_window = cv2.CamShift(dst, track_window, term_crit)
    # Draw it on image
    pts = cv2.boxPoints(ret)
    pts = np.int0(pts)
    img2 = cv2.polylines(frame,[pts],True, 255,2)
    io.imshow(img2)
示例#11
0
文件: main.py 项目: kctess5/sad_robot
def main():
	args = vars(parser.parse_args())
	filename = os.path.join(os.getcwd(), args["image"][0])

	image = skimage.img_as_uint(color.rgb2gray(io.imread(filename)))

	subsample = 1

	if (not args["subsample"] == 1):
		subsample = args["subsample"][0]

		image = transform.downscale_local_mean(image, (subsample, subsample))
		image = transform.pyramid_expand(image, subsample, 0, 0)

	image = exposure.rescale_intensity(image, out_range=(0,args["depth"][0]))

	if (args["visualize"]):
		io.imshow(image)
		io.show()

	source = generate_face(image, subsample, args["depth"][0], FLICKER_SPEED)

	if source:
		with open(args["output"][0], 'w') as file_:
			file_.write(source)
	else:
		print "Attempted to generate source code, failed."
    def predict_image(self, test_img, show=False):
        '''
        predicts classes of input image
        INPUT   (1) str 'test_image': filepath to image to predict on
                (2) bool 'show': True to show the results of prediction, False to return prediction
        OUTPUT  (1) if show == False: array of predicted pixel classes for the center 208 x 208 pixels
                (2) if show == True: displays segmentation results
        '''
        imgs = io.imread(test_img).astype('float').reshape(5,240,240)
        plist = []

        # create patches from an entire slice
        for img in imgs[:-1]:
            if np.max(img) != 0:
                img /= np.max(img)
            p = extract_patches_2d(img, (33,33))
            plist.append(p)
        patches = np.array(zip(np.array(plist[0]), np.array(plist[1]), np.array(plist[2]), np.array(plist[3])))

        # predict classes of each pixel based on model
        full_pred = self.model_comp.predict_classes(patches)
        fp1 = full_pred.reshape(208,208)
        if show:
            io.imshow(fp1)
            plt.show
        else:
            return fp1
def test(frame):
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    lower_white = np.array([0,0,200])
    upper_white = np.array([300,100,300])
    # Threshold the HSV image to get only white colors
    mask = cv2.inRange(hsv, lower_white, upper_white)
    gray = cv2.cvtColor(mask, cv2.COLOR_BAYER_GB2GRAY)
    gradX = cv2.Sobel(gray, ddepth = cv2.CV_32F, dx = 1, dy = 0, ksize = -1)
    gradY = cv2.Sobel(gray, ddepth = cv2.CV_32F, dx = 0, dy = 1, ksize = -1)
    # subtract the y-gradient from the x-gradient
    gradient = cv2.subtract(gradX, gradY)
    gradient = cv2.convertScaleAbs(gradient)
    # blur and threshold the image
    blurred=cv2.blur(gray,(9,9))
    blurred = cv2.blur(gradient, (9, 9))
    (_, thresh) = cv2.threshold(blurred, 225, 255, cv2.THRESH_BINARY)
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (21, 7))
    closed = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
    # perform a series of erosions and dilations
    closed = cv2.erode(closed, None, iterations = 4)
    closed = cv2.dilate(closed, None, iterations = 4)

    image, contours,hierarchy= cv2.findContours(closed,
                               cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    img= cv2.drawContours(frame, contours, -1,(0,0,0),3)
    io.imshow(img)
def background_sub(frame): 
    '''
    out 
    '''
    return gray

    io.imshow(img)
示例#15
0
def show_user_imgs(imgs):
	'''
	Display images to new_user and prompt for response (Like / NoLike)
	Keep track of preferences in a list.
	
	INPUT: list of pairs, (username, img_id)

	OUTPUT: np.array of new_user's preferences (zero or one)
	'''
	# initialize the list of preferences
	likes = []
	for img in imgs:
		# generate the path to the image
		fname = '../imgs/{}'.format(img)
		# update status to terminal
		print fname
		# show image to user
		imshow(fname)
		plt.show()
		# initialize string of preferences
		pref = ''
		# logical xor in python is != ... i know, right??
		while not pref in ['0', '1']:
			# survey whether user liked photo
			print 'you entered: {}'.format(pref)
			pref = raw_input('Did you like that photo? Yes:1, No:0...')
			# keep track of the result in list 'likes'
			likes.append(int(pref))
	# convert list 'likes' to np.array for better handling downstream
	likes = np.array(likes).astype('bool')
	return likes
示例#16
0
def just_do_it(limit_cont):
	fig = plt.figure(facecolor='black')
	plt.gray()
	print("Rozpoczynam przetwarzanie obrazow...")
    
	for i in range(20):
		img = data.imread(images[i])

		gray_img = to_gray(images[i])				# samoloty1.pdf
		#gray_img = to_gray2(images[i],  1001, 0.2, 5, 9, 12) 	# samoloty2.pdf
		#gray_img = to_gray2(images[i],  641, 0.2, 5, 20, 5)	# samoloty3.pdf
		conts = find_contours(gray_img, limit_cont)
		centrs = [find_centroid(cont) for cont in conts]

		ax = fig.add_subplot(4,5,i)
		ax.set_yticks([])
		ax.set_xticks([])
		io.imshow(img)
		print("Przetworzono: " + images[i])
        
		for n, cont in enumerate(conts):
			ax.plot(cont[:, 1], cont[:, 0], linewidth=2)
            
		for centr in centrs:
			ax.add_artist(plt.Circle(centr, 5, color='white'))
            
	fig.tight_layout()
	#plt.show()
	plt.savefig('samoloty3.pdf')
def recognize(breaker, show, sample, LEVEL, THRESHOLD):
    img = io.imread(sample)
    if show:
        io.imshow(img, 'pil')
    res = ''
    for cat in breaker.match(img, LEVEL, THRESHOLD):
        res += cat.name
    print res
示例#18
0
def main():
    from skimage import data, io, filters
    testfolder='/Users/davidgreenfield/Downloads/pics_boots/'
    testimage='B00A0GVP8A.jpg'
    image = io.imread(testfolder+testimage,flatten=True) # or any NumPy array!
    edges = filters.sobel(image)
    io.imshow(edges)
    io.show()
def get_rect(path):
    e, img = detect(path)
    img = img[40:, :]
    imshow(img)
    # import Image
    # im = Image.fromarray(img)
    # im.save("your_file.jpg")
    return img
def testing_result_2(obj, obj2):
    '''obj = hand2_cascade.detectMultiScale(closed) '''
    a = obj2
    for (x,y,w,h) in obj:
        # draw rectangle at the specific location
        cv2.rectangle(a,(x,y),(x+w,y+h),(255,0,0),2) 
        # extract the segmentation 
    io.imshow(a)
示例#21
0
def _detect_spots_hough_circle(image, radius):
	edges = canny(image)
	imshow(edges)
	show()
	hough_radii = np.arange(radius/2, radius*2, 10)
	hough_circles = hough_circle(edges, hough_radii)
	
	print(hough_circles)
 def doEigengesichter(self,pos):
     dim = int(math.sqrt(self.VT.shape[1]))
     mdata = np.zeros((dim,dim))
     imageVector = self.VT[pos,:]
     for i in range(0,len(imageVector),int(dim)):
         row = np.array(imageVector)[i:i+dim]
         mdata[int(i/dim),:] = row
     io.imshow(mdata)          
示例#23
0
def color_transformation():
    # 彩色变换
    image=data.coffee()
    brighter=np.uint8(image*0.5+255*0.5)
    darker=np.uint8(image*0.5)
    io.imshow(brighter)
    io.show()
    io.imshow(darker)
    io.show()
def adapative_threshold(image, block_size=100):
	"""
	This method returns the adaptively-thresholded image.
	"""

	thresholded_image = threshold_adaptive(image, block_size)
	imshow(thresholded_image)

	return thresholded_image
示例#25
0
文件: render.py 项目: Neurita/cajal
def show_connectivity_matrix(image, cmap=None):
    """
    @param image: 2D ndarray
    @param cmap: colormap
    """
    if cmap is None:
        cmap = cm.jet

    skio.imshow(image, cmap=cmap)
 def predict_image(self, filepath_image, show=False):
     '''
     predicts classes of input image
     INPUT   (1) str 'filepath_image': filepath to image to predict on
             (2) bool 'show': True to show the results of prediction, False to return prediction
     OUTPUT  (1) if show == False: array of predicted pixel classes for the center 208 x 208 pixels
             (2) if show == True: displays segmentation results
     '''
     print 'Starting prediction...'
     if self.cascade_model:
         images = io.imread(filepath_image).astype('float').reshape(5, 216, 160)
         p33list = []
         p65list = []
         # create patches from an entire slice
         for image in images[:-1]:
             if np.max(image) != 0:
                 image /= np.max(image)
             patch65 = extract_patches_2d(image, (65, 65))
             p65list.append(patch65)
             p33list.append(self.center_n(33, patch65))
             print str(len(p33list))
         patches33 = np.array(zip(p33list[0], p33list[1], p33list[2], p33list[3]))
         patches65 = np.array(zip(p65list[0], p65list[1], p65list[2], p65list[3]))
         # predict classes of each pixel based on model
         prediction = self.model.predict([patches65, patches33])
         print 'Predicted'
         prediction = prediction.reshape(208, 208)
         if show:
             io.imshow(prediction)
             plt.show
         else:
             return prediction
     else:
         images = io.imread(filepath_image).astype('float').reshape(5, 216, 160)
         p33list = []
         # create patches from an entire slice
         for image in images[:-1]:
             if np.max(image) != 0:
                 image /= np.max(image)
             patch33 = extract_patches_2d(image, (33, 33))
             p33list.append(patch33)
         patches33 = np.array(zip(p33list[0], p33list[1], p33list[2], p33list[3]))
         # predict classes of each pixel based on model
         prediction = self.cnn1.predict(patches33)
         print 'Predicted'
         prediction = prediction.reshape(5, 184, 128)
         predicted_classes = np.argmax(prediction, axis=0)
         if show:
             print 'Let s show'
             for i in range(5):
                 io.imshow(prediction[i])
                 plt.show
                 print 'Showed'
                 return prediction
         else:
             return predicted_classes
示例#27
0
文件: planes3.py 项目: Yamadads/kck
def paint():
    fig = plt.figure(facecolor='black')
    for i in range(0,len(files)):
        contourImage = getContourImage(files[i])
        ax = fig.add_subplot(2,3,i)
        ax.set_xticks([])
        ax.set_yticks([])
        io.imshow(contourImage)
    fig.tight_layout()
    plt.show()
示例#28
0
文件: projekt1.py 项目: khortur/PiRO
def main():
    os.chdir("daneA/set0")

    for file in glob.glob("*.png"):
        img = normalize_one_picture(file)
        io.imshow(img)
        print(file)
        io.show()

    return 0
示例#29
0
def main(argv):
  filename = argv[1]
  img = io.imread(filename, as_grey=True)
  lpyra = tuple(transform.pyramid_laplacian(img))
  l = lpyra[0]
  l = exposure.equalize_hist(l)
  y, x = np.indices((l.shape[0],l.shape[1]))
  vect = np.array(zip(y.reshape(y.size),x.reshape(x.size),l.reshape(l.size)))
  io.imshow(l)
  io.show()
    def show(self, sub_dir, img_ind):
        """
        View the nth image in the nth class

        :param sub_dir: The name of the category
        :param img_ind: The index of the category of images
        """
        sub_dir_ind = self.label_map[sub_dir]
        io.imshow(self.img_lst2[sub_dir_ind][img_ind])
        plt.show()
示例#31
0
# print(img_rec) 
# print(img_gray)
# print(maxm)

# io.imshow(img_rec)


# cnt = 0
# for i in range(512):
#     for j in range(512):
#         if img_rec[i][j] != img_gray[i][j]:
#             cnt += 1
# print(cnt) # 23万
im_uint8 = img_as_ubyte(img_rec)
io.imsave("嵌入水印的图像.jpg",im_uint8)
io.imshow(img_rec)
# print(im_uint8)
print(num1) #4000上下
img_embedded = io.imread('嵌入水印的图像.jpg')
# img_embedded = img_rec
watermark = np.zeros([64,64]) 

for y in range(64):       
    for x in range(64): 
        block = np.zeros([8,8])
        for i in range(8):
            block[i] = img_embedded[y*8+i][8*x:8*(x+1)]
        coeffs1 = pywt.dwt2(block, 'haar') # 进行两次小波变换
        cA, (cH, cV, cD) = coeffs1 
        coeffs2 = pywt.dwt2(cA, 'haar')
        cA, (cH, cV, cD) = coeffs2
示例#32
0
from skimage import io
import numpy as np
import cv2

file_path = "./img/img.jpg"

image = cv2.imread(file_path, cv2.IMREAD_GRAYSCALE)
m, n = image.shape

# 原本L应该时一个对称矩阵,此时则将对称矩阵的对角线元素以一维数组形式输出
# P (m, m)
# L (m,)
# Q (n, n)
P, L, Q = np.linalg.svd(image)
print(P.shape)
print(Q.shape)
# L为800的一位数组,使用diag()输出以L为对角线的矩阵
tmp = np.diag(L)
if m < n:
    # 水平堆叠,目的就是为了将d扩充为mxn大小
    d = np.hstack((tmp, np.zeros((m, n - m))))
else:
    # 垂直堆叠
    d = np.vstack((tmp, np.zeros((m - n, n))))

k = 50

image2 = P[:, :k].dot(d[:k, :k]).dot(Q[:k, :])
io.imshow(np.uint8(image2))
plt.show()
# read in the image
im = skio.imread(imname)

# convert to double (might want to do this later on to save memory)
im = sk.img_as_float(im)

# compute the height of each part (just 1/3 of total)
height = np.floor(im.shape[0] / 3.0).astype(np.int)

# separate color channels
b = im[:height]
g = im[height:2 * height]
r = im[2 * height:3 * height]

# align the images
# functions that might be useful for aligning the images include:
# np.roll, np.sum, sk.transform.rescale (for multiscale)

### ag = align(g, b)
### ar = align(r, b)
# create a color image
im_out = np.dstack([ar, ag, b])

# save the image
fname = '/out_path/out_fname.jpg'
skio.imsave(fname, im_out)

# display the image
skio.imshow(im_out)
skio.show()
示例#34
0
    return sparse_array
    # mask_keep = depth > 0
    # if max_depth is not np.inf:
    #     mask_keep = np.bitwise_and(mask_keep, depth <= max_depth)
    # n_keep = np.count_nonzero(mask_keep)
    # if n_keep == 0:
    #     io.imshow(mask_keep, interpolation='nearest')
    #     io.show()
    #     return mask_keep
    # else:
    #     prob = float(num_samples) / n_keep
    #     io.imshow(np.bitwise_and(mask_keep, np.random.uniform(0, 1, depth.shape) < prob), interpolation='nearest')
    #     io.show()
    #     return np.bitwise_and(mask_keep, np.random.uniform(0, 1, depth.shape) < prob)


rgb1, depth1 = h5_loader(path1)
depth_sparse = dense_to_sparse(depth1)
io.imshow(depth_sparse, interpolation='nearest')
io.show()

print(np.amax(depth_sparse))

io.imshow(depth1)
# io.imshow(rgb1/255)
io.show()

# cv2.imshow('', rgb1)
# cv2.waitKey()
示例#35
0
def performDetect(
        imagePath="/home/snuzero1/darknet/x64/data/obj/pylon_camera_node-000220-1540444604.221.jpg",
        thresh=0.25,
        configPath="/home/snuzero1/darknet/yolo-obj.cfg",
        weightPath="/home/snuzero1/darknet/backup/yolo-obj_last.weights",
        metaPath="/home/snuzero1/darknet/x64/data/obj.data",
        showImage=True,
        makeImageOnly=False,
        initOnly=False):
    """
    Convenience function to handle the detection and returns of objects.

    Displaying bounding boxes requires libraries scikit-image and numpy

    Parameters
    ----------------
    imagePath: str
        Path to the image to evaluate. Raises ValueError if not found

    thresh: float (default= 0.25)
        The detection threshold

    configPath: str
        Path to the configuration file. Raises ValueError if not found

    weightPath: str
        Path to the weights file. Raises ValueError if not found

    metaPath: str
        Path to the data file. Raises ValueError if not found

    showImage: bool (default= True)
        Compute (and show) bounding boxes. Changes return.

    makeImageOnly: bool (default= False)
        If showImage is True, this won't actually *show* the image, but will create the array and return it.

    initOnly: bool (default= False)
        Only initialize globals. Don't actually run a prediction.

    Returns
    ----------------------


    When showImage is False, list of tuples like
        ('obj_label', confidence, (bounding_box_x_px, bounding_box_y_px, bounding_box_width_px, bounding_box_height_px))
        The X and Y coordinates are from the center of the bounding box. Subtract half the width or height to get the lower corner.

    Otherwise, a dict with
        {
            "detections": as above
            "image": a numpy array representing an image, compatible with scikit-image
            "caption": an image caption
        }
    """
    # Import the global variables. This lets us instance Darknet once, then just call performDetect() again without instancing again
    global metaMain, netMain, altNames  #pylint: disable=W0603
    assert 0 < thresh < 1, "Threshold should be a float between zero and one (non-inclusive)"
    if not os.path.exists(configPath):
        raise ValueError("Invalid config path `" +
                         os.path.abspath(configPath) + "`")
    if not os.path.exists(weightPath):
        raise ValueError("Invalid weight path `" +
                         os.path.abspath(weightPath) + "`")
    if not os.path.exists(metaPath):
        raise ValueError("Invalid data file path `" +
                         os.path.abspath(metaPath) + "`")
    if netMain is None:
        netMain = load_net_custom(configPath.encode("ascii"),
                                  weightPath.encode("ascii"), 0,
                                  1)  # batch size = 1
    if metaMain is None:
        metaMain = load_meta(metaPath.encode("ascii"))
    if altNames is None:
        # In Python 3, the metafile default access craps out on Windows (but not Linux)
        # Read the names file and create a list to feed to detect
        try:
            with open(metaPath) as metaFH:
                metaContents = metaFH.read()
                import re
                match = re.search("names *= *(.*)$", metaContents,
                                  re.IGNORECASE | re.MULTILINE)
                if match:
                    result = match.group(1)
                else:
                    result = None
                try:
                    if os.path.exists(result):
                        with open(result) as namesFH:
                            namesList = namesFH.read().strip().split("\n")
                            altNames = [x.strip() for x in namesList]
                except TypeError:
                    pass
        except Exception:
            pass
    if initOnly:
        print("Initialized detector")
        return None
    if not os.path.exists(imagePath):
        raise ValueError("Invalid image path `" + os.path.abspath(imagePath) +
                         "`")
    # Do the detection
    #detections = detect(netMain, metaMain, imagePath, thresh)	# if is used cv2.imread(image)
    detections = detect(netMain, metaMain, imagePath.encode("ascii"), thresh)
    if showImage:
        try:
            from skimage import io, draw
            import numpy as np
            image = io.imread(imagePath)
            print("*** " + str(len(detections)) +
                  " Results, color coded by confidence ***")
            imcaption = []
            for detection in detections:
                label = detection[0]
                confidence = detection[1]
                pstring = label + ": " + str(np.rint(100 * confidence)) + "%"
                imcaption.append(pstring)
                print(pstring)
                bounds = detection[2]
                shape = image.shape
                # x = shape[1]
                # xExtent = int(x * bounds[2] / 100)
                # y = shape[0]
                # yExtent = int(y * bounds[3] / 100)
                yExtent = int(bounds[3])
                xEntent = int(bounds[2])
                # Coordinates are around the center
                xCoord = int(bounds[0] - bounds[2] / 2)
                yCoord = int(bounds[1] - bounds[3] / 2)
                boundingBox = [[xCoord, yCoord], [xCoord, yCoord + yExtent],
                               [xCoord + xEntent, yCoord + yExtent],
                               [xCoord + xEntent, yCoord]]
                # Wiggle it around to make a 3px border
                rr, cc = draw.polygon_perimeter([x[1] for x in boundingBox],
                                                [x[0] for x in boundingBox],
                                                shape=shape)
                rr2, cc2 = draw.polygon_perimeter(
                    [x[1] + 1 for x in boundingBox],
                    [x[0] for x in boundingBox],
                    shape=shape)
                rr3, cc3 = draw.polygon_perimeter(
                    [x[1] - 1 for x in boundingBox],
                    [x[0] for x in boundingBox],
                    shape=shape)
                rr4, cc4 = draw.polygon_perimeter(
                    [x[1] for x in boundingBox],
                    [x[0] + 1 for x in boundingBox],
                    shape=shape)
                rr5, cc5 = draw.polygon_perimeter(
                    [x[1] for x in boundingBox],
                    [x[0] - 1 for x in boundingBox],
                    shape=shape)
                boxColor = (int(255 * (1 - (confidence**2))),
                            int(255 * (confidence**2)), 0)
                draw.set_color(image, (rr, cc), boxColor, alpha=0.8)
                draw.set_color(image, (rr2, cc2), boxColor, alpha=0.8)
                draw.set_color(image, (rr3, cc3), boxColor, alpha=0.8)
                draw.set_color(image, (rr4, cc4), boxColor, alpha=0.8)
                draw.set_color(image, (rr5, cc5), boxColor, alpha=0.8)
            if not makeImageOnly:
                io.imshow(image)
                io.show()
            detections = {
                "detections": detections,
                "image": image,
                "caption": "\n<br/>".join(imcaption)
            }
        except Exception as e:
            print("Unable to show image: " + str(e))
    return detections
示例#36
0
def show_pic(img):
    imshow(img)
    show()
示例#37
0
import skimage.io as io


def img_unbg(img):
    w = img.shape[0]
    h = img.shape[1]
    ub_img = img.copy()
    print("calculate...")
    for i in range(w):
        for j in range(h):
            if img[i, j, 0] == 0 & img[i, j, 1] == 0 & img[i, j, 2] == 0:
                ub_img[i, j, 3] = 0
    return ub_img


if __name__ == '__main__':
    root_dir = 'C:/Users/Administrator/Desktop/666/'
    img_path = root_dir + '*.bmp'
    # coll = io.ImageCollection(str, load_func=transparent_back)
    coll = io.ImageCollection(img_path)
    for i in range(len(coll)):
        print("All:%d,  processing:%d" % (len(coll), i + 1))
        # img = transparent_back(coll[i])
        img = img_unbg(coll[i])
        io.imsave(root_dir + str(i) + '.png', img)
        print("Image %d have saved!" % i)
        io.imshow(coll[0])
        io.show()
    print("All image have saved.")
p = np.zeros([670, 128, 128], dtype=np.uint8)
for i in range(670):
    pixel = Y_train[i]
    for m in range(128):
        for n in range(128):
            if (pixel[m, n]):
                p[i, m, n] = 1
            else:
                p[i, m, n] = 0
#%%
Y_train = keras.utils.to_categorical(p, 2)
#%%
weight_mask = np.ones([670, 128, 128], dtype=np.float32)
#%%
ix = random.randint(0, len(train_ids))
imshow(X_train[ix])
plt.show()
imshow(np.squeeze(Y_train[ix]))
plt.show()


#%%
def mean_iou(y_true, y_pred):
    prec = []
    for t in np.arange(0.5, 1.0, 0.05):
        y_pred_ = tf.to_int32(y_pred > t)
        score, up_opt = tf.metrics.mean_iou(y_true, y_pred_, 2)
        K.get_session().run(tf.local_variables_initializer())
        with tf.control_dependencies([up_opt]):
            score = tf.identity(score)
        prec.append(score)
示例#39
0
    from skimage import io
    npImg1 = cv2.imread("einstein.png")

    img1 = torch.from_numpy(np.rollaxis(npImg1,
                                        2)).float().unsqueeze(0) / 255.0
    img2 = torch.rand(img1.size())

    if torch.cuda.is_available():
        img1 = img1.cuda()
        img2 = img2.cuda()

    img1 = Variable(img1, requires_grad=False)
    img2 = Variable(img2, requires_grad=True)

    ssim_value = ssim(img1, img2).item()
    print("Initial ssim:", ssim_value)

    ssim_loss = SSIMLoss()
    optimizer = optim.Adam([img2], lr=0.01)

    while ssim_value < 0.99:
        optimizer.zero_grad()
        ssim_out = -ssim_loss(img1, img2)
        ssim_value = -ssim_out.item()
        print('{:<4.4f}'.format(ssim_value))
        ssim_out.backward()
        optimizer.step()
    img = np.transpose(img2.detach().cpu().squeeze().float().numpy(),
                       (1, 2, 0))
    io.imshow(np.uint8(np.clip(img * 255, 0, 255)))
示例#40
0
    return images, labels


train_data_dir = '/home/research_centre_gpu/train'
validation_data_dir = '/home/research_centre_gpu/test'

images, labels = load_data(train_data_dir)
val_images, val_labels = load_data(validation_data_dir)

images_64 = np.array(images).astype(np.float32)
labels_64 = np.array(labels)
val_images_64 = np.array(val_images).astype(np.float32)  #uint8 newly added
val_labels_64 = np.array(val_labels)

io.imshow(images_64[1050])
io.imshow(val_images_64[1050])

#images_64 = images_64/255
#val_images_64 = val_images_64/255

#pt='/home/bhagu/Documents/sohail/data/segmented/training_set/1/8c1a0a73-26c9-405e-8957-8dd58df6bdf4___GCREC_Bact.Sp 3738_final_masked.jpg'
#test_img=data.imread(pt)
#ri=test_img * 255
#test_img2=np.array(test_img).astype(np.uint8)
#
#test_img2 = test_img2/255
#print(test_img2[10])
##val_images_64 = val_images_64/255
#io.imshow(test_img)
#io.imshow(test_img2)
示例#41
0
def extractFeatures():
    imageNames = ['a', 'd', 'f', 'h', 'k', 'm', 'n', 'o', 'p', 'q', 'r', 's', 'u', 'w', 'x', 'z']
    #imageNames = ['a']
    Features = []
    featuresLabels = []
    for name in imageNames:
        # Reading an Image File
        img = io.imread(name + '.bmp')
        #print img.shape
        # Visualizing an Image/Matrix
        '''
        io.imshow(img)
        plt.title('Original Image')
        io.show()
        '''
        # Image Histogram
        '''
        hist = exposure.histogram(img)
        plt.bar(hist[1], hist[0])
        plt.title('Histogram')
        plt.show()
        '''
        # Binarization by Thresholding
        ret, binary = cv.threshold(img, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
        #ret, binary = cv.threshold(img, 0, 255, cv.THRESH_BINARY | cv.THRESH_TRIANGLE)
        #print ret
        th = ret
        img_binary = (img < th).astype(np.double)
        img_dilation = morphology.binary_dilation(img_binary, selem=None)
        img_erosion = morphology.binary_erosion(img_binary, selem=None)
        # Displaying Binary Image
        '''
        io.imshow(img_binary)
        plt.title('Binary Image')
        io.show()
        '''
        # Connected Component Analysis
        img_label = label(img_binary, background=0)
        '''
        io.imshow(img_label)
        plt.title('Labeled Image')
        io.show()
        print np.amax(img_label)
        '''
        # Displaying Component Bounding Boxes
        regions = regionprops(img_label)
        io.imshow(img_binary)
        ax = plt.gca()
        thresholdR = 15
        thresholdC = 15
        for props in regions:
            minr, minc, maxr, maxc = props.bbox
            if (maxr - minr) >= thresholdR and (maxc - minc) >= thresholdC:
                # Computing Hu Moments and Removing Small Components
                roi = img_binary[minr:maxr, minc:maxc]
                m = moments(roi)
                cr = m[0, 1] / m[0, 0]
                cc = m[1, 0] / m[0, 0]
                mu = moments_central(roi, cr, cc)
                nu = moments_normalized(mu)
                hu = moments_hu(nu)
                Features.append(hu)
                featuresLabels.append(name)
                plt.text(maxc, minr, name, bbox=dict(facecolor='white', alpha=0.5))
                ax.add_patch(Rectangle((minc, minr), maxc - minc, maxr - minr, fill=False, edgecolor='red', linewidth=1))
        plt.title('Bounding Boxes')
        #plt.savefig('report/' + name + '.png')
        io.show()
    '''
    D = cdist(Features, Features)
    print D
    io.imshow(D)
    plt.title('Distance Matrix')
    io.show()
    '''
    return Features, featuresLabels
示例#42
0
def test():
    trainFeatures, trainLebels = extractFeatures()
    trainMeans, trainDeviations = normalization(trainFeatures)
    testNames = ['test1', 'test2']
    #testNames = ['test1']
    testFeatures = []
    testLabels = []
    testTruth = []
    correct = 0
    D_copy = np.array(0)
    #textPosition = []
    for i in range(len(testNames)):
        classes, locations = readPkl(testNames[i])
        img = io.imread(testNames[i] + '.bmp')
        #testTruth = ['a']*7+['d']*7+['m']*7+['n']*7+['o']*7+['p']*7+['q']*7+['r']*7+['u']*7+['w']*7
        ret, binary = cv.threshold(img, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
        #ret, binary = cv.threshold(img, 0, 255, cv.THRESH_BINARY | cv.THRESH_TRIANGLE)
        #print ret
        th = ret
        img_binary = (img < th).astype(np.double)
        img_dilation = morphology.binary_dilation(img_binary, selem=None)
        img_erosion = morphology.binary_erosion(img_binary, selem=None)
        img_label = label(img_binary, background=0)
        regions = regionprops(img_label)
        io.imshow(img_binary)
        ax = plt.gca()
        thresholdR = 15
        thresholdC = 15
        for props in regions:
            minr, minc, maxr, maxc = props.bbox
            # Computing Hu Moments and Removing Small Components
            if (maxr - minr) >= thresholdR and (maxc - minc) >= thresholdC:
                #textPosition.append((maxc, minr))
                roi = img_binary[minr:maxr, minc:maxc]
                m = moments(roi)
                cr = m[0, 1] / m[0, 0]
                cc = m[1, 0] / m[0, 0]
                mu = moments_central(roi, cr, cc)
                nu = moments_normalized(mu)
                hu = moments_hu(nu)
                testFeatures.append(hu)
                
                for i in range(7):
                    testFeatures[-1][i] = (testFeatures[-1][i] - trainMeans[i]) / trainDeviations[i]
                D = cdist(testFeatures, trainFeatures)
                #D_copy = copy.deepcopy(D)
                D_index = np.argsort(D, axis=1)
                testLabels.append(trainLebels[D_index[-1][0]])
                
                indexFix = locationFix(locations, minr, minc, maxr, maxc)
                if indexFix is not None:
                    if testLabels[-1] == classes[indexFix]:
                        correct += 1
                
                plt.text(maxc, minr, testLabels[-1], bbox=dict(facecolor='white', alpha=0.5))
                ax.add_patch(Rectangle((minc, minr), maxc - minc, maxr - minr, fill=False, edgecolor='red', linewidth=1))
        plt.title('Bounding Boxes')
        io.show()
    print correct, len(testLabels)
    correctRate = correct / len(testLabels)
    print correctRate
示例#43
0
##
%load_ext autoreload
%autoreload 2
##
sys.path.append(".")
##
from skimage.io import imread, imshow
from patchify import patchify, unpatchify
##
img = imread("wp2559551.jpg")

##
patches = patchify(img, (900, 256, 3), step=1)

##
imshow(unpatchify(patches, img.shape))
##
patches = patchify(img[:,:,1], (645, 256), step=1)

##
imshow(img[:,:,1])
##
imshow(unpatchify(patches, img[:,:,1].shape))

##
from skimage import io, data

io.imshow(data.camera())
io.show()

io.imshow(data.text())
io.show()
cols = image.shape[1]

print(rows)
print(cols)

image = image.reshape(image.shape[0] * image.shape[1], 3)
kmeans = KMeans(n_clusters=128, n_init=10, max_iter=200)
kmeans.fit(image)

print('finish1')

clusters = np.asarray(kmeans.cluster_centers_, dtype=np.uint8)
labels = np.asarray(kmeans.labels_, dtype=np.uint8)
labels = labels.reshape(rows, cols)

print('finish2')

print(clusters.shape)
np.save('codebook_test.npy', clusters)
io.imsave('zip_testimg.jpg', labels)

print('finish3')

image = io.imread('compressed_test.jpg')
io.imshow(image)
io.show()

print('finish4')


示例#46
0
img = io.imread('/home/jorge/Documentos/Topicos/subject11.surprised')
#lena = misc.imread('img3.JPG')

io.imsave("lena.JPG", img)
#plt.imshow(img)
#plt.show()

img = cv2.imread('/home/jorge/Documentos/Topicos/lena.JPG')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2)
    roi_gray = gray[y:y + h, x:x + w]
    roi_color = img[y:y + h, x:x + w]
    eyes = eye_cascade.detectMultiScale(roi_gray)
    for (ex, ey, ew, eh) in eyes:
        cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2)

io.imsave("lena.JPG", img)

#cv2.imshow('img',img2)
#cv2.waitKey(0)

io.imshow(img)
io.show()

#plt.imshow(l, cmap=plt.cm.gray)
#plt.show()
示例#47
0
def Colorizing_the_Russian_Empire_tif(file):

    img = skio.imread(file)
    height = img.shape[0] // 3
    Three_Channel = Get_three_channel(img)
    for i in range(len(Three_Channel)):
        Three_Channel[i] = Crop_Img(Three_Channel[i])

    reconstruct_b = Three_Channel[0]
    reconstruct_g = Three_Channel[1]
    reconstruct_r = Three_Channel[2]

    down_scale = 20
    k_size = int((height // down_scale) // 5)
    g_shift_row, g_shift_col, r_shift_row, r_shift_col = 0, 0, 0, 0

    while (down_scale >= 1):

        smaller_b = sktr.downscale_local_mean(Three_Channel[0],
                                              (down_scale, down_scale))
        smaller_g = sktr.downscale_local_mean(Three_Channel[1],
                                              (down_scale, down_scale))
        smaller_r = sktr.downscale_local_mean(Three_Channel[2],
                                              (down_scale, down_scale))

        Shift_result = alignGandRtoB(smaller_r, smaller_g, smaller_b, k_size)
        Shift_g = Shift_result[0]
        Shift_r = Shift_result[1]

        g_shift_row += (Shift_g[0] * down_scale)
        g_shift_col += (Shift_g[1] * down_scale)
        r_shift_row += (Shift_r[0] * down_scale)
        r_shift_col += (Shift_r[1] * down_scale)

        Three_Channel[1] = np.roll(Three_Channel[1],
                                   Shift_g[0] * down_scale,
                                   axis=0)
        Three_Channel[1] = np.roll(Three_Channel[1],
                                   Shift_g[1] * down_scale,
                                   axis=1)
        Three_Channel[2] = np.roll(Three_Channel[2],
                                   Shift_r[0] * down_scale,
                                   axis=0)
        Three_Channel[2] = np.roll(Three_Channel[2],
                                   Shift_r[1] * down_scale,
                                   axis=1)

        down_scale = down_scale // 2
        k_size = k_size // 2

    reconstruct_g = np.roll(reconstruct_g, g_shift_row, axis=0)
    reconstruct_g = np.roll(reconstruct_g, g_shift_col, axis=1)
    reconstruct_r = np.roll(reconstruct_r, r_shift_row, axis=0)
    reconstruct_r = np.roll(reconstruct_r, r_shift_col, axis=1)

    output = np.dstack([reconstruct_r, reconstruct_g, reconstruct_b])

    fname = 'aftre' + file[-8:-4] + '.jpg'
    skio.imsave(fname, output)
    skio.imshow(output)
    skio.show()
示例#48
0
def debugim(im):
    imm = np.asarray(im)
    io.imshow(imm)
    plt.show()
示例#49
0
preds_test_flat = preds_test_t

# Create list of upsampled test masks, so that the masks match the original image sizes
preds_test_upsampled = []
for i in range(len(preds_test)):
    preds_test_upsampled.append(
        resize(np.squeeze(preds_test_flat[i]),
               (sizes_test[i][0], sizes_test[i][1]),
               mode='constant',
               preserve_range=True))

#need take the whole mask of the training set and break them down into individual cells
#use skimage to find separate cells, may have trouble if they overlap
new_test_ids = []
rles = []
for n, id_ in enumerate(test_ids):
    cx, cy = prob_to_centers(preds_test_upsampled[n])
    #rles.extend(rle)
    #new_test_ids.extend([id_] * len(rle))
    if (n < 10):
        pltImg = resize(X_test[n], (sizes_test[n][0], sizes_test[n][1]),
                        mode='constant',
                        preserve_range=True)
        plt.subplot(221)
        imshow(np.squeeze(pltImg))
        plt.subplot(222)
        plt.plot(cy, cx, 'ro')
        plt.axis([0, sizes_test[n][1], sizes_test[n][0], 0])
        plt.subplot(223)
        imshow(np.squeeze(preds_test_upsampled[n]))
        plt.show()
get_ipython().magic(u'matplotlib inline')
plt.rcParams['figure.figsize'] = (20.0, 10.0)

print("opencv version", (cv2.__version__))

# In[2]:

import warnings
warnings.filterwarnings('ignore')

# ## Convert local image to greyscale

# In[3]:

feather = io.imread("./images/pena_pavao.jpg")
io.imshow(feather)
io.show()

# In[4]:

feather = io.imread("./images/pena_pavao.jpg", as_grey=True)
print(type(feather), feather.shape, feather.dtype)
io.imshow(feather)
io.show()

# In[5]:

image = cv2.imread("./images/pena_pavao.jpg", 0)
cv2.imshow('Grayscale', image)
#cv2.waitKey(0) dont use this one on jupyter notebook
cv2.startWindowThread()
示例#51
0
'''
将lena图片进行二值化,像素值大于128的变为1,否则变为0
'''

from skimage import io,data,color

img=data.chelsea()

# 使用了color模块的rgb2gray()函数,将彩色三通道图片转换成灰度图。转换结果为float64类型的数组,范围为[0,1]之间。
img_gray = color.rgb2gray(img)
rows, cols = img_gray.shape
for i in range(rows):
    for j in range(cols):
        if (img_gray[i, j] <= 0.5):
            img_gray[i, j] = 0
        else:
            img_gray[i,j] = 1
        pass
    pass

io.imshow(img_gray)

# 将彩色三通道图片转换成灰度图,最后变成unit8, float转换为unit8是有信息损失的。
def show(img):
    io.imshow(img)
    io.show()
示例#53
0
    ]


def to_bits(s):
    result = []
    for c in s:
        bits = bin(ord(c))[2:]
        result.extend(map(int, bits.rjust(8, '0')))
    return result


def from_bits(bits):
    chars = []
    for i in range(len(bits) // 8):
        byte_char = bits[i * 8:(i + 1) * 8]
        int_char = int(''.join(map(str, byte_char)), 2)
        chars.append(chr(int_char))
    return ''.join(chars)


if __name__ == '__main__':
    original = io.imread('pic.jpeg')
    test_message = to_bits("Hello")

    changed = embed_message(original, test_message)
    mes = retrieve_message(changed, len(test_message))

    print(from_bits(mes))
    io.imshow(np.hstack((original, changed)))
    plt.show()
示例#54
0
    anatPath = '{}\\sub-01_anat_sub-01_T1w.nii'.format(datasetDir)
    #maskImg = masking.compute_gray_matter_mask(anatPath, threshold=0.8, connected=True, opening=3, verbose=1)
    #maskImg = masking.compute_background_mask(anatPath, verbose=2)
    #maskImg = masking.compute_epi_mask(funcPath)
    #brainImg = image.clean_img(funcPath, high_pass=0.009, standardize=True, t_r=2.5, mask_img=maskImg)
    #brainImg = image.mean_img(funcPath, verbose=2, n_jobs=-1)
    #brainImg = image.smooth_img(brainImg, fwhm='fast')
    #brainImg = image.math_img('img1 * img2', img1=brainImg, img2=maskImg)
    #brainImg = image.math_img('img1 * img2', img1=anatPath, img2=maskImg)

    #view = plotting.view_img(stat_map_img=brainImg, bg_img=None, threshold=None)
    #view.open_in_browser()
    #brainImg = image.load_img(anatPath)
    #cutSlices = plotting.find_cut_slices(brainImg, n_cuts=1)
    #plotting.plot_anat(brainImg, display_mode='z', output_file='{}\\test.png'.format(dataDir), cut_coords=(5,), annotate=False, draw_cross=False)
    testImg = r'{}\\test.png'.format(dataDir)
    img = io.imread(testImg)
    img = color.rgb2gray(img)
    #img = util.invert(img)
    #io.imshow(img)
    #res = morphology.thin(img, 1000)
    startPos = np.array([
        img.shape[0] / 2.0, img.shape[1] / 2.0, img.shape[0] / 3.0,
        img.shape[1] / 2.0, img.shape[0] * (2.0 / 3.0), img.shape[1] / 2.0
    ])
    startPos = np.reshape(startPos, (3, 2))
    #res = segmentation.active_contour(image=img, snake=startPos)
    #res = filters.frangi(img, scale_range=(1, 2), scale_step=0.25, beta1=1, beta2=15)
    res = filters.apply_hysteresis_threshold(img, 0.2, 0.24)
    io.imshow(res)
示例#55
0
def run_sgpmd_demixing(folder):
    # read in motion corrected movie
    t1 = time.time()
    #path = os.path.join(folder, 'sgpmd/output')
    path = '/home/nel/Code/volpy_test/invivo-imaging/test_data/memory_test/403106_3min_10000/output'

    noise = np.squeeze(io.imread(path + '/Sn_image.tif'))
    [nrows, ncols] = noise.shape

    if os.path.isfile(path + '/motion_corrected.tif'):
        mov = io.imread(path + '/motion_corrected.tif').transpose(1, 2, 0)
    elif os.path.isfile(path + '/denoised.tif'):
        mov = io.imread(path + '/denoised.tif')
    else:
        raise ValueError('No valid input file')

    # read in the mask for blood
    if os.path.isfile(path + '/bloodmask.tif'):
        bloodmask = np.squeeze(io.imread(path + '/bloodmask.tif'))
        mov = mov * np.repeat(
            np.expand_dims(noise * bloodmask, 2), mov.shape[2], axis=2)
    else:
        mov = mov * np.repeat(np.expand_dims(noise, 2), mov.shape[2], axis=2)

    mov = -mov

    # display average movie
    print(mov.shape)
    #io.imshow(np.std(mov,axis=2));

    # ## Spatial 2x2 Binning
    movB = mov.reshape(int(mov.shape[0] / 2), 2, int(mov.shape[1] / 2), 2,
                       mov.shape[2])
    movB = np.mean(np.mean(movB, axis=1), axis=2)
    movB.shape

    movB = mov
    # show standard deviation image of binned movie
    io.imshow(np.std(movB, axis=2))

    # %% Load Manually Initialized Background
    bg_flag = os.path.isfile(path + '/ff.tif')

    if bg_flag:
        # import manually initialized background components
        ff_ini = io.imread(path + '/ff.tif')
        fb_ini = io.imread(path + '/fb.tif')

        # bin the spatial components
        fb_ini = fb_ini.reshape(mov.shape[1], mov.shape[0],
                                -1).transpose(1, 0, 2)
        #     fb_ini = fb_ini.reshape(int(fb_ini.shape[0]/2),2,int(fb_ini.shape[1]/2),2,fb_ini.shape[2])
        #     fb_ini = np.mean(np.mean(fb_ini,axis=1),axis=2)

        fb_ini.shape

        # plot manually initialized background components
        plt.figure(figsize=(30, 20))

        for i in range(6):
            plt.subplot(3, 4, 2 * i + 1)
            plt.plot(ff_ini[:2000, i])
            plt.subplot(3, 4, 2 * i + 2)
            io.imshow(fb_ini[:, :, i])

    # %%

    if bg_flag:
        # select which background components to use for initialization
        bkg_components = range(3)

        fb_ini = fb_ini[:, :,
                        bkg_components].reshape(movB.shape[0] * movB.shape[1],
                                                len(bkg_components))
        ff_ini = ff_ini[:, bkg_components]

    # %% Get Cell Spatial Supports from High Pass Filtered Movie

    start = time.time()

    # select which window to demix on
    first_frame = 1
    last_frame = 5000

    movHP = sup.hp_filt_data(movB, spacing=10)

    # %%

    rlt = sup.axon_pipeline_Y(
        movHP[:, :, first_frame:last_frame],
        fb_ini=np.zeros(1),
        ff_ini=np.zeros(1),

        ##### Superpixel parameters
        # thresholding level
        th=[4],

        # correlation threshold for finding superpixels
        # (range around 0.8-0.99)
        cut_off_point=[0.95],

        # minimum pixel count of a superpixel
        # don't need to change these unless cell sizes change
        length_cut=[10],

        # maximum pixel count of a superpixel
        # don't need to change these unless cell sizes change
        length_max=[200],
        patch_size=[30, 30],

        # correlation threshold between superpixels for merging
        # likely don't need to change this
        residual_cut=[np.sqrt(1 - (0.8)**2)],
        pass_num=1,
        bg=False,

        ##### Cell-finding, NMF parameters
        # correlation threshold of pixel with superpixel trace to include pixel in cell
        # (range 0.3-0.6)
        corr_th_fix=0.4,

        # correlation threshold for merging two cells
        # (default 0.8, but likely don't need to change)
        merge_corr_thr=0.8,

        ##### Other options
        # if True, only superpixel analysis run; if False, NMF is also run to find cells
        sup_only=False,

        # the number of superpixels to remove (starting from the dimmest)
        remove=0)

    print("Demixing took: " + str(time.time() - start) + " sec")

    #%% plot pure superpixels
    num_pass = len(rlt["superpixel_rlt"])

    scale = np.maximum(1, (rlt["superpixel_rlt"][0]["connect_mat_1"].shape[1] /
                           rlt["superpixel_rlt"][0]["connect_mat_1"].shape[0]))
    fig = plt.figure(figsize=(4 * scale * num_pass, 4))

    plt.subplot(1, num_pass + 2, 1)
    io.imshow(np.std(movB, axis=2))

    for p in range(num_pass):
        connect_mat_1 = rlt["superpixel_rlt"][p]["connect_mat_1"]
        pure_pix = rlt["superpixel_rlt"][p]["pure_pix"]
        brightness_rank = rlt["superpixel_rlt"][p]["brightness_rank"]
        ax1 = plt.subplot(1, num_pass + 2, p + 2)
        dims = connect_mat_1.shape
        connect_mat_1_pure = connect_mat_1.copy()
        connect_mat_1_pure = connect_mat_1_pure.reshape(np.prod(dims),
                                                        order="F")
        connect_mat_1_pure[~np.in1d(connect_mat_1_pure, pure_pix)] = 0
        connect_mat_1_pure = connect_mat_1_pure.reshape(dims, order="F")

        ax1.imshow(connect_mat_1_pure, cmap="nipy_spectral_r")

        for ii in range(len(pure_pix)):
            pos = np.where(connect_mat_1_pure[:, :] == pure_pix[ii])
            pos0 = pos[0]
            pos1 = pos[1]
            ax1.text((pos1)[np.array(len(pos1) / 3, dtype=int)],
                     (pos0)[np.array(len(pos0) / 3, dtype=int)],
                     f"{brightness_rank[ii]+1}",
                     verticalalignment='bottom',
                     horizontalalignment='right',
                     color='black',
                     fontsize=15)  #, fontweight="bold")

        ax1.set(title="pass " + str(p + 1))
        ax1.title.set_fontsize(15)
        ax1.title.set_fontweight("bold")
        plt.tight_layout()

    # %%plot all cell traces and footprints from NMF
    cell_ct = rlt["fin_rlt"]["c"].shape[1]

    plt.figure(figsize=(25, 3 * cell_ct))

    ref_im = np.std(movB, axis=2).transpose(1, 0)

    for cell_num in range(cell_ct):
        plt.subplot(cell_ct, 2, 2 * cell_num + 1)
        plt.plot(rlt["fin_rlt"]["c"][:, cell_num])
        plt.title(cell_num)

        plt.subplot(cell_ct, 2, 2 * cell_num + 2)
        lower, upper = np.percentile(ref_im.flatten(), [1, 99])
        plt.imshow(ref_im,
                   cmap='gray',
                   interpolation='none',
                   clim=[lower, upper])

        cell_loc = rlt["fin_rlt"]["a"][:, cell_num].reshape(
            movB.shape[1], movB.shape[0])  #.transpose(1,0)
        cell_loc = np.ma.masked_where(cell_loc == 0, cell_loc)
        plt.imshow(cell_loc, cmap='jet', alpha=0.5)

    # %% Get Background Components from Unfiltered Movie
    # rank of background to model, if none selected
    bg_rank = 3
    final_cells = list(range(cell_ct))

    nCells = len(final_cells)

    a = rlt["fin_rlt"]["a"][:, final_cells].copy()
    c = rlt["fin_rlt"]["c"][:, final_cells].copy()
    b = rlt["fin_rlt"]["b"].copy()

    suffix = ''
    io.imsave(path + '/nmf_traces' + suffix + '.tif', c)

    #%%
    dims = movB.shape[:2]
    T = last_frame - first_frame

    movVec = movB.reshape(np.prod(dims), -1, order="F")
    mov_min = movVec.min()
    if mov_min < 0:
        mov_min_pw = movVec.min(axis=1, keepdims=True)
        movVec -= mov_min_pw

    normalize_factor = np.std(movVec, axis=1, keepdims=True) * T

    #%%
    if bg_flag:
        fb = fb_ini
        ff = ff_ini[first_frame:last_frame, :]
        bg_rank = fb.shape[1]
    else:
        bg_comp_pos = np.where(a.sum(axis=1) == 0)[0]
        y_temp = movVec[bg_comp_pos, first_frame:last_frame]
        fb = np.zeros([movVec.shape[0], bg_rank])
        y_temp = y_temp - y_temp.mean(axis=1, keepdims=True)
        svd = TruncatedSVD(n_components=bg_rank, n_iter=7, random_state=0)
        fb[bg_comp_pos, :] = svd.fit_transform(y_temp)
        ff = svd.components_.T
        ff = ff - ff.mean(axis=0, keepdims=True)

    a, c, b, fb, ff, res, corr_img_all_r, num_list = sup.update_AC_bg_l2_Y(
        movVec[:, first_frame:last_frame].copy(),
        normalize_factor,
        a,
        c,
        b,
        ff,
        fb,
        dims,
        corr_th_fix=0.35,
        maxiter=35,
        tol=1e-8,
        merge_corr_thr=0.8,
        merge_overlap_thr=0.8,
        keep_shape=True)

    #%% plot all cell traces and footprints
    cell_ct = c.shape[1]

    plt.figure(figsize=(25, 3 * cell_ct))

    ref_im = np.std(movB, axis=2).transpose(1, 0)

    for cell_num in range(cell_ct):
        plt.subplot(cell_ct, 2, 2 * cell_num + 1)
        plt.plot(c[:, cell_num])
        plt.title(cell_num)

        plt.subplot(cell_ct, 2, 2 * cell_num + 2)
        lower, upper = np.percentile(ref_im.flatten(), [1, 99])
        plt.imshow(ref_im,
                   cmap='gray',
                   interpolation='none',
                   clim=[lower, upper])

        cell_loc = a[:, cell_num].reshape(movB.shape[1],
                                          movB.shape[0])  #.transpose(1,0)
        cell_loc = np.ma.masked_where(cell_loc == 0, cell_loc)
        plt.imshow(cell_loc, cmap='jet', alpha=0.5)
        plt.colorbar()

    # %% plot all background traces and footprints
    bg_rank = fb.shape[1]

    plt.figure(figsize=(25, 3 * bg_rank))

    for bkgd_num in range(bg_rank):
        plt.subplot(bg_rank, 2, 2 * bkgd_num + 1)
        plt.plot(ff[:, bkgd_num])

        bkgd_comp = fb[:,
                       bkgd_num].reshape(movB.shape[1::-1])  #.transpose(1,0)
        plt.subplot(bg_rank, 2, 2 * bkgd_num + 2)
        plt.imshow(bkgd_comp)
        plt.colorbar()

    def tv_norm(image):
        return np.sum(np.abs(image[:, :-1] - image[:, 1:])) + np.sum(
            np.abs(image[:-1, :] - image[1:, :]))

    Y = movB.transpose(1, 0, 2).reshape(movB.shape[0] * movB.shape[1],
                                        movB.shape[2])
    X = np.hstack((a, fb))
    X = X / np.ptp(X, axis=0)
    X2 = np.zeros((X.shape[0], nCells + bg_rank))
    X2[:, :nCells] = X[:, :nCells]

    plt.figure(figsize=(25, 3 * bg_rank))
    plt.title('New Background Components')

    lr = 0.001
    maxIters = 1000

    for b in range(bg_rank):
        bg_im = X[:, -(b + 1)].reshape(movB.shape[-2::-1])

        plt.subplot(bg_rank, 2, (bg_rank - b) * 2 - 1)
        plt.imshow(bg_im)
        plt.title(str(tv_norm(bg_im)))
        plt.colorbar()

        weights = torch.zeros((nCells, 1),
                              requires_grad=True,
                              dtype=torch.double)

        image = torch.from_numpy(bg_im)

        for idx in range(maxIters):
            test_im = image - torch.reshape(
                torch.from_numpy(X[:, :nCells]) @ weights, movB.shape[-2::-1])
            tv = torch.sum(
                torch.abs(test_im[:, :-1] - test_im[:, 1:])) + torch.sum(
                    torch.abs(test_im[:-1, :] - test_im[1:, :]))

            tv.backward()

            with torch.no_grad():
                weights -= lr * weights.grad

            weights.grad.zero_()

        opt_weights = weights.data.numpy()

        X2[:, -(b + 1)] = np.maximum(
            X[:, -(b + 1)] - np.squeeze(X[:, :nCells] @ opt_weights), 0)

        plt.subplot(bg_rank, 2, (bg_rank - b) * 2)
        plt.imshow(X2[:, -(b + 1)].reshape(movB.shape[-2::-1]), vmin=0, vmax=1)
        plt.title(str(tv_norm(X2[:, -(b + 1)].reshape(movB.shape[-2::-1]).T)))
        plt.colorbar()

    # %% Get Final Traces
    beta_hat2 = np.linalg.lstsq(X2, Y)[0]
    res = np.mean(np.square(Y - X2 @ beta_hat2), axis=0)

    # %% Visualizations
    num_traces = beta_hat2.shape[0]
    plt.figure(figsize=(25, 3 * num_traces))
    ref_im = np.std(movB, axis=2).transpose(1, 0)

    for idx in range(num_traces):
        plt.subplot(num_traces, 2, 2 * idx + 1)
        plt.plot(beta_hat2[idx, :])

        plt.subplot(num_traces, 2, 2 * idx + 2)
        lower, upper = np.percentile(ref_im.flatten(), [1, 99])
        plt.imshow(ref_im,
                   cmap='gray',
                   interpolation='none',
                   clim=[lower, upper])

        cell_loc = X2[:, idx].reshape(movB.shape[1::-1])  #.transpose(1,0)
        cell_loc = np.ma.masked_where(abs(cell_loc) < 1e-8, cell_loc)
        plt.imshow(cell_loc, cmap='jet', alpha=0.5)

    # %% Save Results
    t2 = time.time()
    suffix = ''

    io.imsave(path + '/spatial_footprints' + suffix + '.tif', X2)
    io.imsave(path + '/cell_spatial_footprints' + suffix + '.tif',
              X2[:, :nCells])
    io.imsave(path + '/temporal_traces' + suffix + '.tif', beta_hat2)
    io.imsave(path + '/cell_traces' + suffix + '.tif', beta_hat2[:nCells, :])
    io.imsave(path + '/residual_var' + suffix + '.tif', res)

    cell_locations = center_of_mass(X2[:,
                                       0].reshape(movB.shape[1::-1]).transpose(
                                           1, 0))
    for idx in range(nCells - 1):
        cell_locations = np.vstack((cell_locations,
                                    center_of_mass(X2[:, idx + 1].reshape(
                                        movB.shape[1::-1]).transpose(1, 0))))
    io.imsave(path + '/cell_locations' + suffix + '.tif',
              np.array(cell_locations))

    if nCells > 1:
        io.imsave(
            path + '/cell_demixing_matrix' + suffix + '.tif',
            np.linalg.inv(np.array(X2[:, :nCells].T @ X2[:, :nCells]))
            @ X2[:, :nCells].T)
    print('Saved!')
示例#56
0
preds_val_t = (preds_val > 0.5).astype(np.uint8)
preds_test_t = (preds_test > 0.5).astype(np.uint8)

# Create list of upsampled test masks
preds_test_upsampled = []
for i in range(len(preds_test)):
    preds_test_upsampled.append(
        resize(np.squeeze(preds_test[i]), (sizes_test[i][0], sizes_test[i][1]),
               mode='constant',
               preserve_range=True))

# In[ ]:

# Perform a sanity check on some random training samples
ix = random.randint(0, len(preds_train_t))
imshow(X_train[ix])
plt.show()
imshow(np.squeeze(Y_train[ix]))
plt.show()
imshow(np.squeeze(preds_train_t[ix]))
plt.show()

# In[ ]:

# Perform a sanity check on some random validation samples
ix = random.randint(0, len(preds_val_t))
imshow(X_train[int(X_train.shape[0] * 0.9):][ix])
plt.show()
imshow(np.squeeze(Y_train[int(Y_train.shape[0] * 0.9):][ix]))
plt.show()
imshow(np.squeeze(preds_val_t[ix]))
示例#57
0
# %%
import numpy as np
from skimage import io

img = io.imread('flowers.jpg')
io.imshow(img)

# %%
img[250][1500]

# %%
for i in range(500):
    for j in range(1000):
        img[i][j][0] = 51
        img[i][j][1] = 0
        img[i][j][2] = 255
    for k in range(1000, 2000):
        img[i][k][0] = 204
        img[i][k][1] = 255
        img[i][k][2] = 51
    for l in range(2000, 3000):
        img[i][l][0] = 255
        img[i][l][1] = 255
        img[i][l][2] = 255

io.imshow(img)

# %%
img = io.imread('flowers.jpg')
io.imshow(img)
示例#58
0
    #all of the align functions call cm.multi_iteration_offset, which in turn calls cm.find_offset_by_subtraction
    ag = cm.align(g, b)
    ar = cm.align(r, b)

    #uncomment to use edges for alignment (Canny)
    #    ag = cm.align_edges(g, b)
    #    ar = cm.align_edges(r, b)

    #uncomment to use gradient for alignment (Sobel)
    #    ag = cm.align_gradient(g, b)
    #    ar = cm.align_gradient(r, b)

    #create color image
    img_out = np.dstack([ar, ag, b])
    img_orig = np.dstack([r, g, b])

    img_out = cm.remove_border(img_out, 0.07, 10, 4)
    img_out = cm.image_correct(img_out)

    #uncomment to view original image
    #    skio.imshow(img_orig)
    #    skio.show()

    skio.imshow(img_out)
    skio.show()

    #uncomment to save image
#    name = os.path.basename(name)
#    name = os.path.splitext(name)[0]
#    skio.imsave("out_" + name + ".jpg", img_out)
示例#59
0
    img = imread(path + '/images/' + id_ + '.png')[:,:,:IMG_CHANNELS]
    img = resize(img, (IMG_HEIGHT, IMG_WIDTH), mode='constant', preserve_range=True)
    X_train[n] = img
    mask = np.zeros((IMG_HEIGHT, IMG_WIDTH, 1), dtype=np.bool)
    for mask_file in next(os.walk(path + '/masks/'))[2]:
        mask_ = imread(path + '/masks/' + mask_file)
        mask_ = np.expand_dims(resize(mask_, (IMG_HEIGHT, IMG_WIDTH), mode='constant', 
                                      preserve_range=True), axis=-1)
        mask = np.maximum(mask, mask_)
    Y_train[n] = mask

"""Check if the image is resized and is viewable (both mask and image for training)"""

#Check if they are parsed right 
ix = np.random.randint(0, len(train_ids))
imshow(X_train[ix])
plt.show()
imshow(np.squeeze(Y_train[ix]))
plt.show()

"""# Trying UNet"""

from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline

from keras.models import Sequential, Model
from keras.wrappers.scikit_learn import KerasClassifier
from keras.layers import Input, concatenate, Conv2D, MaxPool2D, UpSampling2D, Dropout, Cropping2D
from keras.utils import to_categorical
from keras.layers.core import Dropout, Lambda
示例#60
0
    #sobel_gradient_threshold=0.05
    #size threshold=100
    shape = np.shape(image)
    image1 = preprocessing_for_icefinder(image)
    image2 = minimum_filter(image1, 4, mode='reflect')
    sobel1 = sobel(image2)
    image3 = sobel1 > sobel_gradient_threshold
    image4 = binary_fill_holes(mh.morph.dilate(image3))
    #label and clear the image
    labeled, n_nucleus = mh.label(image4)
    sizes = mh.labeled.labeled_size(labeled)
    too_small = np.where(sizes < size_threshold)
    labeled = mh.labeled.remove_regions(labeled, too_small)
    relabeled, n_left = mh.labeled.relabel(labeled)
    if n_left > 20:
        print("can not locate ice in the image")
    else:
        image5 = relabeled > 0  #make the binary image
        image6 = resize(image5,
                        shape,
                        order=2,
                        mode='symmetric',
                        preserve_range=True)
        return image6


img = mrcfile.open('ice11.mrc').data
img2 = find_ice(img, 0.05, 100)
imshow(img2)
show()