我正在研究皮膚斑點的識別。為此,我使用了許多具有不同噪聲的圖像。這些噪音之一是毛發,因為我在染色區域 (ROI) 上有毛發的圖像。如何減少或消除這些類型的圖像噪聲?下面的代碼減少了毛發所在的區域,但不會去除感興趣區域(ROI)上方的毛發。import numpy as npimport cv2IMD = 'IMD436'# Read the image and perfrom an OTSU thresholdimg = cv2.imread(IMD+'.bmp')gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)ret, thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)# Remove hair with openingkernel = np.ones((2,2),np.uint8)opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel, iterations = 2)# Combine surrounding noise with ROIkernel = np.ones((6,6),np.uint8)dilate = cv2.dilate(opening,kernel,iterations=3)# Blur the image for smoother ROIblur = cv2.blur(dilate,(15,15))# Perform another OTSU threshold and search for biggest contourret, thresh = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)cnt = max(contours, key=cv2.contourArea)# Create a new mask for the result imageh, w = img.shape[:2]mask = np.zeros((h, w), np.uint8)# Draw the contour on the new mask and perform the bitwise operationcv2.drawContours(mask, [cnt],-1, 255, -1)res = cv2.bitwise_and(img, img, mask=mask)# Display the resultcv2.imwrite(IMD+'.png', res)cv2.imshow('img', res)cv2.waitKey(0)cv2.destroyAllWindows()出口:如何去除感興趣區域頂部的毛發?使用的圖像:
添加回答
舉報
0/150
提交
取消