def featurePoints(greyscaleImg, mask): surf_keypoints, surf_descriptors = cv.ExtractSURF(greyscaleImg, mask, cv.CreateMemStorage(), (1, 3000, 3, 4)) img2 = np.array(img) for ((x, y), laplacian, size, dir, hessian) in surf_keypoints: if laplacian == -1: img2[int(y), int(x), 0] = 255 img2[int(y), int(x), 1] = 0 img2[int(y), int(x), 2] = 0 else: img2[int(y), int(x), 0] = 0 img2[int(y), int(x), 1] = 0 img2[int(y), int(x), 2] = 255 imsave('/home/cplab/workspace/imageex/src/imageex/static/SURFFFFFF.png', img2) #params = (maxSize, responseThreshold, lineThresholdProjected, lineThresholdBinarized, suppressNonmaxSize) star_keypoints = cv.GetStarKeypoints(greyscaleImg, cv.CreateMemStorage(), (8, 30, 10, 8, 3)) img3 = np.array(img) for ((x, y), size, response) in star_keypoints: if response >= 0: img3[int(y), int(x), 0] = 255 img3[int(y), int(x), 1] = 0 img3[int(y), int(x), 2] = 0 else: img3[int(y), int(x), 0] = 0 img3[int(y), int(x), 1] = 0 img3[int(y), int(x), 2] = 255 imsave('/home/cplab/workspace/imageex/src/imageex/static/STARRRRR.png', img3) return []
def save_array(self, root, template): # 1. iterate through planes in bulk # 2. for each plane, save plane based on root, template # 3. create path with url and add to gon if not os.path.exists(root): os.makedirs(root) file_name = template.rv.format(self.experiment.name, self.series.name, self.channel.name, str_value(self.t, self.series.ts), '{}') url = os.path.join(root, file_name) if len(self.array.shape)==2: imsave(url.format(str_value(self.z, self.series.zs)), self.array) self.paths.create(composite=self.composite if self.composite is not None else self.gon.composite, channel=self.channel, template=template, url=url.format(str_value(self.z, self.series.zs)), file_name=file_name.format(str_value(self.z, self.series.zs)), t=self.t, z=self.z) else: for z in range(self.array.shape[2]): plane = self.array[:,:,z].copy() imsave(url.format(str_value(z+self.z, self.series.zs)), plane) # z level is offset by that of original gon. self.paths.create(composite=self.composite, channel=self.channel, template=template, url=url.format(str_value(self.z, self.series.zs)), file_name=file_name.format(str_value(self.z, self.series.zs)), t=self.t, z=z+self.z) # create gons gon = self.gons.create(experiment=self.composite.experiment, series=self.composite.series, channel=self.channel, template=template) gon.set_origin(self.r, self.c, z, self.t) gon.set_extent(self.rs, self.cs, 1) gon.array = plane.copy().squeeze() gon.save_array(self.experiment.composite_path, template) gon.save()
def action_save(self, info): # First make a copy of the frame we will save save_frame = info.object.camera.frame.copy() # Then find out where to save it dialog = FileDialog(parent=info.ui.control, action='save as', modal=True, title='Save Image') try: dialog.default_directory = info.object._current_folder except TraitError: pass # thrown if _current_folder is None dialog.open() path = dialog.path # Store the directory for the next time info.object._current_folder = dialog.directory if dialog.return_code != OK: return # Default is PNG if '.' not in path: path += '.png' # Save it pilutil.imsave(path, save_frame)
def crop_and_resize(input_image, outdir): # detect face -> crop -> resize -> save im = cv2.imread(input_image) im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB) faces = faceCascade.detectMultiScale(im, scaleFactor=1.5, minNeighbors=5, minSize=(30, 30)) face_color = None for (x, y, w, h) in faces: face_color = im[y:y + h, x:x + w] try: small = cv2.resize(face_color, (64, 64)) file_name = input_image.split('\\')[-1] imsave("{}/{}".format(outdir, file_name), small) except Exception: # if face is not detected im = imread(input_image) height, width, color = im.shape edge_h = int(round((height - 108) / 2.0)) edge_w = int(round((width - 108) / 2.0)) cropped = im[edge_h:(edge_h + 108), edge_w:(edge_w + 108)] small = imresize(cropped, (64, 64)) file_name = input_image.split('\\')[-1] imsave("{}/{}".format(outdir, file_name), small)
def featurePoints(greyscaleImg, mask): surf_keypoints, surf_descriptors = cv.ExtractSURF(greyscaleImg, mask, cv.CreateMemStorage(), (1, 3000, 3, 4)) img2 = np.array(img) for ((x, y), laplacian, size, dir, hessian) in surf_keypoints: if laplacian == -1: img2[int(y),int(x), 0] = 255 img2[int(y),int(x), 1] = 0 img2[int(y),int(x), 2] = 0 else: img2[int(y),int(x), 0] = 0 img2[int(y),int(x), 1] = 0 img2[int(y),int(x), 2] = 255 imsave('/home/cplab/workspace/imageex/src/imageex/static/SURFFFFFF.png', img2) #params = (maxSize, responseThreshold, lineThresholdProjected, lineThresholdBinarized, suppressNonmaxSize) star_keypoints = cv.GetStarKeypoints(greyscaleImg, cv.CreateMemStorage(), (8, 30, 10, 8, 3)) img3 = np.array(img) for ((x, y), size, response) in star_keypoints: if response >= 0: img3[int(y),int(x), 0] = 255 img3[int(y),int(x), 1] = 0 img3[int(y),int(x), 2] = 0 else: img3[int(y),int(x), 0] = 0 img3[int(y),int(x), 1] = 0 img3[int(y),int(x), 2] = 255 imsave('/home/cplab/workspace/imageex/src/imageex/static/STARRRRR.png', img3) return []
def task3(): nasa_matrix = normalize_intensity(imread(NASA)) flatfield_matrix = normalize_intensity(imread(FLATFIELD)) corrected_image = correct_with_flatfield(nasa_matrix, flatfield_matrix) output_path = os.path.join(OUTPUT_DIR, os.path.split(NASA)[-1]) imsave(output_path, corrected_image) pl.imshow(corrected_image, cmap=cm.Greys_r) pl.show()
def task5(): img_mat = np.asarray(normalize_intensity(lena())) outputs = dict() for param in (0.1, 0.3, 0.8): outputs[param] = salt_and_pepper_noise(img_mat, param) file_name = 'noisy_{}.png'.format(str(param).replace('.', '_')) imsave(os.path.join(OUTPUT_DIR, file_name), outputs[param]) pl.imshow(outputs[0.1], cmap=cm.Greys_r) pl.show()
def task2_2(): print('2.2') img = normalize_intensity(imread(CAMERAMAN)) gaussian = gaussian_noise(img, mean=0, std=0.1) peppered = salt_and_pepper_noise(img, density=0.25) output_path = os.path.join(OUTPUT_DIR, "2_2_gauss_med_" + os.path.split(CAMERAMAN)[-1]) imsave(output_path, median_mask(gaussian, filter_size=5)) output_path = os.path.join(OUTPUT_DIR, "2_2_pepper_med_" + os.path.split(CAMERAMAN)[-1]) imsave(output_path, median_mask(peppered, filter_size=5))
def task4_1(): einstein_matrix = imread(EINSTEIN) corrected = gamma_correct(einstein_matrix, gamma=1.5) output_path = os.path.join(OUTPUT_DIR, "gamma_" + os.path.split(EINSTEIN)[-1]) imsave(output_path, corrected) stretched = stretch_range(einstein_matrix) corrected = gamma_correct(stretched, gamma=1.5) output_path = os.path.join(OUTPUT_DIR, "stretched_gamma_" + os.path.split(EINSTEIN)[-1]) imsave(output_path, corrected)
def task3_1(): print('3.1') c_img = normalize_intensity(imread(CAMERAMAN)) fft_res = fft2(a=c_img) output_path = os.path.join(OUTPUT_DIR, "3_1_fft_spectrum_" + os.path.split(CAMERAMAN)[-1]) imsave(output_path, log(1 + abs(fftshift(fft_res)))) b_img = normalize_intensity(imread(BRICKS)) fft_res = fft2(a=b_img) output_path = os.path.join(OUTPUT_DIR, "3_1_fft_spectrum_" + os.path.split(BRICKS)[-1]) imsave(output_path, log(1 + abs(fftshift(fft_res))))
def get_gnf(): imgs = pyslic.image.io.readtjz_recursive( 'raw-data/2007-07-24_Liso_vs._Mito/G7509546/') imgs.sort(key=lambda I: I.id) random.seed(0) random.shuffle(imgs) outputdir = './dna-images/gnf/' maybe_mkdir(outputdir) for i, img in enumerate(imgs[:50]): dna = img.get('dna') imsave('%s/dna-%s.png' % (outputdir, i), as_colourimg(dna))
def detect(model, dataset_dir, subset): """Run detection on images in the given directory.""" print("Running on {}".format(dataset_dir)) # Create directory if not os.path.exists(RESULTS_DIR): os.makedirs(RESULTS_DIR) submit_dir = "submit_{:%Y%m%dT%H%M%S}".format(datetime.datetime.now()) submit_dir = os.path.join(RESULTS_DIR, submit_dir) os.makedirs(submit_dir) # Read dataset dataset = NucleusDataset() dataset.load_nucleus(dataset_dir, subset) dataset.prepare() # Load over images submission = [] for image_id in dataset.image_ids: # Load image and run detection mask = np.zeros((512, 512), dtype='bool') image = dataset.load_image(image_id) # Detect objects r = model.detect([image], verbose=0)[0] # Encode image to RLE. Returns a string of multiple lines source_id = dataset.image_info[image_id]["id"] rle = mask_to_rle(source_id, r["masks"], r["scores"]) submission.append(rle) # Save image with masks visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'], "CELL", r['scores'], show_bbox=False, show_mask=True, title="Predictions") plt.savefig("{}/{}_mask.png".format( submit_dir, dataset.image_info[image_id]["id"])) out = r['masks'] r, c, num = out.shape print(num) for k in range(0, num): mask = mask + out[:, :, k] imsave( "{}/{}.png".format(submit_dir, dataset.image_info[image_id]["id"]), np.uint8(mask) * 255) # Save to csv file submission = "ImageId,EncodedPixels\n" + "\n".join(submission) file_path = os.path.join(submit_dir, "submit.csv") with open(file_path, "w") as f: f.write(submission) print("Saved to ", submit_dir)
def plotRecon(layernames, dataDir, skipFrames, startFrames=0, scale=True, outputDir=None, suffix=""): if outputDir == None: outputDir = dataDir reconDir = outputDir + "Recon/" if not os.path.exists(reconDir): os.makedirs(reconDir) #Open file for layername in layernames: pvpFile = open(dataDir + layername + ".pvp", 'rb') #Grab header header = readHeaderFile(pvpFile) shape = (header["ny"], header["nx"], header["nf"]) numPerFrame = shape[0] * shape[1] * shape[2] #Read until start frames pvpFile.seek((numPerFrame * 4 + 8) * startFrames, 1) (idx, mat) = readData(pvpFile, shape, numPerFrame) #if idx != -1: # #Read until errors out (EOF) # for i in range(startFrames): # (idx, mat) = readData(pvpFile, shape, numPerFrame) # if(idx == -1): # break #While not eof while idx != -1: print(layername + ": " + str(int(idx[0]))) #color bands if header["nf"] == 1 or header["nf"] == 3: if (scale): img = scaleMat(mat) else: img = (np.uint8)(mat.squeeze() * 256) else: img = matToImage(mat) imsave(reconDir + layername + str(int(idx[0])) + suffix + ".png", img) #Read a few extra for skipping frames for i in range(skipFrames): (idx, mat) = readData(pvpFile, shape, numPerFrame) if (idx == -1): break pvpFile.close()
def nearest_neighbor_field(first_image , first_image_weights , second_image , second_image_weights , nnf , difference_function=default_patch_difference , patch_size=(1 , 1) , resize_steps=0): patch_difference_threshold = 10 print patch_size orig_first_image_shape = first_image.shape orig_nnf_shape = nnf.shape #second_image = imresize(second_image , second_image.shape[0] / 8 , second_image.shape[1] / 8) resize_amounts = (1 , 2 , 4 , 8 , 16) test = update_image(nnf , first_image , second_image , patch_size); imsave('orig.tif' , test) diff = second_image.copy() diff[90:210 , 290:462] = diff[90:210 , 290:462] - test print diff[90:210 , 290:462] imsave('diff.tif' , diff) for i in resize_amounts[0:resize_steps+1]: #first_image = imresize(first_image , ((orig_first_image_shape[0] / resize_amounts[resize_steps]) * i , (orig_first_image_shape[1] / resize_amounts[resize_steps]) * i)) #nnf = resize(nnf , ((orig_nnf_shape[0] / resize_amounts[resize_steps]) * i, (orig_nnf_shape[1] / resize_amounts[resize_steps]) * i , 2)) for j in range(7): nnf_rgb = zeros((nnf.shape[0] , nnf.shape[1] , 3)) nnf_rgb[: , : , 0] = nnf[: , : , 0] nnf_rgb[: , : , 1] = nnf[: , : , 1] imsave('nnf_' + str(j) + '_' + str(i) + '.tif' , nnf_rgb) (nnf , first_image_weights) = update_nearest_neighbor_field(first_image , first_image_weights , second_image , second_image_weights , nnf , difference_function , patch_size) imsave('image_weights_' + str(j) + '.tif' , first_image_weights) first_image = update_image(nnf ,first_image , second_image , patch_size) imsave('fixed_' + str(j) + '_' + str(i) + '.tif' , first_image) print j return first_image
def task3_2(): print('3.2') low_pass = normalize_intensity(imread(LOW_PASS)) high_pass = normalize_intensity(imread(HIGH_PASS)) img_freq_dom = fft2(a=normalize_intensity(imread(BRICKS_2))) apply_low_pass = img_freq_dom * low_pass ifft_res = ifft2(a=apply_low_pass) output_path = os.path.join(OUTPUT_DIR, "3_2_low_pass_" + os.path.split(BRICKS_2)[-1]) imsave(output_path, abs(ifft_res)) apply_high_pass = img_freq_dom * high_pass ifft_res = ifft2(a=apply_high_pass) output_path = os.path.join(OUTPUT_DIR, "3_2_high_pass_" + os.path.split(BRICKS_2)[-1]) imsave(output_path, abs(ifft_res))
def ensemble_image(files, dirs, ensembling_dir, strategy): for file in files: images = [] for dir in dirs: file_path = os.path.join(dir, file) if os.path.exists(file_path): images.append(imread(file_path, mode='L')) images = np.array(images) if strategy == 'average': ensembled = average_strategy(images) elif strategy == 'hard_voting': ensembled = hard_voting(images) else: raise ValueError('Unknown ensembling strategy') imsave(os.path.join(ensembling_dir, file), ensembled)
def get_ksr(): imgs = pyslic.image.io.read_ksr_dir('raw-data/080518MFJLX_CC1KSR/') F = pyslic.preprocess.preprocess_collection( imgs, pyslic.preprocess.FixIlluminationHVRadialGradient('dna')) imgs.sort(key=lambda x: x.id) L = 100 idx = 0 outputdir = 'dna-images/ksr' maybe_mkdir(outputdir) for img in imgs: with pyslic.image.loadedimage(img): F.process(img) dna = img.get('dna') if (dna > L * 3).sum() > 100: imsave('%s/dna-%s.png' % (outputdir, idx), dna) idx += 1 if idx == 50: break
def plotRecon(layernames, dataDir, skipFrames, startFrames=0, scale=True, outputDir=None, suffix=""): if outputDir == None: outputDir = dataDir reconDir = outputDir + "Recon/" if not os.path.exists(reconDir): os.makedirs(reconDir) #Open file for layername in layernames: pvpFile = open(dataDir + layername + ".pvp", 'rb') #Grab header header = readHeaderFile(pvpFile) shape = (header["ny"], header["nx"], header["nf"]) numPerFrame = shape[0] * shape[1] * shape[2] #Read until start frames pvpFile.seek((numPerFrame*4+8)*startFrames,1) (idx, mat) = readData(pvpFile, shape, numPerFrame) #if idx != -1: # #Read until errors out (EOF) # for i in range(startFrames): # (idx, mat) = readData(pvpFile, shape, numPerFrame) # if(idx == -1): # break #While not eof while idx != -1: print(layername + ": " + str(int(idx[0]))) #color bands if header["nf"] == 1 or header["nf"] == 3: if(scale): img = scaleMat(mat) else: img = (np.uint8)(mat.squeeze()*256) else: img = matToImage(mat) imsave(reconDir + layername + str(int(idx[0])) + suffix + ".png", img) #Read a few extra for skipping frames for i in range(skipFrames): (idx, mat) = readData(pvpFile, shape, numPerFrame) if(idx == -1): break pvpFile.close()
def task2_4(): img = normalize_intensity(imread(CAMERAMAN)) img = img[30:95, [i for i in range(80, 160)]] # select subsection of image vel_x, vel_y = gradient(f=img) magn_img = gaussian_gradient_magnitude(img, 3) output_path = os.path.join(OUTPUT_DIR, "2_4_gradien_magnitude_" + os.path.split(CAMERAMAN)[-1]) imsave(output_path, magn_img) dim_x, dim_y = len(img[0]), len(img) x, y = range(dim_x), range(dim_y) x, y = meshgrid(x, y) plt.figure() imgplot = plt.imshow(img) imgplot.set_cmap('gray') plt.ylim(dim_y, 0) plt.quiver(x, y, vel_x, vel_y, pivot='middle') plt.show()
def download(self, lon, lat, step=False): """ given the list of coordinates, it downloads the corresponding satellite images. Notes: Google Maps Static API takes {latitude,longitude} Args: lon (list): list of longitudes. lat (list): list of latitudes. step (bool): if you want to add buffer images. SMore accurate but slow. """ if step: print('INFO: adding steps to coordinates set.') lon, lat = self.add_steps(lon, lat) @retry(Exception, tries=4) def _urlopen_with_retry(url): return urlopen(url).read() _cnt, _total = 0, len(lon) # counter and total number of images. for i, j in zip(lon, lat): print("INFO: {} images downloaded out of {}".format(_cnt, _total), end='\r') _cnt += 1 file_name = str(i) + '_' + str(j) + '_' + str(ZOOM_LEVEL) + '.jpg' if os.path.exists(self.directory + file_name): print("INFO: {} already downloaded".format(file_name), end='\r') else: center_point = str(j) + "," + str(i) url = """https://maps.googleapis.com/maps/api/staticmap?center={}&zoom={}&size={}&maptype={}&key={}""".\ format(center_point, ZOOM_LEVEL, map_size, imagery_set, os.environ['Google_key']) buffer = BytesIO(_urlopen_with_retry(url)) image = imread(buffer, mode='RGB') if (image[:, :, 0] == 245).sum() >= 100000: # Gray image in Bing print("No image in Bing API", file_name) else: print('file path: ', os.path.join(self.directory, file_name)) imsave(os.path.join(self.directory, file_name), image[50:450, :, :])
def get_ic100(): imgs = pyslic.image.io.read_ic100dir('raw-data/080328MFLPC_BV1/') wells = ['C2', 'B2'] # These are two control wells: more cells. selected = [img for img in imgs if img.label in wells] def imgkey(I): w, c = I.id idx = (wells.index(w) if w in wells else len(wells)) return idx, c if w in wells: return wells.index(w), c return len(wells), c selected.sort(key=imgkey) outputdir = 'dna-images/ic100' maybe_mkdir(outputdir) for idx, img in enumerate(selected[:50]): imsave('%s/dna-%s.png' % (outputdir, idx), as_colourimg(img.get('dna')))
def testImages(files1, name1, name2): L = len(files1) X = [] Y = [] # bndry = misc.imread('Offsetmask.png').astype('float32') #filePath = 'membrane/morseUpdate/' for f1 in files1: print(f1) img = imread(name1 + "/" + f1) print (img.max()) # print max(max(row) for row in img) img = img.astype('float32') dm = imread(name2 + "/" + f1).astype('float32') img = img / 255. dm = dm / 255. print(img.max(), dm.max()) # if img.max(): # img = img / img.max() # dm = dm / 255. # print dm.max(), img.max() # org = misc.imread(name3 + "/" + f1[:-8] + '.tif').astype('float32') # if org.max(): # org = org / org.max() X_arr = np.asarray(img) X_arr = X_arr[..., np.newaxis] X_arr = X_arr[np.newaxis, ...] Y_arr = np.asarray(dm) Y_arr = Y_arr[..., np.newaxis] Y_arr = Y_arr[np.newaxis, ...] # P_arr = np.asarray(org) # P_arr = P_arr[..., np.newaxis] # P_arr = P_arr[np.newaxis, ...] # print model.summary() out_img = model.predict([X_arr, Y_arr]) # out_img = sigmoid(out_img) # print(out_img.min(), out_img.max()) img = np.squeeze(out_img[0]) * 255. #* 100000. print(img.min(), img.max()) imsave("tosamik/red_count/dmpp/" + f1, img.astype('uint8'))
def export_subject(subject, folder): logging.debug(f"Exporting subject {subject.subject_id}") cdr_indices = list(zip(*subject.get_mri_cdr_offset_indices())) if cdr_indices.count((-1, -1)) == len(cdr_indices): logging.warning(f"Subject {subject.subject} has no CDR data, skipping") for mri_index, mri_data in enumerate(subject.mri_data): cdr = calc_cdr(mri_data, cdr_indices[mri_index], subject.cdr_data) if cdr == -1: logging.warning( f"Runs from {subject.subject_id} d{mri_data.day} has no valid CDR data" ) continue for i, filename in enumerate(mri_data.filenames): path = folder + f"{cdr}/{subject.subject_id}_{mri_index}_run{i}.png" if not os.path.isfile(path): img = load_image(filename + ".nii.gz") plane = get_single_plane(img) if plane is None: logging.warning( f"Skipping exporting wrongly rotated image {path}") else: pilutil.imsave(path, get_single_plane(img))
def create_training_data(): print('Converting Training video images to numpy array ...') for index in INDEX: index = str(index) print(index) os.mkdir(input + '/' + index) path_train = TR_IMG_DIR + index + '/' # path_train = os.path.join(TR_IMG_DIR1, index) training_data = [] for img in tqdm(os.listdir(path_train)): if img == 'saliency': continue img_array = cv2.imread(os.path.join(path_train, img), cv2.COLOR_RGB2BGR) new_array = cv2.resize(img_array, IMG_SIZE).astype(float) # im = np.expand_dims(new_array, axis=0) # a = model.predict(im) training_data.append([new_array]) training_data = np.array(training_data).reshape(-1, 224, 224, 3) s = feature_map_function(training_data) del training_data X = Batch_Creation(s) X = np.array(X) del s for i in range(0, len(X), 1): Predictions = modelsal.predict(X[i:i + 1, :, :, :, :]) p = Predictions[0][0] s = np.array(p[:, :, 0]) imsave(input + index + '/' + str(format(i + 1, '04')) + '.jpg', s) print(i) gc.collect() print('Converting Training video images to numpy array ok ...')
def task2_1(): print('2.1') img = normalize_intensity(imread(CAMERAMAN)) gaussian = gaussian_noise(img, mean=0, std=0.1) output_path = os.path.join(OUTPUT_DIR, "2_1_gauss_" + os.path.split(CAMERAMAN)[-1]) imsave(output_path, gaussian) peppered = salt_and_pepper_noise(img, density=0.25) output_path = os.path.join(OUTPUT_DIR, "2_1_pepper_" + os.path.split(CAMERAMAN)[-1]) imsave(output_path, peppered) output_path = os.path.join(OUTPUT_DIR, "2_1_gauss_avg_" + os.path.split(CAMERAMAN)[-1]) imsave(output_path, averaging_mask(gaussian, filter_size=5)) output_path = os.path.join(OUTPUT_DIR, "2_1_pepper_avg_" + os.path.split(CAMERAMAN)[-1]) imsave(output_path, averaging_mask(peppered, filter_size=5))
def gen_hvs_mask(image): edge = edge_mask(im) * 255 print edge imsave("edge.tif", edge) texture = generic_filter(im, texture_mask, (3, 3)) print texture imsave("texture.tif", texture) # luminance = zeros(im.shape) luminance = generic_filter(im, luminance_mask, (5, 5)) # luminance = imread("luminance.tif") # luminance = luminance_mask(im) print luminance imsave("luminance.tif", luminance) hvs = hvs_mask(edge, texture, luminance) print hvs imsave("hvs.tif", hvs) return hvs
def find_PCAKmeans(imagepath1, imagepath2): print("Operating") image1 = imread(imagepath1) image2 = imread(imagepath2) image1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY) image2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY) # print(image1.shape,image2.shape) new_size = np.asarray(image1.shape) / 5 new_size = new_size.astype(np.int) * 5 image1 = imresize(image1, (new_size)).astype(np.int16) image2 = imresize(image2, (new_size)).astype(np.int16) diff_image = np.abs(image1 - image2) imsave("diff_%s" % imagepath1.split("/")[1], diff_image) print("\nBoth images resized to ", new_size) vector_set, mean_vec = find_vector_set(diff_image, new_size) pca = PCA() pca.fit(vector_set) EVS = pca.components_ FVS = find_FVS(EVS, diff_image, mean_vec, new_size) print("\ncomputing k means") components = 3 least_index, change_map = clustering(FVS, components, new_size) change_map[change_map == least_index] = 255 change_map[change_map != 255] = 0 change_map = change_map.astype(np.uint8) kernel = np.asarray( ( (0, 0, 1, 0, 0), (0, 1, 1, 1, 0), (1, 1, 1, 1, 1), (0, 1, 1, 1, 0), (0, 0, 1, 0, 0), ), dtype=np.uint8, ) cleanChangeMap = cv2.erode(change_map, kernel) imsave("changemap_%s" % imagepath1.split("/")[1], change_map) imsave("cleanchangemap_%s" % imagepath1.split("/")[1], cleanChangeMap)
def task2_3(): print('2.3') img = normalize_intensity(imread(BRICKS)) a_img = alias(img, 4) output_path = os.path.join(OUTPUT_DIR, "2_3_downscaled_no_filter_" + os.path.split(BRICKS)[-1]) imsave(output_path, a_img) gfilter_img = gauss_filter(img, 0.8) agf_img = alias(gfilter_img, 4) output_path = os.path.join(OUTPUT_DIR, "2_3_gauss_8_" + os.path.split(BRICKS)[-1]) imsave(output_path, agf_img) gfilter_img = gauss_filter(img, 0.4) agf_img = alias(gfilter_img, 4) output_path = os.path.join(OUTPUT_DIR, "2_3_gauss_4_" + os.path.split(BRICKS)[-1]) imsave(output_path, agf_img)
def save_predict_output(estimator, predict_input): folder_count_celeba = 0 image_count = 0 images_in_folder = 300 predicted_images_path = "data/sfsnet_inference/" current_folder_path = "" if not os.path.exists(predicted_images_path): os.makedirs(predicted_images_path) else: shutil.rmtree(predicted_images_path) os.makedirs(predicted_images_path) for i in estimator.predict(input_fn=predict_input): if (image_count % images_in_folder == 0): folder_count_celeba = folder_count_celeba + 1 current_folder_path = predicted_images_path + '/' + str( folder_count_celeba).zfill(4) os.makedirs(current_folder_path) img_path = current_folder_path + '/' + str(image_count).zfill( 6) + '_img.png' mask_path = current_folder_path + '/' + str(image_count).zfill( 6) + '_mask.png' normal_path = current_folder_path + '/' + str(image_count).zfill( 6) + '_normal.png' albedo_path = current_folder_path + '/' + str(image_count).zfill( 6) + '_albedo.png' light_path = current_folder_path + '/' + str(image_count).zfill( 6) + '_light.txt' imsave(img_path, i["image"]) imsave(mask_path, i["mask"]) imsave(normal_path, i["normal"]) imsave(albedo_path, i["albedo"]) light_array = '\t'.join(str(e) for e in i["light"]) with open(light_path, 'w+') as file: file.write(light_array) image_count = image_count + 1
def task4_2(): matrix = random_matrix(4, 4, n=8) output_path = os.path.join(OUTPUT_DIR, 'original_matrix.png') imsave(output_path, matrix) histogram = create_histogram(matrix) normalized = normalize_histogram(histogram) cdf = create_cdf(normalized) image = transform(matrix, cdf) print(matrix) print(image) output_path = os.path.join(OUTPUT_DIR, 'normalized_matrix.png') imsave(output_path, image) pl.imshow(image, cmap=cm.Greys_r) pl.show() einstein = imread(EINSTEIN) ein_cdf = create_cdf(normalize_histogram(create_histogram(einstein))) imsave(os.path.join(OUTPUT_DIR, "cdf_" + os.path.split(EINSTEIN)[-1]), transform(einstein, ein_cdf))
for a in act: # print act name = a.split()[1].lower() i = 0 for line in open("facescrub_actresses.txt"): if a in line: filename = name + str(i) + '.' + line.split()[4].split('.')[-1] face_dim = line.split("\t")[4].split(',') face_dim = [int(j) for j in face_dim] # print(face_dim) #A version without timeout (uncomment in case you need to #unsupress exceptions, which timeout() does) #testfile.retrieve(line.split()[4], "uncropped/"+filename) #timeout is used to stop downloading images which take too long to download timeout(testfile.retrieve, (line.split()[4], "uncropped/" + filename), {}, 30) if not os.path.isfile("uncropped/" + filename): continue try: imarray = imread("uncropped/" + filename) except: continue cropped = imarray[face_dim[1]:face_dim[3], face_dim[0]:face_dim[2]] resized = imresize(cropped, (32, 32)) if len(resized.shape) == 3: grayscale = rgb2gray(resized) imsave("cropped/" + filename, grayscale) print filename i += 1
def save_image(self, image): imsave( self.result_image_filepath, self.postprocessing_img(image.reshape(self.e_image_shape).copy()))
def run_query_and_save_map(table): global MAP_ARRAY global COLOR_FUNC # These dimensions are not reversed - number of rows is first MAP_ARRAY = numpy.zeros((FLAGS.height, FLAGS.width, 3), dtype=numpy.float) # Set color_field from FLAGS color_field = COLOR_FIELDS[FLAGS.color_field] COLOR_FUNC = color_field['func'] try: query = ("SELECT %s," "connection_spec.client_geolocation.latitude," "connection_spec.client_geolocation.longitude " "FROM [%s.%s] " "WHERE log_time > 0 AND %s AND " "web100_log_entry.is_last_entry == true") % \ (color_field['select'], DATASET_ID, table, color_field['where']) logging.info('Running %s', query) job_collection = BQ_SERVICE.jobs() job_data = { 'configuration': { 'query': { 'query': query, 'priority': FLAGS.priority } } } insert_response = job_collection.insert(projectId=PROJECT_ID, body=job_data).execute() import time current_status = 'INVALID' while current_status != 'DONE': time.sleep(30) status = job_collection.get( projectId=PROJECT_ID, jobId=insert_response['jobReference']['jobId']).execute() current_status = status['status']['state'] logging.info('%s', current_status) current_row = 0 logging.info('getQueryResults %d', current_row) query_reply = job_collection.getQueryResults( projectId=PROJECT_ID, jobId=insert_response['jobReference']['jobId'], startIndex=current_row).execute() total_rows = int(query_reply['totalRows']) while ('rows' in query_reply) and current_row < total_rows: logging.info('Received rows from %d / %d [%.2f%%]', current_row, total_rows, 100.0 * float(current_row) / float(total_rows)) plot_rows(query_reply['rows']) current_row += len(query_reply['rows']) logging.info('getQueryResults %d', current_row) query_reply = job_collection.getQueryResults( projectId=PROJECT_ID, jobId=query_reply['jobReference']['jobId'], startIndex=current_row).execute() # convert to image and show # img = pilutil.toimage(MAP_ARRAY) # img.show() # TODO(dominic): Normalize/gamma correct on flag MAP_ARRAY.clip(0.0, 1.0, out=MAP_ARRAY) # save image to disk output_name = FLAGS.output + table + '.bmp' logging.info('Saving map to %s', output_name) pilutil.imsave(output_name, MAP_ARRAY) except HttpError as err: logging.error('Error running query: %s', pprint.pprint(err.content)) sys.exit(1) except AccessTokenRefreshError: logging.error( 'Credentials have been revoked or expired. Please re-run ' 'the application to re-authorize.') sys.exit(1)
skip_mean_IU_ice_2 += 1 seg_img = (seg_img * label_diff).astype(np.uint8) if len(seg_img.shape) != 3: seg_img = np.stack((seg_img, seg_img, seg_img), axis=2) if stitch and stitch_seg: stitched = np.concatenate((stitched, seg_img), axis=1) if not stitch and show_img: cv2.imshow('seg_img', seg_img) if stitch: if save_stitched: seg_save_path = os.path.join( save_path, '{}.{}'.format(img_fname_no_ext, out_ext)) imsave(seg_save_path, stitched) if show_img: cv2.imshow('stitched', stitched) else: if show_img: cv2.imshow('src_img', src_img) if labels_path: cv2.imshow('labels_img', labels_img) if show_img: k = cv2.waitKey(1 - _pause) if k == 27: sys.exit(0) elif k == 32: _pause = 1 - _pause img_done = img_id - start_id + 1
print "sum", square_diff.sum() mse = square_diff.sum() / square_diff.size print "mse ", mse psnr = 20 * log10(1 / sqrt(mse)) return psnr im = lena() hvs = gen_hvs_mask(im) message = gen_message(im.shape) imsave("message.tif", message) (a, watermark) = watermark(im, hvs, message) imsave("watermarked.tif", a) diff = im - a imsave("wdiff.tif", diff) # noise = a # noise = a + 10 # n = reshape( normal( 0 , 2 , a.size ) , a.shape) # noise = a + n # noise = gaussian_filter(a , .5) noise = (a - 0) / (240 - 0) * 255
def save_planes(subject, index, planes, folder): suffixes = "abc" for i, plane in enumerate(planes): path = folder + f"{subject.subject_id}{index}{suffixes[i]}.png" if not os.path.isfile(path): pilutil.imsave(path, plane)
with tf.Session(config=config) as sess: init.run() for epoch in range(n_epochs): if epoch % 100 == 0: loss_convnet_val = loss_convnet.eval(feed_dict={X: img}) print("Epoch:", epoch, "Convnet loss:", loss_convnet_val) sess.run(training_op, feed_dict={X: img, lr: learning_rate}) if (epoch + 1) % 10 == 0: learning_rate = 0.9 * learning_rate loss_convnet_val = loss_convnet.eval(feed_dict={X: img}) print("Epoch:", epoch, "Convnet loss:", loss_convnet_val) phi_val = phi.eval(feed_dict={X: img}) ClassIndicator = phi_val.reshape((height, width, nclass)) # plt.imshow(ClassIndicator) Seg = np.zeros((height, width), dtype=np.uint8) label_diff = 127 for i in range(height): for j in range(width): val = -1 label = -1 for n in range(nclass): if ClassIndicator[i, j, n] > val: val = ClassIndicator[i, j, n] label = n Seg[i, j] = label * label_diff imsave('result.png', Seg) # plt.imshow(Seg)
# path for image C:\Users\haame\DSND_Term1\projects/python_bgn # Python script using Scipy # for image manipulation , # from scipy.misc import from scipy.misc.pilutil import imread, imsave, imresize # from scipy.misc.pilutil import Image as img # import Image # Read a JPEG image into a numpy array img = imread('C:/Users/haame/DSND_Term1/projects/python_bgn/drgnfly.jpg' ) # path of the image print(img.dtype, img.shape) # Tinting the image img_tint = img * [1, 0.45, 0.3] # Saving the tinted image imsave('C:/Users/haame/DSND_Term1/projects/python_bgn/drgnfly_2.jpg', img_tint) # Resizing the tinted image to be 300 x 300 pixels img_tint_resize = imresize(img_tint, (300, 300)) # Saving the resized tinted image imsave( 'C:/Users/haame/DSND_Term1/projects/python_bgn/drgnfly_tinted_resized.jpg', img_tint_resize)
print 'Completed reading input' for date in stats.keys(): # note: number of rows is first. # Initialize with ones so we start all white array = numpy.ones((len(tools), len(servers), 3), dtype = numpy.float) y = 0 x = 0 for tool in tools: if tool in stats[date].keys() and max_test_count[date][tool] != 0: for server in servers: if server in stats[date][tool].keys(): color = stats[date][tool][server] / max_test_count[date][tool] array[y, x] = [1.0, 1.0 - color, 1.0 - color] x += 1 x = 0 y += 1 if not os.path.exists('out'): os.makedirs('out') output = 'out/' + FLAGS.output + '.' + date + '.bmp' sys.stdout.write('Saving usage to %s\r' % output) sys.stdout.flush() resized_array = pilutil.imresize(array, (10*len(tools), 10*len(servers)), 'nearest') pilutil.imsave(output, resized_array) print '\nDone' if __name__ == '__main__': main()
if curr_labels_indices is None: curr_labels_indices = class_indices else: curr_labels_indices = np.concatenate( (curr_labels_indices, class_indices), axis=0) if skip_image: continue mask = np.ones(labels_img.shape, np.bool) mask[np.unravel_index(curr_labels_indices, labels_img.shape)] = 0 labels_img[mask] = 255 dst_labels_img_fname = os.path.join(out_labels_path, img_fname) imsave(dst_labels_img_fname, labels_img) src_img_fname = os.path.join(images_path, img_fname) dst_img_fname = os.path.join(out_images_path, img_fname) if copy_images: os.system('cp {} {}'.format(src_img_fname, dst_img_fname)) else: os.system('ln -s {} {}'.format(src_img_fname, dst_img_fname)) sys.stdout.write('\rDone {}/{} images ({} skipped)'.format( _id + 1, _n_images, n_skipped)) sys.stdout.flush() sys.stdout.write('\n') sys.stdout.flush()
from scipy.cluster.vq import vq, kmeans2, whiten from scipy.ndimage.measurements import watershed_ift from scipy.misc.pilutil import imread, imsave from matplotlib.pyplot import imshow import color img = imread("blue.jpg") markers = imread("bluem2.jpg") markers = int32(markers[:, :, 1]) markers = threshold(markers, 100, None, 1) # black to 1 print max(markers) print min(markers) markers = threshold(markers, None, 200, -1) # white to -1 print max(markers) print min(markers) markers = threshold(markers, None, 2, 0) # gray to 0 markers = int8(markers) mask = watershed_ift(img[:, :, 1], markers) print mask mask = mask + 1 print mask print max(mask) mask = threshold(mask, None, 1, 1) print max(mask) mask = 255 * mask imsave("mask_watershed.jpg", mask) a = 1
#im_two[125:200 , 325:425] = im_two.mean() im_one = im_two[90:210 , 290:462].copy() #im_two[125:200 , 325:425] = random_integers(0 , 255 , (75 , 100)) im_one_weights = ones(im_one.shape) im_two_weights = zeros(im_two.shape) im_one_weights[im_one < 128] = 0 im_two_weights[im_two < 140] = 128 im_one_weights = floor(gaussian_filter(im_one_weights , .5)) im_two_weights = ceil(gaussian_filter(im_two_weights , .5)) imsave('im_one_weights.tif' , im_one_weights) imsave('im_two_weights.tif' , im_two_weights) #nnf = initialize_nearest_neighbor_field(im_one.shape , im_two.shape , (3 , 3) , 90 , 290) default_x_offset = 90 default_y_offset = 290 nnf_size = im_one.shape nnf = ones((nnf_size[0] , nnf_size[1] , 2)) print default_x_offset , default_y_offset print nnf_size nnf[: , : , 0] = transpose(range(default_x_offset , nnf_size[0] + default_x_offset) * transpose(nnf[: , : , 0])) nnf[: , : , 1] = range(default_y_offset , nnf_size[1] + default_y_offset) * nnf[: , : , 1] print nnf print nnf.shape
# TODO: filter item against color_field.select and type if item["type"] == FLAGS.test_type: plot_item(item) f.close() # convert to image and show # img = pilutil.toimage(MAP_ARRAY) # img.show() # TODO(dominic): Normalize/gamma correct on flag MAP_ARRAY.clip(0.0, 1.0, out=MAP_ARRAY) # save image to disk output_name = FLAGS.output + FLAGS.test_type + '.' + \ FLAGS.color_field + '.' + str(year) + '.' + str(month).zfill(2) + '.bmp' logging.info('Saving map to %s', output_name) pilutil.imsave(output_name, MAP_ARRAY) month += 1 if month > 12: month -= 12 year += 1 except Exception as e: logging.error(e) sys.exit(1) logging.info('Complete') if __name__ == '__main__': main()
from scipy.misc.pilutil import imsave import numpy import sys print ('********************************************************************************') print ('* Really simple negative image *') print ('********************************************************************************') sys.path.append('../utils') import userinput fpath = userinput.get_img_path() img_array = userinput.get_gray_img(fpath) (img_array_x_len, img_array_y_len) = img_array.shape new_img_array = 255 - img_array new_path = fpath + '.grayscale-negative.bmp' print 'saving grayscale negative image as \'%s\'...' % (new_path) imsave(new_path, new_img_array) print ('********************************************************************************')
def shapeAnalysis(mask): height, width = mask.shape pixels = height * width # spatial and central moments moments = cv.Moments(mask, binary=1) huMoments = cv.GetHuMoments(moments) print "Shape hu moments", huMoments # distances from the gravity point contour_seq = cv.FindContours(np.array(mask), cv.CreateMemStorage(), cv.CV_RETR_TREE, cv.CV_CHAIN_APPROX_SIMPLE) gravity_center = (int(moments.m10 / moments.m00), int(moments.m01 / moments.m00)) # (x, y) gx, gy = gravity_center distances = np.array( [math.sqrt((gx - x)**2 + (gy - y)**2) for (x, y) in contour_seq]) dist_distri, bin_dist = np.histogram(distances, bins=10, range=None, normed=True) print "dist distribution", dist_distri dist_max = np.max(distances) dist_min = np.min(distances) dist_ratio_min_max = dist_min / dist_max print "dist ratio min max", dist_ratio_min_max dist_mean = np.mean(distances) dist_std = np.std(distances) # normalize distance min and max dist_max = dist_max / pixels dist_min = dist_min / pixels dist_mean = dist_mean / pixels dist_std = dist_std / pixels print "dist max", dist_max print "dist min", dist_min print "dist mean", dist_mean print "dist std", dist_std # number of petals nbPetals = np.sum([ min(x1, x2) < dist_mean < max(x1, x2) for x1, x2 in zip(distances[:-1], distances[1:]) ]) / 2 print "petals", nbPetals poly_seq = cv.ApproxPoly(contour_seq, cv.CreateMemStorage(), cv.CV_POLY_APPROX_DP, 2.8) ppimg = np.zeros(mask.shape) for (x, y) in poly_seq: ppimg[y, x] = 255 imsave('/home/cplab/workspace/imageex/src/imageex/static/POLYYYAAAAA.png', ppimg) convex_hull = cv.ConvexHull2(poly_seq, cv.CreateMemStorage()) convexity_defects = cv.ConvexityDefects(poly_seq, convex_hull, cv.CreateMemStorage()) # number of defects nbDefects = len(convexity_defects) print "defects", nbDefects convexity_seq = sum([[cd[0], cd[2], cd[1]] for cd in convexity_defects], []) ppimg = np.zeros(mask.shape) for (x, y) in convexity_seq: ppimg[y, x] = 255 imsave('/home/cplab/workspace/imageex/src/imageex/static/CONVEXXAAAAA.png', ppimg) convexity_depths = np.array([cd[3] for cd in convexity_defects]) convexity_depth_max = np.max(convexity_depths) convexity_depth_min = np.min(convexity_depths) convexity_depth_ratio_min_max = convexity_depth_min / convexity_depth_max print "convexity depth ratio min max", convexity_depth_ratio_min_max #normalize convexity_depth_max = convexity_depth_max / pixels print "convexity depth max", convexity_depth_max area = cv.ContourArea(contour_seq) perimeter = cv.ArcLength(contour_seq) perimeterOarea = perimeter / area print "perimeter over area", perimeterOarea features = [] features += list(huMoments) features += dist_distri, dist_ratio_min_max, dist_max, dist_min, dist_mean, dist_std features += nbPetals, nbDefects features += convexity_depth_ratio_min_max, convexity_depth_max, perimeterOarea return features
def _convert_dataset(db_name): """Converts the specified dataset split to TFRecord format. Args: db_name: The dataset split (e.g., train, test). Raises: RuntimeError: If loaded image and label have different shape. """ output_dir = os.path.join(FLAGS.db_root_dir, FLAGS.output_dir, 'tfrecord') sys.stdout.write('Processing {}\n\n'.format(db_name)) images = os.path.join(FLAGS.db_root_dir, db_name, 'images', '*.{}'.format(FLAGS.image_format)) print('Reading images from: {}'.format(images)) image_filenames = glob.glob(images) if image_filenames is None: raise SystemError('No images found at {}'.format(images)) if FLAGS.create_dummy_labels: labels_path = os.path.join(FLAGS.db_root_dir, db_name, 'labels') if not os.path.isdir(labels_path): os.makedirs(labels_path) print('Creating dummy labels at: {}'.format(labels_path)) for image_filename in image_filenames: image = imread(image_filename) height, width, _ = image.shape dummy_label = np.zeros((height, width), dtype=np.uint8) out_fname = os.path.splitext( os.path.basename(image_filename))[0] + '.{}'.format( FLAGS.label_format) imsave(os.path.join(labels_path, out_fname), dummy_label) print('Done') labels = os.path.join(FLAGS.db_root_dir, db_name, 'labels', '*.{}'.format(FLAGS.label_format)) print('Reading labels from: {}'.format(labels)) seg_filenames = glob.glob(labels) if seg_filenames is None: raise SystemError('No labels found at {}'.format(labels)) # filenames = [x.strip('\n') for x in open(dataset_split, 'r')] num_images = len(image_filenames) num_labels = len(seg_filenames) if num_images != num_labels: raise SystemError( 'Mismatch between image and label file counts: {}, {}'.format( num_images, num_labels)) num_per_shard = int(math.ceil(num_images / float(_NUM_SHARDS))) image_reader = build_data.ImageReader('png', channels=3) label_reader = build_data.ImageReader('png', channels=3) if not os.path.isdir(output_dir): os.makedirs(output_dir) print('Writing tfrecords to: {}'.format(output_dir)) for shard_id in range(_NUM_SHARDS): output_filename = os.path.join( output_dir, '%s-%05d-of-%05d.tfrecord' % (db_name, shard_id, _NUM_SHARDS)) with tf.python_io.TFRecordWriter(output_filename) as tfrecord_writer: start_idx = shard_id * num_per_shard end_idx = min((shard_id + 1) * num_per_shard, num_images) for i in range(start_idx, end_idx): sys.stdout.write('\r>> Converting image %d/%d shard %d' % (i + 1, num_images, shard_id)) sys.stdout.flush() image_filename = image_filenames[i] f1 = os.path.basename(image_filename)[:-4] seg_filename = seg_filenames[i] f2 = os.path.basename(image_filename)[:-4] if f1 != f2: raise SystemError( 'Mismatch between image and label filenames: {}, {}'. format(f1, f2)) # Read the image. image_data = tf.gfile.FastGFile(image_filename, 'r').read() height, width = image_reader.read_image_dims(image_data) # Read the semantic segmentation annotation. seg_data = tf.gfile.FastGFile(seg_filename, 'r').read() seg_height, seg_width = label_reader.read_image_dims(seg_data) if height != seg_height or width != seg_width: raise RuntimeError( 'Shape mismatched between image and label.') # Convert to tf example. example = build_data.image_seg_to_tfexample( image_data, image_filename, height, width, seg_data) tfrecord_writer.write(example.SerializeToString()) sys.stdout.write('\n') sys.stdout.flush()
# note: number of rows is first. # Initialize with ones so we start all white array = numpy.ones((len(tools), len(servers), 3), dtype=numpy.float) y = 0 x = 0 for tool in tools: if tool in stats[date].keys() and max_test_count[date][tool] != 0: for server in servers: if server in stats[date][tool].keys(): color = stats[date][tool][server] / max_test_count[ date][tool] array[y, x] = [1.0, 1.0 - color, 1.0 - color] x += 1 x = 0 y += 1 if not os.path.exists('out'): os.makedirs('out') output = 'out/' + FLAGS.output + '.' + date + '.bmp' sys.stdout.write('Saving usage to %s\r' % output) sys.stdout.flush() resized_array = pilutil.imresize(array, (10 * len(tools), 10 * len(servers)), 'nearest') pilutil.imsave(output, resized_array) print '\nDone' if __name__ == '__main__': main()
x_block_count = expanded_x_len / 8 y_block_count = expanded_y_len / 8 intensities = [1, 2, 4, 8, 16, 32]; for intensity in intensities: print '\trunning JPEG (quantization matrix scale * %i)...' % (intensity) for x_block in range(0, x_block_count): for y_block in range(0, y_block_count): x_start, y_start = x_block * 8, y_block * 8 x_end, y_end = x_start + 8, y_start + 8 # perform DCT and quantization over each 8x8 block dct_array[x_start:x_end, y_start:y_end] = dct(expanded_array[x_start:x_end, y_start:y_end].copy()) quantized_array[x_start:x_end, y_start:y_end] = round(dct_array[x_start:x_end, y_start:y_end] / (quantization_array * intensity)) # perform dequantization and inverse DCT over each 8x8 block dequantized_array[x_start:x_end, y_start:y_end] = quantized_array[x_start:x_end, y_start:y_end] * quantization_array * intensity idct_array[x_start:x_end, y_start:y_end] = idct(dequantized_array[x_start:x_end, y_start:y_end]) # get the error rate error_rate = 0. error_array = img_array - idct_array for index, err in numpy.ndenumerate(error_array): error_rate += abs(err) error_rate *= (1.0 / (64.0 * x_block_count * y_block_count)) print '\t> error_rate: ', error_rate new_path = fpath + '.my_jpeg-' + str(intensity) + '.bmp' print '\t\tsaving image as \'%s\'' % (new_path) imsave(new_path, idct_array.astype('uint8')) print ('********************************************************************************')
"""Load the image""" img = np.array(imread(in_fname, 1)/255, dtype=int) """Determine the images depth""" depth = np.max(img)+1 """Define the sliding window size""" seg_size = 100 """ If the image is smaller that the sliding window, then just denoise the whole image """ if img.shape[0]<seg_size: new_img = denoise_image(img) imsave(out_fname + str(1) + '.png', new_img) else: """Denoise the image in overlapping segments""" count = 0 cut = int(float(seg_size)/2) for i in range(cut, img.shape[0], cut): for j in range(cut, img.shape[1], cut): """Extract the window to denoise""" sub_img = img[i-cut:i+cut, j-cut:j+cut] """Denoise the window""" new_img = denoise_image(sub_img, depth, 6) """ Place the denoised window back into the noisy image, except for the leading edge pixels of the window.
# fw_IU[img_id] = eval.frequency_weighted_IU(labels, labels_img) if save_test: print('Saving test result') Seg = (labels * label_diff).astype(np.uint8) seg_save_path = os.path.join(save_path, '{:s}_epoch_{:d}.png'.format( test_names[img_id], epoch_id + 1)) if save_stitched: gt_seq = (labels_img * label_diff).astype(np.uint8) if len(gt_seq.shape) != 3: gt_seq = np.stack((gt_seq, gt_seq, gt_seq), axis=2) if len(Seg.shape) != 3: Seg = np.stack((Seg, Seg, Seg), axis=2) Seg = np.concatenate((test_img_orig, gt_seq, Seg), axis=1) imsave(seg_save_path, Seg) overall_end_t = time.time() overall_fps = 1.0 / (overall_end_t - overall_start_t) mean_pix_acc += (pix_acc[img_id] - mean_pix_acc) / (img_id + 1) sys.stdout.write('\rDone {:5d}/{:5d} frames in epoch {:5d} ({:6.2f}({:6.2f}, {:6.2f}) fps) ' 'pix_acc: {:.10f}'.format( img_id + 1, n_test_images, epoch_id, fps, fps_with_input, overall_fps, mean_pix_acc)) sys.stdout.flush() print() # mean_pix_acc = np.mean(pix_acc) if mean_pix_acc > max_pix_acc: max_pix_acc = mean_pix_acc
def manual(img1, img2, k, x): rows1 = img1.shape[0] cols1 = img1.shape[1] channel1 = img1.shape[2] rows2 = img2.shape[0] cols2 = img2.shape[1] channel2 = img2.shape[2] image1 = img1.reshape((rows1*cols1), channel1) image2 = img2.reshape((rows2*cols2), channel2) #print(img1) pixel1 = img1[1, 2] pixel2 = img2[3, 4] pixel3 = img2[5, 6] newImage = np.concatenate((image1, image2), axis =0) pixels = [] pixels.append(pixel1) pixels.append(pixel2) pixels.append(pixel3) #print(np.array(pixels)) km = KMeans(n_clusters = k, init = np.array(pixels), n_init = 1) km.fit(newImage) clusters = np.array(km.cluster_centers_) labels = np.array(km.labels_) labels = labels.reshape(rows1*2, cols1) #cv2.imwrite("out.jpg", labels) #print(clusters) imsave("out.jpg", labels) '''im = imread("105c.jpg") im = convertColorSpace(im) rows0 = im.shape[0] cols0 = im.shape[1] channel0 = im.shape[2] image = im.reshape(rows0*cols0, channel0) new = km.predict(image) labels0 = np.array(new) labels0 = labels0.reshape(rows0, cols0) imsave("outer.jpg", labels0)''' #################################################### if x == 1: i = 1 imageList = [] imageDir = "filarioidea/" #imageDir = "plasmodium/" #imageDir = "schistoma/" imageExte = ".jpg" for filename in os.listdir(imageDir): extension = os.path.splitext(filename)[1] if extension.lower() != imageExte: continue imageList.append(os.path.join(imageDir, filename)) for imagePath in imageList: #image = cv2.imread(imagePath) #cluster(image, centroids, 3, i) im = imread(imagePath) im = convertColorSpace(im) rows1 = im.shape[0] cols1 = im.shape[1] channel1 = im.shape[2] image = im.reshape(rows1*cols1, channel1) new = km.predict(image) labels1 = np.array(new) labels1 = labels1.reshape(rows1, cols1) output = "filaria_clustered{}.jpg".format(i) imsave(output, labels1) i = i + 1 elif x == 2: i = 1 imageList = [] imageDir = "schistoma/" #imageDir = "plasmodium/" #imageDir = "schistoma/" imageExte = ".jpg" for filename in os.listdir(imageDir): extension = os.path.splitext(filename)[1] if extension.lower() != imageExte: continue imageList.append(os.path.join(imageDir, filename)) for imagePath in imageList: #image = cv2.imread(imagePath) #cluster(image, centroids, 3, i) im = imread(imagePath) im = convertColorSpace(im) rows1 = im.shape[0] cols1 = im.shape[1] channel1 = im.shape[2] image = im.reshape(rows1*cols1, channel1) new = km.predict(image) labels1 = np.array(new) labels1 = labels1.reshape(rows1, cols1) output = "schistoma_clustered{}.jpg".format(i) imsave(output, labels1) i = i + 1 elif x == 3: i = 1 imageList = [] imageDir = "plasmodium/" #imageDir = "plasmodium/" #imageDir = "schistoma/" imageExte = ".jpg" for filename in os.listdir(imageDir): extension = os.path.splitext(filename)[1] if extension.lower() != imageExte: continue imageList.append(os.path.join(imageDir, filename)) for imagePath in imageList: #image = cv2.imread(imagePath) #cluster(image, centroids, 3, i) im = imread(imagePath) im = convertColorSpace(im) rows1 = im.shape[0] cols1 = im.shape[1] channel1 = im.shape[2] image = im.reshape(rows1*cols1, channel1) new = km.predict(image) labels1 = np.array(new) labels1 = labels1.reshape(rows1, cols1) output = "plasmodium_clustered{}.jpg".format(i) imsave(output, labels1) i = i + 1
def task6(): imgs = ((BRICKS, 'bricks'), (lena(), 'lena')) for img, name in imgs: for n in (2, 3, 4): imsave('output/{}_down_{}x.png'.format(name, n), alias(img, n))
def shapeAnalysis(mask): height, width = mask.shape pixels = height * width # spatial and central moments moments = cv.Moments(mask, binary = 1) huMoments = cv.GetHuMoments(moments) print "Shape hu moments", huMoments # distances from the gravity point contour_seq = cv.FindContours(np.array(mask), cv.CreateMemStorage(), cv.CV_RETR_TREE, cv.CV_CHAIN_APPROX_SIMPLE) gravity_center = (int(moments.m10/moments.m00), int(moments.m01/moments.m00)) # (x, y) gx, gy = gravity_center distances = np.array([math.sqrt((gx - x)**2 + (gy - y)**2) for (x,y) in contour_seq]) dist_distri, bin_dist = np.histogram(distances, bins=10, range=None, normed=True) print "dist distribution", dist_distri dist_max = np.max(distances) dist_min = np.min(distances) dist_ratio_min_max = dist_min / dist_max print "dist ratio min max", dist_ratio_min_max dist_mean = np.mean(distances) dist_std = np.std(distances) # normalize distance min and max dist_max = dist_max / pixels dist_min = dist_min / pixels dist_mean = dist_mean / pixels dist_std = dist_std / pixels print "dist max", dist_max print "dist min", dist_min print "dist mean", dist_mean print "dist std", dist_std # number of petals nbPetals = np.sum([min(x1,x2) < dist_mean < max(x1,x2) for x1,x2 in zip(distances[:-1], distances[1:])])/2 print "petals", nbPetals poly_seq = cv.ApproxPoly(contour_seq, cv.CreateMemStorage(), cv.CV_POLY_APPROX_DP, 2.8) ppimg = np.zeros(mask.shape) for (x, y) in poly_seq: ppimg[y, x] = 255 imsave('/home/cplab/workspace/imageex/src/imageex/static/POLYYYAAAAA.png', ppimg) convex_hull = cv.ConvexHull2(poly_seq, cv.CreateMemStorage()) convexity_defects = cv.ConvexityDefects(poly_seq, convex_hull, cv.CreateMemStorage()) # number of defects nbDefects = len(convexity_defects) print "defects", nbDefects convexity_seq = sum([[cd[0], cd[2], cd[1]] for cd in convexity_defects], []) ppimg = np.zeros(mask.shape) for (x, y) in convexity_seq: ppimg[y, x] = 255 imsave('/home/cplab/workspace/imageex/src/imageex/static/CONVEXXAAAAA.png', ppimg) convexity_depths = np.array([cd[3] for cd in convexity_defects]) convexity_depth_max = np.max(convexity_depths) convexity_depth_min = np.min(convexity_depths) convexity_depth_ratio_min_max = convexity_depth_min / convexity_depth_max print "convexity depth ratio min max", convexity_depth_ratio_min_max #normalize convexity_depth_max = convexity_depth_max / pixels print "convexity depth max", convexity_depth_max area = cv.ContourArea(contour_seq) perimeter = cv.ArcLength(contour_seq) perimeterOarea = perimeter/area print "perimeter over area", perimeterOarea features = [] features += list(huMoments) features += dist_distri, dist_ratio_min_max, dist_max, dist_min, dist_mean, dist_std features += nbPetals, nbDefects features += convexity_depth_ratio_min_max, convexity_depth_max, perimeterOarea return features
def train(img1, img2, k, x): rows1 = img1.shape[0] cols1 = img1.shape[1] channel1 = img1.shape[2] rows2 = img2.shape[0] cols2 = img2.shape[1] channel2 = img2.shape[2] image1 = img1.reshape((rows1*cols1), channel1) image2 = img2.reshape((rows2*cols2), channel2) newImage = np.concatenate((image1, image2), axis =0) km = KMeans(n_clusters = k) km.fit(newImage) clusters = np.array(km.cluster_centers_) labels = np.array(km.labels_) labels = labels.reshape(rows1*2, cols1) #cv2.imwrite("out.jpg", labels) #labels.append(img1.shape[2]) imsave("out.jpg", labels) #imsave("D:/Owl/CS180/MP2/output/out.jpg", labels) '''im = imread("filaria.jpg") im = convertColorSpace(im) rows0 = im.shape[0] cols0 = im.shape[1] channel0 = im.shape[2] image = im.reshape(rows0*cols0, channel0) new = km.predict(image) labels0 = np.array(new) labels0 = labels0.reshape(rows0, cols0) #labels0 = cv2.cvtColor(labels0, cv2.COLOR_HSV2BGR) imsave("outerer.jpg", labels0)''' #labels0 = cv2.cvtColor(im, cv2.COLOR_HSV2BGR) ############################################## if x == 1: i = 1 imageList = [] imageDir = "filarioidea/" #imageDir = "plasmodium/" #imageDir = "schistoma/" imageExte = ".jpg" for filename in os.listdir(imageDir): extension = os.path.splitext(filename)[1] if extension.lower() != imageExte: continue imageList.append(os.path.join(imageDir, filename)) for imagePath in imageList: #image = cv2.imread(imagePath) #cluster(image, centroids, 3, i) im = imread(imagePath) im = convertColorSpace(im) rows1 = im.shape[0] cols1 = im.shape[1] channel1 = im.shape[2] image = im.reshape(rows1*cols1, channel1) new = km.predict(image) labels1 = np.array(new) labels1 = labels1.reshape(rows1, cols1) output = "filaria_clustered{}.jpg".format(i) imsave(output, labels1) i = i + 1 elif x == 2: i = 1 imageList = [] imageDir = "schistoma/" #imageDir = "plasmodium/" #imageDir = "schistoma/" imageExte = ".jpg" for filename in os.listdir(imageDir): extension = os.path.splitext(filename)[1] if extension.lower() != imageExte: continue imageList.append(os.path.join(imageDir, filename)) for imagePath in imageList: #image = cv2.imread(imagePath) #cluster(image, centroids, 3, i) im = imread(imagePath) im = convertColorSpace(im) rows1 = im.shape[0] cols1 = im.shape[1] channel1 = im.shape[2] image = im.reshape(rows1*cols1, channel1) new = km.predict(image) labels1 = np.array(new) labels1 = labels1.reshape(rows1, cols1) output = "schistoma_clustered{}.jpg".format(i) imsave(output, labels1) i = i + 1 elif x == 3: i = 1 imageList = [] imageDir = "plasmodium/" #imageDir = "plasmodium/" #imageDir = "schistoma/" imageExte = ".jpg" for filename in os.listdir(imageDir): extension = os.path.splitext(filename)[1] if extension.lower() != imageExte: continue imageList.append(os.path.join(imageDir, filename)) for imagePath in imageList: #image = cv2.imread(imagePath) #cluster(image, centroids, 3, i) im = imread(imagePath) im = convertColorSpace(im) rows1 = im.shape[0] cols1 = im.shape[1] channel1 = im.shape[2] image = im.reshape(rows1*cols1, channel1) new = km.predict(image) labels1 = np.array(new) labels1 = labels1.reshape(rows1, cols1) output = "plasmodium_clustered{}.jpg".format(i) imsave(output, labels1) i = i + 1