how to remove black margines automatically from images?

how can i automatically remove black margines around border of an image,like the dark lines remains around an image when you scan it,there is also this link which describes about this,but can any one gives a better code,thanx http://uk.mathworks.com/matlabcentral/answers/128133-automatically-trim-remove-crop-black-borders-margins-from-images-volumes i also used imclearborder,but it wont work on my image,

4 Comments

You didn't post your image so there's nothing I can recommend.
my image is something like this,i want to remove the black margine on the right part,and use the remain part,some black margines around borders,
and when i want to segment my image,it gives wrong results,i want to remove the black part and use the grey remain to segment
Just use imcrop() or regular indexing to extract a subimage.
sara
sara on 30 Jan 2015
Edited: sara on 30 Jan 2015
there are more than one image,and the problem is that in one image the margin is on the right side,in another it is in left side,and even their size is different,in one image it is a thick line,another is a thin line, i will work on regular indexing to extract sub image,thank you

Sign in to comment.

 Accepted Answer

I'm not sure why that is a problem. Just find the first and last column and crop like I suggested:
horizontalProfile = mean(grayImage, 1) > 10; % Or whatever.
firstColumn = find(horizontalProfile, 1, 'first');
lastColumn = = find(horizontalProfile, 1, 'last');
subImage = grayImage(:, firstColumn, lastColumn);
Is there a problem with that? I think it should handle the cases you mentioned. Does it not?

5 Comments

Nope. It looks like the original image was first displayed by some medical program that added patient information, and then you, or someone, took that screenshot and put two of them side by side with x and y axis labels and tick marks. And then made another screenshot of that dual-image screenshot.
The interior images, which I guess are what you want, most likely occur at the same rows and columns so you just have to figure out what they are and then extract the image within that region
croppedImage = fullImage(row1:row2, col1:col2, :);
Again, row1, row2, col1, and col2 will most likely be the same for all your images. Examine your image to find them. Zoom in or use impixelinfo() after you display them.
I don't help with c, only MATLAB. If you want to convert MATLAB code to c, you should use the MATLAB Coder Toolbox, which I don't have, or ask in a C forum for code to do what you want to do.
i mean (c) as a number alignment not c language , i want the cropping using matlab but automatically using the first and last nonzero line,as well as of the first and last nonzero column from the binary image @Image Analyst
Oh, sorry - I didn't read closely enough, or I misinterpreted.
To get the bounding box of everything, here is one way
[r, c] = find(binaryImage);
row1 = min(r);
row2 = max(r);
col1 = min(c);
col2 = max(c);
croppedImage = grayImage(row1:row2, col1:col2);

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!