Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 删除背景_Matlab - Fatal编程技术网

Matlab 删除背景

Matlab 删除背景,matlab,Matlab,我用这段代码裁剪了一个矩形图像,但它不起作用。怎么了?在第一行,“mg”应该是“img”,我从你的URL中删除了多余的空格。在我做了这些更改之后,您的代码生成了一个完美的边界框 当您声称代码不起作用时,究竟是什么问题?非常感谢您,先生。。。问题是它返回我的原始图像而不进行裁剪。我实际上是在使用我电脑上的钞票上的代码,不一定是URL图像。这当然是可能的——不同的图像可能会反转前景和背景。但是,您提供的图像在没有将“>”更改为“是”的情况下工作…甚至我的一些图像仍然将其反转,并且它无法在所有图像上工

我用这段代码裁剪了一个矩形图像,但它不起作用。怎么了?

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


当您声称代码不起作用时,究竟是什么问题?

非常感谢您,先生。。。问题是它返回我的原始图像而不进行裁剪。我实际上是在使用我电脑上的钞票上的代码,不一定是URL图像。这当然是可能的——不同的图像可能会反转前景和背景。但是,您提供的图像在没有将“>”更改为“是”的情况下工作…甚至我的一些图像仍然将其反转,并且它无法在所有图像上工作。再次比您更有效。
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 );