繁体   English   中英

分割后将色彩空间从RGB更改为HSV(OpenCv Python)

[英]Changing Color Space from RGB to HSV after segmentation (OpenCv Python)

我正在分割图像,然后将其转换为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)

以下是转换为HSV之后的输出。 输出量

我们可以观察到,与原始图像相比,黑色空间的粒度有所减小。 我怎么解决这个问题?

谢谢您的帮助。

照片取自4

您的代码显示了您应用了GaussianBlur()cv2.adaptiveThreshold()cv2.morphologyEx() ,所有这些过滤都可能会使所生成的细节在某种程度上丢失。

如果需要将色彩空间从BGR转换为HSV,请使用cv2.cvtColor(img, cv2.COLOR_BGR2HSV) ,然后在将图像转换为HSV之前以及进行进一步的HSV色彩处理之前,只需进行最少的预处理以减少失真。空间。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM