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 Center および File ExchangeImages についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by