简体   繁体   English

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

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

I am segmenting an image and then converting it into HSV format. 我正在分割图像,然后将其转换为HSV格式。 But after converting it into HSV and separating out each of the channels, the granularity of the segmented region is lost. 但是,在将其转换为HSV并分离出每个通道之后,就会丢失分段区域的粒度。 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 input image. 以下是输入图像。 输入项

Following is the corresponding output. 以下是相应的输出。

输出量 Now, In a new program I try to read this output and convert it into HSV format. 现在,在一个新程序中,我尝试读取此输出并将其转换为HSV格式。 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. 以下是转换为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 照片取自4

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

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

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

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