MATLAB : How to crop an object from a binary image by identifying some features?

天涯浪子 提交于 2020-02-05 08:38:09

问题


I have an image like shown here:

My ultimate aim is to extract the vein pattern in the finger. So What I am trying to do is the extraction of the finger object alone. For that purpose, I tried an Otsu thresholding step first, then an erosion and dilation. Now using the binary image as a mask I multiplied element wise with original image to obtain the finger alone (not that accurate though).

The code is as below:

I = imread('3.5.bmp');
[level] = graythresh(I);
BW = im2bw(I,level);
[BWm] = imerode(BW,strel('disk',10)); figure, imshow(BWm)
[BWmm] = imdilate(BWm,strel('disk',15)); figure, imshow(BWmm)
B = I.*uint8(BWmm); % BWmm is the mask
imshow(B)

Now I want to crop this finger part alone using the mask which I created before. How to achieve that?

For clarity I am uploading the image with the area to be cropped:

(And finally I don't want to do this manually or using imcrop() utility with pixel coordinates as input. I would like to get those pixel coordinate using some algorithms.)

回答1:


You can find the pixel coordinates from the mask BWmm and then use the coordinates with imcrop.

To find the extermal points of BWmm use

projX = any( BWmm, 1 ); % projection of mask along x direction
projY = any( BWmm, 2 ); % projection of mask along y direction
fx = find( projX, 1, 'first' ); % first column with non-zero val in mask
tx = find( projX, 1, 'last' );  % last column with non-zero val in mask
fy = find( projY, 1, 'first' ); % first row with non-zero val in mask
ty = find( projY, 1, 'last' );  % last row with non-zero val in mask
cropRect = [fx, fy, tx-fx+1, ty-fy+1];
cImg = imcrop( I, cropRect );


来源:https://stackoverflow.com/questions/23263101/matlab-how-to-crop-an-object-from-a-binary-image-by-identifying-some-features

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!