在HSV颜色空间(Python,OpenCV,图像分析)中定义组织学图像蒙版的颜色范围: [英] Defining color range for histologic image mask within HSV colorspace (Python, OpenCV, Image-Analysis):

查看:820
本文介绍了在HSV颜色空间(Python,OpenCV,图像分析)中定义组织学图像蒙版的颜色范围:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了根据颜色将组织切片分成几层,我修改了OpenCV社区提供的一些广泛分布的代码(1)。我们的染色程序标记具有不同颜色的不同细胞类型的组织横切面(B细胞是红色的,巨噬细胞是棕色的,背景nuceli具有蓝色)。



我有兴趣只选择图像的品红色和棕色部分。



这是我尝试为洋红色素制作面膜:

  import cv2 
import numpy as np

def mask_builder(filename,hl,hh,sl,sh,vl,vh):
#load image,转换为hsv
bgr = cv2.imread(filename)
hsv = cv2.cvtColor(bgr,cv2.COLOR_BGR2HSV)
#set的下限和上限范围根据争论
lower_bound = np.array([hl,sl,vl],dtype = np.uint8)
upper_bound = np.array([hh,sh,vh],dtype = np。 uint8)
返回cv2.inRange(hsv,lower_bound,upper_bound)

mask = mask_builder('sample 20 138 1.jpg',170,180,0,200,0,230)
cv2。 imwrite('mask.jpg',mask)

到目前为止,试错法导致效果不佳:



任何人都可以建议一种更智能的方法来在HSV色彩空间内进行阈值处理吗?我已经尽力在以前的帖子中搜索答案,但由于图像的性质,似乎这些颜色范围特别难以定义。



参考文献:


  1. 与Colorspaces分离:

    解决方案

    我很高兴你找到了答案。



    我会建议一种可行的替代方法。不幸的是我不熟悉python,所以你需要找到如何在python(它的基本)中编码。



    如果我有你的第一张图像HSV阈值,我会使用



    请注意,我是从糟糕的HSV细分开始的。如果你尝试更好的结果可能会改善。另外,使用内核大小进行侵蚀和扩张。


    In an effort to separate histologic slides into several layers based on color, I modified some widely distributed code (1) available through OpenCV's community. Our staining procedure marks different cell types of tissue cross sections with different colors (B cells are red, Macrophages are brown, background nuceli have a bluish color).

    I'm interested in selecting only the magenta-colored and brown parts of the image.

    Here's my attempt to create a mask for the magenta pigment:

        import cv2
        import numpy as np
    
        def mask_builder(filename,hl,hh,sl,sh,vl,vh):
            #load image, convert to hsv
            bgr = cv2.imread(filename)
            hsv = cv2.cvtColor(bgr, cv2.COLOR_BGR2HSV)
            #set lower and upper bounds of range according to arguements
            lower_bound = np.array([hl,sl,vl],dtype=np.uint8)
            upper_bound = np.array([hh,sh,vh],dtype=np.uint8)
            return cv2.inRange(hsv, lower_bound,upper_bound)
    
        mask = mask_builder('sample 20 138 1.jpg', 170,180, 0,200, 0,230)
        cv2.imwrite('mask.jpg', mask)
    

    So far a trial and error approach has produced poor results:

    The can anyone suggest a smarter method to threshhold within the HSV colorspace? I've done my best to search for answers in previous posts, but it seems that these color ranges are particularly difficult to define due to the nature of the image.

    References:

    1. Separation with Colorspaces: http://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_imgproc/py_colorspaces/py_colorspaces.html
    2. python opencv color tracking
    3. BGR separation: http://www.pyimagesearch.com/2014/08/04/opencv-python-color-detection/

    UPDATE: I've found a working solution to my problem. I increased the lower bound of 'S' and 'V' by regular intervals using a simple FOR control structure, outputing the results for each test image and choosing the best. I found my lower bounds for S and V should be set at 100 and 125. This systematic method of trial and error produced better results:

    解决方案

    I am happy you found your answer.

    I will suggest an alternate method that might work. Unfortunately I am not proficient with python so you'll need to find out how to code that in python (its basic).

    If I had the firs image you have after the HSV threshold, I would use morphological operations to get the information I want.

    I would probably give it a go to "closing", but if it doesnt work I would first dilate, then fill and then erode the same amount firstly dilated.

    Probably after this first step you'll need to delete the small "noise" blobs you have around and you'll get the image.

    This is how it would be in Matlab (showing this mainly so you can see the results):

    I=imread('http://i.stack.imgur.com/RlH4V.jpg');
    
    I=I>230;                        % Create Black and white image (this is because in stackoverflow its a jpg)
    ker=strel('square',3);          % Create a 3x3 square kernel
    
    I1=imdilate(I,ker);             % Dilate
    I2=imfill(I1,'holes');          % Close
    I3=imerode(I2,ker);             % Erode
    
    Ilabel=bwlabel(I3,8);            % Get a label per independent blob
    
    % Get maximum area blob (you can do this with a for in python easily)
    areas = regionprops(Ilabel,'Centroid','Area','PixelIdxList');
    [~,index] = max([areas.Area]);   % Get the maximum area
    
    Imask=Ilabel==index;             % Get the image with only the max area.
    
    
    
    % Plot: This is just matlab code, no relevance
    
    figure;
    subplot(131)
    title('Dialted')
    imshow(I1);
    subplot(132)
    title('Closed')
    imshow(I2);
    subplot(133)
    title('Eroded')
    imshow(I3);
    
    
    figure;
    imshow(imread('http://i.stack.imgur.com/ZqrF9.jpg'))
    hold on
    h=imshow(bwperim(Imask));
    set(h,'alphadata',Imask/2)
    

    Note that I started from the "bad" HSV segmentation. If you try a better one the results may improve. Also, play with the kernel size for the erosion and dilation.

    这篇关于在HSV颜色空间(Python,OpenCV,图像分析)中定义组织学图像蒙版的颜色范围:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆