Reputation: 21
I am segmenting an image and then converting it into HSV format. But after converting it into HSV and separating out each of the channels, the granularity of the segmented region is lost. Following is the segmentation code.
import cv2
from os import listdir
from os.path import isfile, join
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
path = "C:/Users/Intern/Desktop/dataset/rust images/"
files_test = [f for f in listdir(path+ 'Input/') if isfile(join(path+ 'Input/', f))]
for img_name in files_test:
img = cv2.imread(path + "Input/" + img_name)
gray_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
gray_blur = cv2.GaussianBlur(gray_img, (7, 7), 0)
adapt_thresh_im = cv2.adaptiveThreshold(gray_blur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 20)
max_thresh, thresh_im = cv2.threshold(gray_img, 100, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
thresh = cv2.bitwise_or(adapt_thresh_im, thresh_im)
kernel = np.ones((3,3),np.uint8)
opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel, iterations = 2)
sure_bg = cv2.dilate(thresh,kernel,iterations=2)
img[sure_bg == 0] = [0,0,0]
cv2.imwrite(path + "Segmented/" + img_name, img)
Following is the corresponding output.
Now, In a new program I try to read this output and convert it into HSV format. Following is the code.
import cv2
from os import listdir
from os.path import isfile, join
import numpy as np
path = "C:/Users/Intern/Desktop/dataset/rust images/"
files_test = [f for f in listdir(path+ "Segmented/") if isfile(join(path+ "Segmented/", f))]
for img_name in files_rust:
img = cv2.imread(path + "Segmented/" + img_name)
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
print img_hsv.shape
h, s, v = cv2.split(img_hsv)
cv2.imshow("hsv image", s)
cv2.waitKey(0)
Following is the output after converting into HSV.
We can observe that compared to the original one the granularity of the black spaces has reduced. How can I solve this problem?
Thanks for the help.
Photograph taken from 4
Upvotes: 0
Views: 1526
Reputation: 8626
You code showed you applied GaussianBlur()
, cv2.adaptiveThreshold()
and cv2.morphologyEx()
, all those filtering would likely make the details lost in some degree in the resulted image.
If you need to convert color space from BGR to HSV, cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
, then you may just do minimal preprocessing to reduce the distortion before converting the image to HSV, and before you further any processing in HSV
color space.
Upvotes: 1