经过2到3天的搜索,我仍然找不到解决问题的方法。
我想在没有阴影的情况下创建鼠标分段。问题是如果我设法移除阴影我也删除尾部和脚部这是一个问题。阴影来自鼠标所在的竞技场墙壁。
我想从灰度图像中删除阴影,但我不知道如何做到这一点。首先,我删除了图像的背景,然后获得了以下图片。
edit1:感谢你的答案,当阴影没有碰到鼠标时效果很好。这就是我得到的结果:
来自这张原始图片:
我从tif文件中提取每个帧并为每个帧应用代码。这是我使用的代码:
for k=1:1000
%reads image
I = imread('souris3.tif',k);
%first stage: perform thesholding and fill holes
seg = I >20000;
seg = imfill(seg,'holes');
%fixes the missing tail problem
%extract edges, and add them to the segmentation.
edges = edge(I);
seg = seg | edges;
%fill holes (again)
seg = imfill(seg,'holes');
%find all the connected components
CC = bwconncomp(seg,8);
%keeps only the biggest CC
numPixels = cellfun(@numel,CC.PixelIdxList);
[biggest,idx] = max(numPixels);
seg = zeros(size(edges));
seg(CC.PixelIdxList{idx}) = 1;
imshow(seg);
end
我使用命令impixelinfo
选择20000步骤,因为图像在uint16
中,它是鼠标的平均值。
如果你想拥有tif文件,这是链接:
感谢您的帮助。
答案 0 :(得分:8)
我建议采用以下方法:
代码:
%reads image
I = rgb2gray(imread('mSWm4.png'));
%defines thersholds (you may want to tweak these thresholds, or find
%a way to calculate it automatically).
FIRST_STAGE_THRESHOLD = 70;
IM_BOUNDARY_RELEVANCE_THRESHOLD = 10;
%perform thesholding and fill holes, the tail is still missing
seg = I > FIRST_STAGE_THRESHOLD;
seg = imfill(seg,'holes');
%second stage fix the missing tail problem:
%extract edges from relevant areas (in which the matter is not too dark), and add them to the segmentation.
%the boundries of the image which are close enough to edges are also considered as edges
edges = edge(I);
imageBoundries = ones(size(I));
imageBoundries(2:end-1,2:end-1) = 0;
relevantDistFromEdges = bwdist(edges) > IM_BOUNDARY_RELEVANCE_THRESHOLD;
imageBoundries(bwdist(edges) > IM_BOUNDARY_RELEVANCE_THRESHOLD) = 0;
seg = seg | (edges | imageBoundries);
%fill holes (again) and perform noise cleaning
seg = imfill(seg,'holes');
seg = getBiggestCC(imopen(seg,strel('disk',1)));
getBiggestCC功能:
function [ res ] = getBiggestCC(mask)
CC = bwconncomp(mask,8);
numPixels = cellfun(@numel,CC.PixelIdxList);
[~,idx] = max(numPixels);
res = zeros(size(mask));
res(CC.PixelIdxList{idx}) = 1;
end
<强>结果
每个阶段的结果:
结果 图1结果:
图像2结果:
另一种观点(细分为红色):