我正在分割图像,然后将其转换为HSV格式。但是在将其转换为HSV并分离出每个通道之后,分段区域的粒度就会丢失。以下是细分代码。
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)
以下是相应的输出。
现在,在一个新程序中,我尝试读取此输出并将其转换为HSV格式。以下是代码。
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)
我们可以观察到,与原始的相比,黑色空间的粒度减小了。我怎么解决这个问题?
感谢您的帮助。
从4
拍摄的照片答案 0 :(得分:1)
您的代码显示您已应用GaussianBlur()
,cv2.adaptiveThreshold()
和cv2.morphologyEx()
,所有这些过滤都可能会使结果图片中的细节在某种程度上丢失。
如果您需要将色彩空间从BGR转换为HSV cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
,那么您可以在将图像转换为HSV之前进行最少的预处理以减少失真,然后再进一步处理{{1}色空间。