Remove White Pixels from Image for Processing

I have some tissues stained with Masson's Trichrome. I have posted a sample picture below.
I am attempting to quantify the amount of blue stain as a fraction of the amount of red stain + blue stain in this image, and images like it. To accomplish this, I use a k-means algorithm (im is just the image in this case).
My only issue with this algorithm is that it outputs the percent of the total image that is blue, when I want it to just output the percent of the tissue that is blue. I would like to threshold out, based on brightness, the background pixels. Ideally, this would be before the k-means clustering algorithm would take place, so as to avoid any interference that background has on the algorithm. Unfortunately, I am a novice in matlab, and have no idea how I would do any of this. If any of you can provide some help, I'd greatly appreciate it.
im = imread(fullfile(obj.pathlist{i, 1}, obj.pathlist{i, 2}));
makecform('srgb2lab');
[seg_im, PercentStained(i)] = QuantifyArea.blueThreshold(im, cform);
function [seg_im, percent_stained] = blueThreshold(im, cform)
%BLUETHRESHOLD performs thresholding for Masson's Trichrome
%stain
%convert colours
lab_he = applycform(im,cform);
%isolate the colour and luminance data.
ab = double(lab_he(:, :, 2:3));
L = double(lab_he(:, :, 1));
%use a k-means clustering algorithm to isolate'red' and 'blue'
nrows = size(ab,1); ncols = size(ab,2);
ab = reshape(ab,nrows*ncols,2);
[cluster_idx, cluster_center] = kmeans(ab, 2,'distance','sqEuclidean', 'Replicates',3);
%order the cluster labels based on the cluster centers.
[~, sorted_cluster_index] = sort(cluster_center(:, 1), 1, 'ascend');
%isolate the red and blue regions
pixel_labels = reshape(cluster_idx,nrows,ncols);
blue_image = (pixel_labels == sorted_cluster_index(1));
red_image = (pixel_labels == sorted_cluster_index(2));
%don't include blue voxels that are within red regions, as they are
%likely noise
strel1 = strel('disk', 3, 8);
red_image = imdilate(red_image, strel1);
blue_image = imclose(blue_image, strel1);
%create an image overlay for quality assurance and calculate the percent
%blue voxels
blue_image = uint8(repmat(blue_image.*(red_image == 0 & L <= 210 & L >= 0), [1 1 3]));
seg_im = blue_image.*uint8(permute(repmat([0; 0; 255], [1, nrows, ncols]), [2 3 1])) + uint8(blue_image == 0).*uint8(permute(repmat([255; 0; 0], [1, nrows, ncols]), [2 3 1]));
%calculate the percent area of the image stained blue
percent_stained = sum(blue_image(:))/(3*nrows*ncols)*100;
end

回答(1 个)

Doesnt work fast, but you can separate any color you want:
clc, clear
A = imread('images\masson.jpg');
subplot(1,2,1)
imshow(A)
[m, n, ~] = size(A);
RGB = [190 226 250]; % color you want
e = 20; % color shift
B = zeros(m,n,3);
B = uint8(B)+255;
count = 0;
for i = 1:m
for j = 1:n
if pix_in(A(i,j,:), RGB, e)
count = count + 1;
B(i,j,:) = A(i,j,:);
end
end
end
subplot(1,2,2)
imshow(B)
percent = count/(m*n)*100;
% And the function
function b = pix_in(A,RGB,e)
B = [0 0 0];
for k = 1:3
t = double(A(1,1,k));
B(k) = abs(t-RGB(k)) < e;
end
if sum(B) == 3, b = true;
else b = false;
end
end

类别

帮助中心File Exchange 中查找有关 Images 的更多信息

产品

版本

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by