Determining the color range for the histological image mask in the HSV color space (Python, OpenCV, Image-Analysis):

To split the histological slides into several layers based on color, I changed some of the common codes (1) available through the OpenCV community. In our staining procedure, various types of cells of tissue sections of different colors are noted (B cells are red, macrophages are brown, background nodules have a bluish tint).

I am interested in choosing only the magenta and brown parts of the image. raw_image

Here is my attempt to create a mask for 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, trial and error has led to poor results: mask_output

- HSV? , , , - .

:

UPDATE: . "S" "V" , FOR, . , S V 100 125. : better_mask

+2
2

, .

, . , python, , python ( ).

, HSV, , .

, , "", , , , erode .

, "" , , .

Matlab ( , ):

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)

enter image description here enter image description here

, "" HSV. , . , .

+1

( "S" "V" ) , "S" "V". , , , - ​​ .

, , .

0

Source: https://habr.com/ru/post/1607979/


All Articles