分割后将颜色空间从RGB更改为HSV(OpenCv Python)
我正在分割图像,然后将其转换为HSV格式。但在将其转换为HSV并分离出每个通道后,分割区域的粒度丢失。下面是分段代码分割后将颜色空间从RGB更改为HSV(OpenCv Python),python,opencv,numpy,Python,Opencv,Numpy,我正在分割图像,然后将其转换为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/
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)
以下是转换为HSV后的输出。
我们可以观察到,与原始黑空间相比,黑空间的粒度减小了。我怎样才能解决这个问题
谢谢你的帮助
从您的代码中拍摄的照片显示,您应用了
GaussianBlur()
、cv2.adaptiveThreshold()
和cv2.morphologyEx()
,所有这些过滤都可能会在一定程度上使结果图像中的细节丢失
如果您需要将颜色空间从BGR转换为HSV,
cv2.cvtColor(img,cv2.color\u BGR2HSV)
,那么您可以在将图像转换为HSV之前,以及在HSV
颜色空间中进一步处理之前,只需进行最小的预处理,以减少失真。在上述问题中发布图像。随着时间的推移,链接的图像可能无法访问。如果只需要在HSV颜色空间中保存图像,为什么必须应用过滤器并对图像进行预处理?这就是JPEG for ya。不要使用有损压缩,尤其不要用于中间结果。@thewaywewere,谢谢你的建议!我做了适当的修改。我可以解决这个问题@Satwikhamidi Geat听说你已经解决了这个问题。我将把我的建议作为答复,请您接受。:)