I am working on a project to identify healthy and unhealthy plants. So that I tried out to remove the background from the plants before feeding the neural network and train the model. But when I get output it was contained noise in the foreground image. What can I do to remove the noise from the foreground image.
Here is my code.
import cv2
import numpy as np
import matplotlib .pyplot as plt
from PIL import Image
original_img=cv2.imread('C:\\pictures\\plants\\download2.jpg')
img=cv2.imread('C:\\pictures\\plants\\download2.jpg',0)
blur = cv2.GaussianBlur(img, (5, 5), 0)
sobelx8u= cv2.Sobel(blur,cv2.CV_8U,1,0,ksize=5)
sobelx64f = cv2.Sobel(blur,cv2.CV_64F,1,0,ksize=5)
abs_sobel64f = np.absolute(sobelx64f)
sobel_8u = np.uint8(abs_sobel64f)
plt.subplot(1,3,1),plt.imshow(blur,cmap = 'gray')
plt.title('Original'), plt.xticks([]), plt.yticks([])
plt.subplot(1,3,2),plt.imshow(sobelx8u,cmap = 'gray')
plt.title('Sobel CV_8U'), plt.xticks([]), plt.yticks([])
plt.subplot(1,3,3),plt.imshow(sobel_8u,cmap = 'gray')
plt.title('Sobel abs(CV_64F)'), plt.xticks([]), plt.yticks([])
plt.show()
resulted plots of plt.show() command 1
median = cv2.medianBlur(sobel_8u,3)
plt.subplot(121),plt.imshow(sobel_8u)
plt.title('Edge Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(median,cmap = 'gray')
plt.title('median Image'), plt.xticks([]), plt.yticks([])
plt.show()
resulted plot for plt.show() command 2
ret2,th2 = cv2.threshold(median,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
plt.subplot(121),plt.imshow(median)
plt.title('median image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(th2,cmap = 'gray')
plt.title(' threshold Image'), plt.xticks([]), plt.yticks([])
plt.show()
resulted plot for plt.show() command3
kernel = np.ones((3,3),np.uint8)
th3_inv=cv2.morphologyEx(th2, cv2.MORPH_CLOSE, kernel)
plt.subplot(121),plt.imshow(th2)
plt.title(' threshold Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(th3_inv,cmap = 'gray')
plt.title('morphed image '), plt.xticks([]), plt.yticks([])
plt.show()
resulted plot for plt.show() command 4
mask=np.zeros(img.shape[:2],np.uint8)
mask_inv = cv2.bitwise_not(mask)
rgb_image=cv2.cvtColor(original_img,cv2.COLOR_BGR2RGB)
kernel = np.ones((3,3),np.uint8)
cv2.morphologyEx(th3_inv, cv2.MORPH_OPEN, kernel)
img1_bg = cv2.bitwise_and(blur,th3_inv,mask = mask_inv)
plt.subplot(121),plt.imshow(rgb_image)
plt.title('rgb Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(img1_bg,cmap = 'gray')
plt.title('background removed image '), plt.xticks([]), plt.yticks([])
plt.show()
resulted plot for plt.show() command5
bgr_new_image = cv2.cvtColor(img1_bg, cv2.COLOR_GRAY2BGR)*255
rgb_new_image=cv2.cvtColor(bgr_new_image,cv2.COLOR_BGR2RGB)*255
final_image=cv2.bitwise_and(rgb_new_image,rgb_image)
cv2.fastNlMeansDenoisingColored(final_image,None,10,10,7,21)
plt.subplot(121),plt.imshow(rgb_image)
plt.title('rgb Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(final_image,cmap = 'gray')
plt.title('background removed image '), plt.xticks([]), plt.yticks([])
plt.show()
resulted plot for plt.show() command 6
cv2.imshow('final output',final_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
Since the final background removed image is too noisy and also there seems to be some color differences in the foreground when compare with the original image what can do as improvements?