def readImage(path_image, convert): if (convert): im = Image.open(path_image).convert('L') else: im = Image.open(path_image) im = scipy.misc.fromimage(im) return im
def recognizeDigit(cnnModel, digit_location): digit_img = Image.open(digit_location).convert('L') digit_arr = np.asarray(digit_img) digit_arr.setflags(write=1) digit_arr = cv2.resize(digit_arr, (28, 28)) digit_arr[0] = 0 digit_arr[1] = 0 digit_arr[2] = 0 for x in digit_arr: i = 0 for y in x: if y < 30: x[i] = 0 i += 1 # img = np.zeros((20,20,3), np.uint8) # mi.imsave('test.jpg', test) digit_arr = digit_arr / 255.0 digit_arr = digit_arr.reshape(-1, 28, 28, 1) # predict results results = cnnModel.predict(digit_arr) # select the indix with the maximum probability accuracy = np.amax(results, axis=1) results = np.argmax(results, axis=1) ketqua = [results[0], accuracy[0]] # results = pd.Series(result,name="Label") return ketqua
def mean_filter(image, filter_size): im = Image.open(image).convert('L') # convert image to a numpy array im_matrix = misc.fromimage(im) # initialize the kernel of size 5-by-5 k = np.ones((filter_size, filter_size)) / (filter_size**2) im_meanfilter = filters.convolve(im, k, mode='nearest') # convert array to image image_new = misc.toimage(im_meanfilter) image_new.save('meanfilter.png')
def choosek(): #path is defined global global path2 #opening file using tkinter and it return image path path2=tkFileDialog.askopenfilename(filetypes=[("Image File",'.png'),("Image File",'.jpg')]) # global kernel1 ima=Image.open(path2) #image is resized ima1=ima.resize((380,380)); #image is configured to photo image image2=ImageTk.PhotoImage(ima1) label3.configure(image=image2) label3.image=image2 label3.place(x=500,y=20)
def median_filter(image, size): im = Image.open(image).convert('L') im = misc.fromimage(im) im_new = filters.median_filter(im, size=size, mode='reflect') im_new = misc.toimage(im_new) im_new.save('medianfilter.png')
import numpy, math import scipy.misc import scipy.fftpack as fftim from scipy.misc.pilutil import Image # opening the image and converting it to grayscale a = Image.open('../Figures/endothelium.png').convert('L') # a is converted to an ndarray b = scipy.misc.fromimage(a) # performing FFT c = fftim.fft2(b) # shifting the Fourier frequency image d = fftim.fftshift(c) # intializing variables for convolution function M = d.shape[0] N = d.shape[1] # H is defined and # values in H are initialized to 1 H = numpy.ones((M, N)) center1 = M / 2 center2 = N / 2 d_0 = 30.0 # cut-off radius t1 = 1 # the order of BHPF t2 = 2 * t1 # defining the convolution function for BHPF for i in range(1, M): for j in range(1, N): r1 = (i - center1)**2 + (j - center2)**2 # euclidean distance from
for i in range(first_bin, last_bin) : # calculate background entropy ent_back = 0 term = 0.5 / P1[i] for j in range(1, i) : ent_back -= hist[j] * np.log(1 - term*P1[j-1]) ent_back *= term # calculate foreground entropy ent_fore = 0 term = 0.5 / P2[i] for j in range(i+1, 256) : ent_fore -= hist[j] * np.log(1 - term*P2[j-1]) ent_fore *= term # set threshold to value where difference in entropy is minimal tot_ent = abs(ent_back - ent_fore) if (tot_ent < min_ent) : min_ent = tot_ent threshold = i return threshold # test case from scipy.misc.pilutil import Image a = Image.open('Rat_Hippocampal_Neuron.png').convert('L') adata = scipy.misc.fromimage(a) outimg = scipy.misc.toimage(adata > shanbhag(adata)) outimg.show()
import numpy as np import scipy.ndimage from scipy.misc.pilutil import Image img = Image.open("../dog1.jpeg").convert('L') # The filter k is a numpy array of size 5 by 5, with all values = 1/25. kernel = np.ones((5, 5)) / 25 b = scipy.ndimage.filters.convolve(img, kernel) b = scipy.misc.toimage(b) b.save("../dog2.jpeg")
import scipy.misc import numpy, math from scipy.misc.pilutil import Image #opening the image and converting it to grayscale a = Image.open('intensity transform.jpg').convert('L') a.save('input.jpg') # a is converted to an ndarray b = scipy.misc.fromimage(a) # b is converted to type float b1 = b.astype(float) # maximum value in b1 is determined b2 = numpy.max(b1) # performing the log transformation c = (255.0 * numpy.log(1 + b1)) / numpy.log(1 + b2) # c is converted to type int c1 = c.astype(int) # c1 is converted from ndarray to Image d = scipy.misc.toimage(c1) #saving d as logtransform_output.png d.save('output.jpg')
import numpy as np import scipy.misc, math from scipy.misc.pilutil import Image from skimage.transform import AffineTransform, warp img = Image.open('../Figures/angiogram1.png').convert('L') img1 = scipy.misc.fromimage(img) # rotation angle in radians transformation = AffineTransform(rotation=0.1) img2 = warp(img1, transformation) im4 = scipy.misc.toimage(img2) im4.save('../Figures/rotate_output.png') im4.show()
import scipy.misc import scipy.ndimage from scipy.misc.pilutil import Image # opening the image and converting it to grayscale a = Image.open('../Figures/vhuman_t1.png').convert('L') # performing Laplacian of Gaussian b = scipy.ndimage.filters.gaussian_laplace(a, 1, mode='reflect') # b is converted from an ndarray to an image b = scipy.misc.toimage(b) #b.save('../Figures/log_vh1.png') b.show()
import math, numpy import scipy.misc from scipy.misc.pilutil import Image # opening the image and converting it to grayscale im = Image.open('../Figures/hequalization_input.png').convert('L') # im is converted to an ndarray im1 = scipy.misc.fromimage(im) # finding the maximum and minimum pixel values b = im1.max() a = im1.min() print a, b # converting im1 to float c = im1.astype(float) # contrast stretching transformation im2 = 255 * (c - a) / (b - a) # im2 is converted from an ndarray to an image im3 = scipy.misc.toimage(im2) # saving im3 as contrast_output.png in # Figures folder im3.save('../Figures/contrast_output2.png')
import scipy.misc, numpy from skimage import feature from scipy.misc.pilutil import Image # opening the image and converting it to grayscale a = Image.open('../Figures/maps1.png').convert('L') # converting a to an ndarray a = scipy.misc.fromimage(a) # performing Canny edge filter b = feature.canny(a, sigma=1.0) # b is converted from ndarray to an image b = scipy.misc.toimage(b) # saving b as canny_output.png #b.save('../Figures/canny_output.png') b.show()
# Mean Filtering import scipy.ndimage from scipy.misc.pilutil import Image import matplotlib.pyplot as mpl # opening the image and converting it to grayscale a = Image.open('lenna.png').convert('L') # performing convolution b = scipy.ndimage.filters.median_filter(a, size=5, mode='reflect') # b is converted from an ndarray to an image b = scipy.misc.toimage(b) #b.save('meanfilter_lenna.png') mpl.imshow(b)
import numpy, math import scipy.misc from skimage.measure import label from scipy.misc.pilutil import Image from skimage.measure import regionprops # opening the image and converting it to grayscale a = Image.open('../figure/houghcircles_segmented.png').convert('L') # a is converted to an ndarray a = scipy.misc.fromimage(a) # labelling is preformed on a c = label(a) # c is converted from an ndarray to an image c1 = scipy.misc.toimage(c) # c1 is saved as label_output.png #c1.save('label_output.png') # on the labelled image c, regionprops is performed d = regionprops(c) print "The diameter of the circles are: " for props in d: print 2 * math.sqrt(props['Area'] / math.pi)
import scipy.misc import scipy.ndimage from scipy.misc.pilutil import Image # opening the image and converting it to grayscale a = Image.open('../Figures/ct_saltandpepper.png').convert('L') # performing the median filter b = scipy.ndimage.filters.median_filter(a, size=5, footprint=None, output=None, mode='reflect', cval=0.0, origin=0) # b is converted from an ndarray to an image b = scipy.misc.toimage(b) b.save('../Figures/median_output.png')
import scipy.misc import scipy.ndimage from scipy.misc.pilutil import Image # opening the image and converting it to grayscale a = Image.open('../Figures/wave.png').convert('L') # performing maximum filter b = scipy.ndimage.filters.maximum_filter(a,size=5,\ footprint=None,output=None,mode='reflect',\ cval=0.0,origin=0) # b is converted from an ndarray to an image b = scipy.misc.toimage(b) b.save('../Figures/maxo.png')
import math import scipy.misc import numpy as np from scipy.misc.pilutil import Image # opening the image and converting it to grayscale im = Image.open('../Figures/imageinverse_input.png').convert('L') # im is converted to an ndarray im1 = scipy.misc.fromimage(im) # performing the inversion operation im2 = 255 - im1 # im2 is converted from an ndarray to an image im3 = scipy.misc.toimage(im2) # saving the image as imageinverse_output.png in # Figures folder im3.save('../Figures/imageinverse_output.png')
import scipy.misc from skimage.filter import edges from scipy.misc.pilutil import Image # opening the image and converting it to grayscale a = Image.open('../Figures/cir.png').convert('L') # performing the Prewitt filter b = edges.prewitt(a) # b is converted from an ndarray to an image b = scipy.misc.toimage(b) b.save('../Figures/prewitt_cir.png')
import numpy as np from scipy.misc.pilutil import Image from skimage import filters from PIL import Image from PIL import ImageFilter from PIL import Image, ImageFilter from skimage import io from pylab import * import scipy.misc #read file to folder have name images im = Image.open('input.png') im1 = im.filter(ImageFilter.CONTOUR) #show image on figure figure() subplot(2, 4, 1), imshow(im), title("original image") axis('off') subplot(2, 4, 2), imshow(im1), title("Laplican Image") axis('off') im1arr = asarray(im, dtype=uint8) im2arr = asarray(im1, dtype=uint8) im2 = im1arr - im2arr im2 = scipy.misc.toimage(im2) subplot(2, 4, 3), imshow(im2), title("c:Subtracting (a) and (b)") axis('off') im3 = filters.sobel(im) # b is converted fimrom an ndarray to an image im3 = scipy.misc.toimage(im3)
def getDigit2(image, digit_name, stt): test = np.asarray(image) test.setflags(write=1) for x in test: i = 0 for y in x: t = 255 - y x[i] = t i += 1 for x in test: i = 0 for y in x: if y < 40: x[i] = 0 i += 1 test[0] = 0 test[1] = 0 test[2] = 0 test[3] = 0 test_width = len(test[0]) for column in test.T: test_weight = len(column) break test[test_weight - 1] = 255 for x in test: i = 0 x[0] = 0 x[1] = 0 x[2] = 0 x[len(x) - 1] = 0 x[len(x) - 2] = 0 x[len(x) - 3] = 0 i += 1 # xoa duong ke ngang arr = [] for x in test: i = 3 j = 0 if x[3] > 30 or x[4] > 30 or x[5] > 30: a = 0 for y in x: if a < 40: x[i] = 0 arr[i] = 0 else: arr[i] = x[i] break # print(arr) i += 1 if i > len(x) - 1: break else: a = arr[i] # print(a) k = len(x) - 3 for y in x: if arr[k - j] < 40: x[k - j] = 0 arr[k - j] = 0 else: arr[k - j] = x[k - j] break # print(arr) j += 1 else: arr.clear() for y in x: arr.append(y) i += 1 # tinh kich thuoc so pixel_a = 0 i = 0 for column in reversed(test.T): j = 0 for x in column: if j > len(column) - 5: break if x > 30: pixel_a = i break j += 1 if pixel_a > 0: break i += 1 digit_width = 0 i = 0 for column in reversed(test.T): if i < pixel_a + 13: i += 1 continue j = 0 for x in column: if j < 3: j += 1 continue if j == len(column) - 12: break if x == 0: check = 1 j += 1 continue else: check = 0 break if check == 1: digit_width = 5 break else: digit_width = 13 break i += 1 pixel_b = 0 i = 0 for column in reversed(test.T): if i < pixel_a + digit_width: i += 1 continue j = 0 for x in column: if j < 3: j += 1 continue if j == len(column) - 10: break if x == 0: check = 1 j += 1 continue else: check = 0 break if check == 1: pixel_b = i break i += 1 if pixel_b > pixel_a + 30: pixel_b = pixel_a + 20 # get digit2 digit2 = [] i = 0 k = 1 for x in test: if i < 3: i += 1 continue j = 0 arr1 = [] for y in reversed(x): if j < pixel_a - 2: j += 1 continue arr1.append(y) j += 1 if j == pixel_b: arr1.append(0) arr1.append(0) arr1.append(0) break # ============================================================================= # if i > test_weight - 4: # arr1[pixel_b - pixel_a + 2] = 0 # ============================================================================= digit2.append(arr1) i += 1 arr2 = [] for x in arr1: arr2.append(0) digit2.append(arr2) digit2.append(arr2) # imageio.imwrite('input/digit2.jpg', np.asarray(digit)) mi.imsave('temp/digit2.jpg', digit2) # fix digit2 digit2_img = Image.open('temp/digit2.jpg').convert('L') digit2_arr = np.asarray(digit2_img) digit2_arr.setflags(write=1) digit2_arr_new = [] for x in digit2_arr: arr2 = [] for y in reversed(x): arr2.append(y) digit2_arr_new.append(arr2) mi.imsave('temp/digit2.jpg', digit2_arr_new) dir = "./temp/" + str(digit_name) if not os.path.exists(dir): os.makedirs(dir) save_location = dir + "/" + str(stt) + "b.jpg" mi.imsave(save_location, digit2_arr_new)
import skimage.filters as fs import numpy as np import numpy.polynomial.polynomial as poly from skimage import feature from scipy.misc import pilutil as pu from scipy.misc.pilutil import Image from skimage import exposure from scipy.optimize import curve_fit from skimage import color, io, img_as_float #directory navigation i.e. path to image path_windows = 'C:/Users/sm2/Documents/Github Repository Clone/Polar-Nephelometer/Data/06-19-2017/Image30s50mW.BMP' path_linux = '/home/austen/PycharmProjects/TSI-3563-INeph/Data/06-19-2017/Image30s50mW.BMP' #Image.open reads the BMP as an image format im = Image.open(path_windows) #imread reads the image as a Matrix, the other a 2D array imMat = pu.imread(path_windows) imArray = mi.fromimage(im) # Otsu method for thresholding automatically finds the value that maximizes the variance between the background and foreground # using skimages module we will conduct otsu thresholding # I have included a variable to tune up or down the thresholding as I please val_variable = -40 val = fs.threshold_otsu(imArray) + val_variable hist, bins_center = exposure.histogram(imArray) otsu_imArray = imArray > val otsu_im = mi.toimage(otsu_imArray) # Create figure showing otsu thresholding based upon raw image histogram
import scipy.misc import numpy as np from skimage import filter import matplotlib.pyplot as plt from scipy.misc.pilutil import Image from skimage.morphology import label from skimage.measure import regionprops from skimage.feature import match_template # opening the image and converting it to grayscale image =Image.open('../Figures/airline_seating.png').convert('L') # converting the input image into an ndarray image = scipy.misc.fromimage(image) # reading the template image temp = Image.open('../Figures/template1.png'). convert('L') # converting the template into an ndarray temp = scipy.misc.fromimage(temp) # performing template matching result = match_template(image, temp) thresh = 0.7 # thresholding the result from template # matching considering pixel values where the # normalized cross-correlation is greater than 0.7 res = result > thresh # labeling the thresholded image c = label(res, background = 0) # performing regionprops to count the # number of labels reprop = regionprops(c) print "The number of seats are:", len(reprop)
def sobel_filter(image): im = Image.open(image).convert('L') im_new = filters.sobel(im) im_new = misc.toimage(im_new) im_new.save('sobelfilter.png')
import scipy.misc from scipy.misc.pilutil import Image #membuka image baru dan mengkonversinya ke Grayscale var_gambar = Image.open("Black.jpg").convert('L') #Konversi ke Array var_hasil1 = scipy.misc.fromimage(var_gambar) #Perlihatkan Nilai Array #temukan nilai maksimun dan minimun pixel #Nilai Maksimal Pixel var_b = var_hasil1.max() #Nilai Minimum pada Pixel var_a = var_hasil1.min() #konversi hasil1 ke int var_c = var_hasil1.astype(int) #transformasi Contrast Stretching var_hasil2 = (var_c - var_a) * 255 / (var_b - var_a) #konversi dari Array ke Image var_gambar_hasil = scipy.misc.toimage(var_hasil2) var_gambar_hasil.save("Streching.jpg") print("nilai hasil1", var_hasil1) print("nilai pixel minimum adalah:", var_a) print("nilai pixel maximal adalah:", var_b) print('nilai c adalah:', var_c) print(var_hasil2)
import matplotlib.pyplot as plt # fix random seed for reproducibility seed_value = 123 np.random.seed(seed_value) #set_random_seed(seed_value) # Keras uses its source of randomness regardless Theano or TensorFlow.In addition, TensorFlow has its own random number generator that must also be seeded by calling the set_random_seed() function immediately after the NumPy random number generator: exec(open(os.path.abspath('image_common_utils.py')).read()) #%% Filter: Basic definition on ppt #Filter for removing noise # Mean Filter # opening the image and converting it to grayscale a = Image.open('./image_data/elephant_marked.png').convert('L') plt.imshow(np.array(a)) plt.show() # initializing the filter of size 5 by 5. the filter is divided by 25 for normalization k = np.ones((5, 5)) / 25 np.sum(k) # performing convolution b = scipy.ndimage.filters.convolve(a, k) plt.imshow(b) plt.show() # The mean filter effectively removed the noise but in the process blurred the image. #Advantages of the mean lter # Removes noise.
import scipy.misc from scipy.misc.pilutil import Image #membuka image baru dan mengkonversinya ke Grayscale var_gambar = Image.open("test_2.jpg").convert('L') #Konversi ke Array var_hasil1 = scipy.misc.fromimage(var_gambar) #Perlihatkan Nilai Array #temukan nilai maksimun dan minimun pixel #Nilai Maksimal Pixel var_b = var_hasil1.max() #Nilai Minimum pada Pixel var_a = var_hasil1.min() #konversi hasil1 ke int var_c = var_hasil1.astype(int) #transformasi Contrast Stretching var_hasil2 = (var_c - var_a) * 255 / (var_b - var_a) #konversi dari Array ke Image var_gambar_hasil = scipy.misc.toimage(var_hasil2) var_gambar_hasil.save("test_3.jpg") print("nilai hasil1", var_hasil1) print("nilai pixel minimum adalah:", var_a) print("nilai pixel maximal adalah:", var_b) print('nilai c adalah:', var_c) print(var_hasil2)
import scipy.misc import scipy.ndimage from scipy.misc.pilutil import Image # opening the image and converting it to grayscale a =Image.open('../Figures/imagefor_laplacian.png').convert('L') # performing Laplacian filter b = scipy.ndimage.filters.laplace(a,mode='reflect') # b is converted from an ndarray to an image b = scipy.misc.toimage(b) #b.save('../Figures/laplacian_new.png') b.show()
import numpy, math import scipy.misc import scipy.fftpack as fftim from scipy.misc.pilutil import Image # opening the image and converting it to grayscale a = Image.open('../Figures/fft1.png').convert('L') # a is converted to an ndarray b = scipy.misc.fromimage(a) # performing FFT c = fftim.fft2(b) # shifting the Fourier frequency image d = fftim.fftshift(c) # intializing variables for convolution function M = d.shape[0] N = d.shape[1] # H is defined and # values in H are initialized to 1 H = numpy.ones((M, N)) center1 = M / 2 center2 = N / 2 d_0 = 30.0 # cut-off radius t1 = 1 # the order of BLPF t2 = 2 * t1 # defining the convolution function for BLPF for i in range(1, M): for j in range(1, N): r1 = (i - center1)**2 + (j - center2)**2 # euclidean distance from
def prewitt_filter(image): im = Image.open(image).convert('L') im_new = filters.prewitt(im) im_new = misc.toimage(im_new) im_new.save('prewittfilter.png')
import scipy.misc as mi import scipy.ndimage as ndim import scipy.ndimage.filters as filters import skimage.filters as fs import numpy as np from skimage import feature from scipy.misc import pilutil as pu from scipy.misc.pilutil import Image from skimage import data, color, exposure, img_as_float, io from skimage.feature import hog #directory navigation i.e. path to image path = '/home/austen/PycharmProjects/Polar Nephelometer/Data/05-10-2017/plots/Sum5Images.BMP' #Image.open reads the BMP as an image format im = Image.open(path).convert('L') #imread reads the image as a Matrix, the other a 2D array imMat = pu.imread(path) imArray = mi.fromimage(im) #Sobel filtering the image, looks decent for edge detection sobel_filter_im = fs.sobel(im) sobel_filter_imh = fs.sobel_h(im) sobel_filter_imv = fs.sobel_v(im) sobel_filter_im = mi.toimage(sobel_filter_im) sobel_filter_imh = mi.toimage(sobel_filter_imh) sobel_filter_imv = mi.toimage(sobel_filter_imv) #sobel_filter_im.show() ''' #scipy sobel filter
import scipy.misc import scipy.ndimage from scipy.misc.pilutil import Image from pylab import * # opening the image and converting it to grayscale a = Image.open('./images/obama.png').convert('L') figure() subplot(1,2,1), imshow(a), title("original Image") # performing the median filter b = scipy.ndimage.filters.median_filter(a,size=5, footprint=None,output=None,mode='reflect', cval=0.0,origin=0) # b is converted from an ndarray to an image b = scipy.misc.toimage(b) subplot(1,2,2), imshow(b), title("Median filter") #b.save('median_output.png') show()
import scipy.misc as mi import numpy as np from scipy.misc.pilutil import Image import scipy.ndimage as nd from skimage import filters from skimage import feature import matplotlib.pyplot as plt from skimage.morphology import watershed #from skimage.filter.thresholding import threshold_otsu a = Image.open('resize.jpg').convert('L') a = mi.fromimage(a) thresh = filters.threshold_otsu(a) im_otsu = a > thresh im_otsu = mi.toimage(im_otsu) im_otsu.save('otsu_semoutput.png') im_canny = feature.canny(a, sigma=3) fill_holes = nd.binary_fill_holes(im_canny) fill_holes = mi.toimage(fill_holes) im_canny = mi.toimage(im_canny) im_canny.save('test_canny.jpg') #to remove bushes try to adopt canny edges for leaves, fill holes and then negate obtained mask fill_holes.save('fill_holes.jpg') im_laplace = nd.gaussian_laplace(a, 3) im_laplace = mi.toimage(im_laplace) im_laplace.save('test_laplace.jpg')