2016-02-27 112 views
0
mg = im2double(imread('http://i.stack.imgur.com/ZuiEt.jpg')); % read image and convert it to double in range [0..1] 
b = sum((1-img).^2, 3); % check how far each pixel from "white" 

% display 
figure; imshow(b > .5); title('non background pixels'); 

% use regionprops to get the bounding box 
st = regionprops(double(b > .5), 'BoundingBox'); % convert to double to avoid bwlabel of logical input 

rect = st.BoundingBox; % get the bounding box 

% display 
figure; imshow(img); 
hold on; rectangle('Position', rect); 

我使用此代码来裁剪矩形图像,但它不起作用。哪里不对?删除背景

回答

0

在第一行中,“mg”应该是“img”,我从URL中删除了额外的空格。在做出这些更改后,您的代码会生成一个完美的边界框。

当您声称代码无效时,问题究竟是什么?

+0

非常感谢您先生...问题是,它返回我的原始图像没有裁剪。我实际上是在计算机上使用银行券上的代码,而不一定是URL图像。 –

+0

我将>更改为<,它的工作原理...谢谢。 –

+0

这当然是可能的 - 不同的图像可能会颠倒前景和背景。但是,您提供的图像无需将“>”更改为“<”。 – Herb