MATLAB自动裁剪 [英] MATLAB Auto Crop

查看:642
本文介绍了MATLAB自动裁剪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自动裁剪下面的图像到边界框。背景将始终是相同的颜色。我已经尝试了
的答案的回答,我提出了一种绕过regionprops(图像处理工具箱)的方法基于在黑白图像上找到

 %load 
img = im2double(imread('http://i.stack.imgur.com/ZuiEt.jpg'));
%黑白图像按阈值检查每个像素与白色的距离
bw = sum((1-img)。^ 2,3)> .5;
%显示bw image
figure; imshow(BW);标题('bw image');

%获得边界框(第一行,第一列,数字行,数字列)
[row,col] = find(bw);
bounding_box = [min(row),min(col),max(row) - min(row)+ 1,max(col) - min(col)+ 1];

%显示矩形
rect = bounding_box([2,1,4,3]); %rectangle想要x,y,w,h我们有行,列,...需要转换
figure; imshow(IMG);等一下;矩形('Position',rect);


I am trying to automatically crop the image below to a bounding box. The background will always be the same colour. I have tried the answers at Find the edges of image and crop it in MATLAB and various applications and examples on Mathworks' file exchange but I get stuck at getting a proper boundingbox.

I was thinking to convert the image to black and white, converting it to binary and removing everything that's closer to white than black, but I'm not sure how to go about it.

解决方案

Following the answer of Shai, I present a way to circumvent regionprops (image processing toolbox) just based on find on the black-white image.

% load
img = im2double(imread('http://i.stack.imgur.com/ZuiEt.jpg'));
% black-white image by threshold on check how far each pixel from "white"
bw = sum((1-img).^2, 3) > .5; 
% show bw image
figure; imshow(bw); title('bw image');

% get bounding box (first row, first column, number rows, number columns)
[row, col] = find(bw);
bounding_box = [min(row), min(col), max(row) - min(row) + 1, max(col) - min(col) + 1];

% display with rectangle
rect = bounding_box([2,1,4,3]); % rectangle wants x,y,w,h we have rows, columns, ... need to convert
figure; imshow(img); hold on; rectangle('Position', rect);

这篇关于MATLAB自动裁剪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆