matlab auto,MATLAB Auto Crop

问题

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.

be2c5fd6483961fe3a8a59af8fb2f5df.png

回答1:

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);

回答2:

Here's a nice way

img = 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 );

98832bb12058d82a24952c5030e93941.png

Following Jak's request, here's the second line explained

after converting img to double type (using im2double), the image is stored in memory as h-by-w-by-3 matrix of type double. Each pixel has 3 values between 0 and 1 (not 255!), representing its RGB values 0 being dark and 1 being bright.

Thus (1-img).^2 checks, for each pixel and each channel (RGB) how far it is from 1 - bright. The darker the pixel - the larger this distance.

Next, we sum the distance per channel to a single value per pixel using sum( . ,3 ) command leaving us with h-by-w 2D matrix of distances of each pixels from white.

Finally, assuming background is bright white we select all pixels that are significantly far from birght b > .5. This threshold is not perfect, but it captures well the boundary of the object.

回答3:

to crop an image

first create boundry box where you want to crop.

crp = imcrop(original_image_name,boundry_box);

I have done this in my assignment. This really works!!!!!!

来源:https://stackoverflow.com/questions/24135462/matlab-auto-crop

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值