Crop coloured padding of image

조회 수: 1 (최근 30일)
YT
YT 2018년 10월 14일
댓글: YT 2018년 10월 14일
So this is pretty much a follow-up question to this one I asked yesterday, but now I'm interested in cropping coloured padding (attachment for test-image-2.jpg). With the answer from Walter on the previous question, I tried to do it like this:
I = imread('test-image-2.jpg');
rI = I(:,:,1);
gI = I(:,:,2);
bI = I(:,:,3);
Rw = all(rI == 237); %red channel value
Gw = all(gI == 27); %green channel value
Bw = all(bI == 36); %blue channel value
%mask
Rmaskc = all(Rw, 1);Rmaskr = all(Rw, 2);
Gmaskc = all(Gw, 1);Gmaskr = all(Gw, 2);
Bmaskc = all(Bw, 1);Bmaskr = all(Bw, 2);
%set to 0
rJ = rI; rJ(Rmaskr,:,:) = 0; rJ(:,Rmaskc,:) = 0;
gJ = gI; gJ(Gmaskr,:,:) = 0; gJ(:,Gmaskc,:) = 0;
bJ = bI; bJ(Bmaskr,:,:) = 0; bJ(:,Bmaskc,:) = 0;
%RED
rJ( ~any(rJ,2), : ) = []; %rows
rJ( :, ~any(rJ,1) ) = []; %columns
%GREEN
gJ( ~any(gJ,2), : ) = []; %rows
gJ( :, ~any(gJ,1) ) = []; %columns
%BLUE
bJ( ~any(bJ,2), : ) = []; %rows
bJ( :, ~any(bJ,1) ) = []; %columns
IM = cat(3, rJ, gJ, bJ);
It crops the sides just fine (the small strokes on sides have different RGB values so guess thats ok), but the top/bottom is not getting cropped.
So I'm a bit confused on why the sides get cropped decently, but for some reason the top/bottom gets ignored

채택된 답변

Image Analyst
Image Analyst 2018년 10월 14일
Try this:
rgbImage = imread('test-image-2.jpg');
rI = rgbImage(:,:,1);
gI = rgbImage(:,:,2);
bI = rgbImage(:,:,3);
subplot(2, 2, 1);
imshow(rgbImage);
title('Mask', 'FontSize', 16);
mask = ~((rI == 237) & (gI == 27) & (bI == 36));
subplot(2, 2, 2);
imshow(mask);
title('Mask', 'FontSize', 16);
[maskRows, maskColumns] = find(mask);
row1 = min(maskRows);
row2 = max(maskRows);
col1 = min(maskColumns);
col2 = max(maskColumns);
maskedRGBImage = rgbImage(row1:row2, col1:col2, :);
subplot(2, 2, 3);
imshow(maskedRGBImage);
title('Masked Image', 'FontSize', 16);
  댓글 수: 1
YT
YT 2018년 10월 14일
Thank you, exactly what I was looking for.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by