How to automatically crop empty space (with known color) around an image object (like "-trim" in ImageMagick)?

조회 수: 1 (최근 30일)
After a distortion of an image the transformed image can have boarders with a color that was specified with 'FillValues'. I want to automatically crop the image object in the middle i.e. to get rid of the possible asymmetric boarders. Is there a function in Matlab or in the Image Processing Tools?

답변 (1개)

Jordan Ross
Jordan Ross 2017년 1월 24일
Hello,
One possible solution would be to get the region of interest by color using the "roicolor" function. http://www.mathworks.com/help/images/ref/roicolor.html
Then using that function you can determine the indices of the image without that color. However, please note that this assumes that this color only appears in the border and not inside the image. For example, consider the following script where I remove the outer white box from an image:
I = imread('sampleImage.png');
I = rgb2gray(I);
colorOfInterest = 255;
roi = ~roicolor(I, colorOfInterest, colorOfInterest);
idx = find(roi==1);
[y, x] = ind2sub(size(I), idx);
x1 = round(min(x));
y1 = round(min(y));
x2 = round(max(x));
y2 = round(max(y));
croppedImage = I(y1:y2, x1:x2);
figure;
subplot(1,2,1);
imshow(I);
subplot(1,2,2);
imshow(croppedImage);

카테고리

Help CenterFile Exchange에서 Read, Write, and Modify Image에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by