形態學的操作主要是去除影響圖像形狀和信息的噪聲。形態學運算在圖像分割中非常有用,可以得到無噪聲的二值圖像。
形態學方法當圖像經過預處理進行增強和閾值等性能操作時,圖像就有可能得到一些噪聲。從而導致圖像中存在像素信息不平衡的問題。形態學的操作主要是去除影響圖像形狀和信息的噪聲。形態學運算在圖像分割中非常有用,可以得到無噪聲的二值圖像。基本的形態操作是侵蝕和膨脹。下面對這兩種操作進行說明:膨脹
在放大操作中,如果物體是白色的,那麼白色像素周圍的像素就會增大。它增加的區域取決於物體像素的形狀。膨脹過程增加了對象的像素數,減少了非對象的像素數。
import numpy as np
import imutils
import cv2#reading the input image
img = cv2.imread('thumb.png') #reads the image
#cv2.imwrite('Input_image.jpg',image)
#Resizing the image
scale_percent = 70
width = int(img.shape[1] * scale_percent / 100)
height = int(img.shape[0] * scale_percent / 100)
dim = (width, height)
# resize the input image
image = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)
kernel = np.ones((1,1), dtype = "uint8")/9
dilation = cv2.dilate(image,kernel,iterations = 1)
cv2.imwrite('dilation.jpg', dilation)
kernel = np.ones((2,2), dtype = "uint8")/9
dilation = cv2.dilate(image,kernel,iterations = 1)
cv2.imwrite('dilation.jpg', dilation)
kernel = np.ones((2,2), dtype = "uint8")/9
dilation = cv2.dilate(image,kernel,iterations = 3)
cv2.imwrite('dilation.jpg', dilation)
kernel = np.ones((2,2), dtype = "uint8")/9
dilation = cv2.dilate(image,kernel,iterations = 5)
cv2.imwrite('dilation.jpg', dilation)
kernel = np.ones((3,3), dtype = "uint8")/9
dilation = cv2.dilate(image,kernel,iterations = 2)
cv2.imwrite('dilation.jpg', dilation)侵蝕侵蝕函數正好與膨脹功函數相反。侵蝕作用使物體體積變小。侵蝕過程增加了非目標像素,減少了目標像素。import numpy as np
import imutils
import cv2
#reading the input image
img = cv2.imread('thumb.png')
#cv2.imwrite('Input_image.jpg',image)
#Resizing the image
scale_percent = 70
width = int(img.shape[1] * scale_percent / 100)
height = int(img.shape[0] * scale_percent / 100)
dim = (width, height)
# resize the input image
image = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)
kernel = np.ones((1,1), dtype = "uint8")/9
erosion = cv2.erode(image, kernel, iterations = 1)
cv2.imwrite('erosion.jpg', erosion)
kernel = np.ones((2,2), dtype = "uint8")/9
erosion = cv2.erode(image, kernel, iterations = 2)
cv2.imwrite('erosion.jpg', erosion)
kernel = np.ones((2,2), dtype = "uint8")/9
erosion = cv2.erode(image, kernel, iterations = 3)
cv2.imwrite('erosion.jpg', erosion)
kernel = np.ones((2,2), dtype = "uint8")/9
erosion = cv2.erode(image, kernel, iterations = 5)
cv2.imwrite('erosion.jpg', erosion)
kernel = np.ones((5,5), dtype = "uint8")/9
erosion = cv2.erode(image, kernel, iterations = 2)
cv2.imwrite('erosion.jpg', erosion)開操作此方法可用於從圖像中去除噪聲。該方法的工作功能是先腐蝕再膨脹,以保持物體像素的原始性,去除背景中的小噪聲。
import numpy as np
import imutils
import cv2
#reading the input image
img = cv2.imread('11.png')
kernel = np.ones((5,5), dtype = "uint8")/9
opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
cv2.imwrite('opening.jpg', opening)此方法可用於從圖像中去除噪聲。這種方法的工作功能是先膨脹再腐蝕,去除內部的小噪聲。import numpy as np
import imutils
import cv2
#reading the input image
img = cv2.imread('thumb.png')
kernel = np.ones((9,9), dtype = "uint8")/9
closing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
cv2.imwrite('closing.jpg', closing)形態學梯度import numpy as np
import imutils
import cv2
#reading the input image
img = cv2.imread('g1.png')
kernel = np.ones((6,6), dtype = "uint8")/9
gradient = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel)
cv2.imwrite('gradient.jpg', gradient)總結這些操作是處理二進位圖像的一種非常簡單的方法,也是圖像處理應用程式中預處理的一部分。
編輯:文婧