ASIPPract-8December28,2021[9]:#!pipinstallscikit-imagefromskimage.ioimportimreadimportnumpyasnpfromskimage.colorimportrgb2grayimportmatplotlib.pylabaspylabfromskimage.morphologyimportbinary_erosion,rectangle[10]:#erosiondefplot_image(image,title=''):pylab.title(title,size=20),pylab.imshow(image)pylab.axis('off')#commentthislineifyouwantaxisticksim=rgb2gray(imread('images/clock2.jpg'))im[im<=0.5]=0#createbinaryimagewithfixedthreshold0.5im[im>0.5]=1pylab.gray()pylab.figure(figsize=(20,10))pylab.subplot(1,3,1),plot_image(im,'original')im1=binary_erosion(im,rectangle(1,5))pylab.subplot(1,3,2),plot_image(im1,'erosionwithrectanglesize(1,5)')im1=binary_erosion(im,rectangle(1,15))pylab.subplot(1,3,3),plot_image(im1,'erosionwithrectanglesize(1,15)')pylab.show()<Figuresize432x288with0Axes>1
[11]:#Dialationfromskimage.morphologyimportbinary_dilation,diskfromskimageimportimg_as_floatim=img_as_float(imread('images/tagore.png'))im=1-im[...,3]im[im<=0.5]=0im[im>0.5]=1pylab.gray()pylab.figure(figsize=(18,9))pylab.subplot(131)pylab.imshow(im)pylab.title('original',size=20)pylab.axis('off')fordinrange(1,3):pylab.subplot(1,3,d+1)im1=binary_dilation(im,disk(2*d))pylab.imshow(im1)pylab.title('dilationwithdisksize'+str(2*d),size=20)pylab.axis('off')pylab.show()<Figuresize432x288with0Axes>[12]:#Openingandclosingfromskimage.morphologyimportbinary_opening,binary_closing,binary_erosion,,binary_dilation,diskim=rgb2gray(imread('images/circles.jpg'))im[im<=0.5]=02
im[im>0.5]=1pylab.gray()pylab.figure(figsize=(20,10))pylab.subplot(1,3,1),plot_image(im,'original')im1=binary_opening(im,disk(12))pylab.subplot(1,3,2),plot_image(im1,'openingwithdisksize'+str(12))im1=binary_closing(im,disk(6))pylab.subplot(1,3,3),plot_image(im1,'closingwithdisksize'+str(6))pylab.show()<Figuresize432x288with0Axes>[13]:#Skeletonizingdefplot_images_horizontally(original,filtered,filter_name,sz=(18,7)):pylab.gray()pylab.figure(figsize=sz)pylab.subplot(1,2,1),plot_image(original,'original')pylab.subplot(1,2,2),plot_image(filtered,filter_name)pylab.show()fromskimage.morphologyimportskeletonizeim=img_as_float(imread('images/dynasaur.png')[...,3])threshold=0.5im[im<=threshold]=0im[im>threshold]=1skeleton=skeletonize(im)plot_images_horizontally(im,skeleton,'skeleton',sz=(18,9))<Figuresize432x288with0Axes>3
[14]:#Computingtheconvexhullfromskimage.morphologyimportconvex_hull_imageim=rgb2gray(imread('images/horse-dog.jpg'))threshold=0.5im[im<threshold]=0#converttobinaryimageim[im>=threshold]=1chull=convex_hull_image(im)plot_images_horizontally(im,chull,'convexhull',sz=(18,9))<Figuresize432x288with0Axes>[27]:#Removingsmallobjectsfromskimage.morphologyimportremove_small_objectsim=rgb2gray(imread('images/circles.jpg'))im[im>0.5]=1#createbinaryimagebythresholdingwithfixedthreshold0.54
im[im<=0.5]=0im=im.astype(np.bool)pylab.figure(figsize=(15,15))pylab.subplot(2,2,1),plot_image(im,'original')i=2foroszin[50,200,500]:im1=remove_small_objects(im,osz,connectivity=1)pylab.subplot(2,2,i),plot_image(im1,'removingsmallobjectsbelowsize',+str(osz))i+=1pylab.show()5
[28]:#Whiteandblacktop-hatsfromskimage.morphologyimportwhite_tophat,black_tophat,squareim=imread('images/tagore.png')[...,3]im[im<=0.5]=0im[im>0.5]=1im1=white_tophat(im,square(5))im2=black_tophat(im,square(5))pylab.figure(figsize=(15,15))pylab.subplot(1,2,1),plot_image(im1,'whitetophat')pylab.subplot(1,2,2),plot_image(im2,'blacktophat')pylab.show()[17]:#Extractingtheboundaryfromskimage.morphologyimportbinary_erosionim=rgb2gray(imread('images/horse-dog.jpg'))threshold=0.5im[im<threshold]=0im[im>=threshold]=1boundary=im-binary_erosion(im)plot_images_horizontally(im,boundary,'boundary',sz=(18,9))<Figuresize432x288with0Axes>6
[29]:#Fingerprintcleaningwithopeningandclosingim=rgb2gray(imread('images/fingerprint.jpg'))im[im<=0.5]=0#binarizeim[im>0.5]=1im_o=binary_opening(im,square(2))im_c=binary_closing(im,square(2))im_oc=binary_closing(binary_opening(im,square(2)),square(2))pylab.figure(figsize=(15,15))pylab.subplot(221),plot_image(im,'original')pylab.subplot(222),plot_image(im_o,'opening')pylab.subplot(223),plot_image(im_c,'closing')pylab.subplot(224),plot_image(im_oc,'opening+closing')pylab.show()7
[19]:#Grayscaleoperationsfromskimage.morphologyimportdilation,erosion,closing,opening,squareim=imread('images/zebras.jpg')im=rgb2gray(im)struct_elem=square(5)eroded=erosion(im,struct_elem)plot_images_horizontally(im,eroded,'erosion')<Figuresize432x288with0Axes>8