python图像色彩分析_在HSV颜色空间(Python,OpenCV,图像分析)中定义组织学图像蒙版的颜色范围:...

1586010002-jmsa.png

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.

ZqrF9.jpg

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:

RlH4V.jpg

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:

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:

kK01C.jpg

解决方案

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)

0Jz2i.png

QxxQG.jpg

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值