This content is powered by Balige Publishing. Visit this link
Part 1 Part 2 Part 3
Tutorial Steps To Implement Image Denoising Using Non-local Means Denoising, Total Variation Filter, Bilateral Filter, and Wavelet Denoising Filter.
First, use cv2.fastNlMeansDenoisingColored() function which is the implementation of Non-local Means Denoising algorithm as defined below:
cv2.fastNlMeansDenoisingColored(src[, dst[, \
h[, hColor[, templateWindowSize[, searchWindowSize]]]]])
The parameters are:
The three other algorithms you will use in this project are total variation filter, bilateral filter, and wavelet denoising filter. You need to import the three algorithms using this import statement as follows:
from skimage.restoration import (denoise_tv_chambolle,\denoise_bilateral, denoise_wavelet, estimate_sigma)
Add a Combo Box widget onto form in Qt Designer. Set its objectName property as cboDenoising. Double click on that widget and add four items as shown in Figure below.
Add five new Label widgets and set their text properties as h, patch, search, weight, and sigma.
Add a new Spin Box widget and set its objectName property as sbH. Set its value property to 15, its minimum property to 3, its maximum property to 99, and its singleStep to 1.
Add another Spin Box widget and set its objectName property as sbPatch. Set its value property to 13, its minimum property to 3, its maximum property to 99, and its singleStep to 2.
Add another Spin Box widget and set its objectName property as sbSearch. Set its value property to 29, its minimum property to 3, its maximum property to 99, and its singleStep to 2.
Add a new Horizonal Slider widget and set its objectName property as hsWeight. Set its value property to 0, its minimum property to 0, its maximum property to 10, and its singleStep to 1.
Add a new Horizonal Slider widget and set its objectName property as hsSigma. Set its value property to 0, its minimum property to 0, its maximum property to 100, and its singleStep to 1.
Add two new Line Edit widgets and set their objectName properties to leWeight and leSigma. Set their text properties to 0.1 and 0.25. The newly modified form is shown in Figure below
Define a new method, choose_denoising(), to read currentText property of cboDenoising and implement image denoising accordingly:
def choose_denoising(self,img):strCB = self.cboDenoising.currentText()h = self.sbH.value()patch = self.sbPatch.value()size = self.sbSearch.value()weightVal = float(self.leWeight.text())sigmaVal = float(self.leSigma.text())noisy = self.choose_noise(img)if strCB == 'Non-Local Means Denoising':self.hsWeight.setEnabled(False)self.leWeight.setEnabled(False)self.hsSigma.setEnabled(False)self.leSigma.setEnabled(False)self.sbH.setEnabled(True)self.sbPatch.setEnabled(True)self.sbSearch.setEnabled(True)denoised_img = cv2.fastNlMeansDenoisingColored(\noisy,None,h,h,patch,size)return denoised_imgif strCB == 'Total Variation Filter':self.hsWeight.setEnabled(True)self.leWeight.setEnabled(True)self.hsSigma.setEnabled(False)self.leSigma.setEnabled(False)self.sbH.setEnabled(False)self.sbPatch.setEnabled(False)self.sbSearch.setEnabled(False)denoised_img = denoise_tv_chambolle(noisy, \weight=weightVal, multichannel=True)cv2.normalize(denoised_img, \denoised_img, 0, 255, cv2.NORM_MINMAX, dtype=-1)denoised_img = denoised_img.astype(np.uint8)return denoised_imgif strCB == 'Bilateral Filter':self.hsWeight.setEnabled(False)self.leWeight.setEnabled(False)self.hsSigma.setEnabled(True)self.leSigma.setEnabled(True)self.sbH.setEnabled(False)self.sbPatch.setEnabled(False)self.sbSearch.setEnabled(False)denoised_img = denoise_bilateral(noisy, \sigma_color=sigmaVal, sigma_spatial=15, \multichannel=True)cv2.normalize(denoised_img, \denoised_img, 0, 255, cv2.NORM_MINMAX, dtype=-1)denoised_img = denoised_img.astype(np.uint8)return denoised_imgif strCB == 'Wavelet Denoising Filter':self.hsWeight.setEnabled(False)self.leWeight.setEnabled(False)self.hsSigma.setEnabled(False)self.leSigma.setEnabled(False)self.sbH.setEnabled(False)self.sbPatch.setEnabled(False)self.sbSearch.setEnabled(False)denoised_img = denoise_wavelet(noisy, \multichannel=True, convert2ycbcr=True, \rescale_sigma=True)cv2.normalize(denoised_img, denoised_img, 0, 255, \cv2.NORM_MINMAX, dtype=-1)denoised_img = denoised_img.astype(np.uint8)return denoised_img
Define a new method, do_denoising(), to call choose_denoising() and convert the result from BGR into RGB color space:
def do_denoising(self):denoised = self.choose_denoising(img)height, width, channel = denoised.shapebytesPerLine = 3 * widthcv2.cvtColor(denoised, cv2.COLOR_BGR2RGB, denoised)qImg = QImage(denoised, width, height, \bytesPerLine, QImage.Format_RGB888)pixmap = QPixmap.fromImage(qImg)self.display_image(pixmap, denoised, self.labelImage, \self.widgetHistFilter, 'Histogram of Denoised Image')
Define two new methods, set_hsWeight() and set_hsSigma(), to set text properties of leWeight and leSigma and to invoke do_denoising() method.
def set_hsWeight(self, value):self.leWeight.setText(str(round((value/10),2)))self.do_denoising()def set_hsSigma(self, value):self.leSigma.setText(str(round((value/100),2)))self.do_denoising()
Connect currentIndexChanged() event of cboDenoising to do_denoising(). Connect valueChanged() event of sbH, sbPatch, and sbSearch to do_denoising(). Put them inside __init__() method as follows:
self.cboDenoising.currentIndexChanged.connect(self.do_denoising)self.sbH.valueChanged.connect(self.do_denoising)self.sbPatch.valueChanged.connect(self.do_denoising)self.sbSearch.valueChanged.connect(self.do_denoising)
Connect valueChanged() event of hsWeight to set_hsWeight() and valueChanged() event of hsSigma to set_hsSigma(). Put them inside __init__() method as follows:
self.hsWeight.valueChanged.connect(self.set_hsWeight)self.hsSigma.valueChanged.connect(self.set_hsSigma)
Run image_processing.py. Choose test image and select Gaussian noise type to generate noisy image. Then select Non-Local Means Denoising and you can select h, patch, and search values that suit you better. The result is shown in Figure below.
Then select Total Variation Filter and you can select weight value that suits you better. The result is shown in Figure below.
Choose another test image and select Salt & Pepper noise type to generate noisy image. Then select Bilateral Filter and you can select sigma value that suits you better. The result is shown in Figure below.
Then select Wavelet Denoising Filter. The result is shown in Figure below.
The following is the last version of image_processing.py script:
|
|
No comments:
Post a Comment