ASIP-Pract-2December27,2021[19]:#AdvancedSignalandImageProcessingPractical#CodecompiledandrunbyR.K.Maurya#Credittooriginalauthorsandprogrammers.#Formorelearningresourcesvisithttps://www.rajeshmaurya.in[22]:#Writeprogramtodemonstratethefollowingaspectsofsignalandimage,processing#a.Convolutionongreyandcolorimages#b.TemplateMatching[9]:#Importnecessarylibrariesandpakagesfromskimage.ioimportimread,imshow,showfromskimage.colorimportrgb2grayimportnumpyasnpfromscipyimportndimage,misc,signalimportmatplotlib.pylabaspylab[10]:#ConvolutionongreyandColorImagesim=rgb2gray(imread('images/rkm.jpg')).astype(float)print(np.max(im))print(im.shape)blur_box_kernel=np.ones((3,3))/9edge_laplace_kernel=np.array([[0,1,0],[1,-4,1],[0,1,0]])im_blurred=signal.convolve2d(im,blur_box_kernel)im_edges=np.clip(signal.convolve2d(im,edge_laplace_kernel),0,1)fig,axes=pylab.subplots(ncols=3,sharex=True,sharey=True,figsize=(18,6))axes[0].imshow(im,cmap=pylab.cm.gray)axes[0].set_title('OriginalImage',size=20)axes[1].imshow(im_blurred,cmap=pylab.cm.gray)axes[1].set_title('BoxBlur',size=20)axes[2].imshow(im_edges,cmap=pylab.cm.gray)axes[2].set_title('LaplaceEdgeDetection',size=20)foraxinaxes:ax.axis('off')pylab.show()0.9858333333333333(246,233)1
[17]:#Applyingconvolutiontoacolor(RGB)imageim=imread('images/rkm.jpg').astype(np.float)#readasfloatprint(np.max(im))sharpen_kernel=np.array([0,-1,0,-1,5,-1,0,-1,0]).reshape((3,3,1))emboss_kernel=np.array(np.array([[-2,-1,0],[-1,1,1],[0,1,2]])).reshape((3,3,,1))im_sharp=ndimage.convolve(im,sharpen_kernel,mode='nearest')im_sharp=np.clip(im_sharp,0,255).astype(np.uint8)#clip(0to255)and,converttounsignedintim_emboss=ndimage.convolve(im,emboss_kernel,mode='nearest')im_emboss=np.clip(im_emboss,0,255).astype(np.uint8)pylab.figure(figsize=(10,15))pylab.subplot(131),pylab.imshow(im.astype(np.uint8)),pylab.axis('off')pylab.title('OriginalImage',size=25)pylab.subplot(132),pylab.imshow(im_sharp),pylab.axis('off')pylab.title('SharpenedImage',size=25)pylab.subplot(133),pylab.imshow(im_emboss),pylab.axis('off')pylab.title('EmbossedImage',size=25)pylab.tight_layout()pylab.show()255.02
[18]:#Templatematchingwithcross-correlationbetweentheimageandtemplateface_image=misc.face(gray=True)-misc.face(gray=True).mean()template_image=np.copy(face_image[300:365,670:750])#righteyetemplate_image-=template_image.mean()face_image=face_image+np.random.randn(*face_image.shape)*50#addrandom,noisecorrelation=signal.correlate2d(face_image,template_image,boundary='symm',,mode='same')y,x=np.unravel_index(np.argmax(correlation),correlation.shape)#findthe,matchfig,(ax_original,ax_template,ax_correlation)=pylab.subplots(3,1,,figsize=(6,15))ax_original.imshow(face_image,cmap='gray')ax_original.set_title('Original',size=20)ax_original.set_axis_off()ax_template.imshow(template_image,cmap='gray')ax_template.set_title('Template',size=20)ax_template.set_axis_off()ax_correlation.imshow(correlation,cmap='afmhot')ax_correlation.set_title('Cross-correlation',size=20)ax_correlation.set_axis_off()ax_original.plot(x,y,'ro')fig.show()<ipython-input-18-cea32e39e318>:19:UserWarning:Matplotlibiscurrentlyusingmodule://ipykernel.pylab.backend_inline,whichisanon-GUIbackend,socannotshowthefigure.fig.show()3