Remove black pixels from RGB image.

48 views (last 30 days)
Luke G.
Luke G. on 15 Jan 2021
Commented: Luke G. on 17 Jan 2021
I know that variations of this question get asked a lot on here, but I haven't found a solution that works for my problem very well.
How do I remove the black border from the image above programmatically, but not by using imcrop!? I would prefer to find & remove all of the black pixels in the image, by effectively deleting that data--leaving me with a (yes) cropped image that contains only the color region of this image. My attempt is as follows:
% define scale bar:
firstFrame = read(vidObj,1);
imshow(firstFrame,'InitialMagnification',300);
title('Define the scale bar region:','FontSize',16);
scaleBar = drawrectangle('Color',[1 1 0]);
roi_Bar = scaleBar.Position;
% cropped and screwed:
I = imcrop(firstFrame, roi_Bar);
figure;
imshow(I);
% remove black border:
mask = (I(:, :, 1) == 0) & (I(:, :, 2) == 0) & (I(:, :, 3) == 0);
I(mask) = [];
figure;
imshow(I); % this returns a horrific result
Many thanks in advance!
  2 Comments
Luke G.
Luke G. on 15 Jan 2021
Thanks - this was a good information! Since all of the values might not be RGB [0 0 0], I could delete all the pixels around the border of the image (shown below), but if the user didn't select exactly the border of the scale bar correctly, this might crop part of the scale bar & lead to erroneous temperature readings.
% cropped and screwed:
I = imcrop(firstFrame, roi_Bar);
figure;
imshow(I);
figure;
bW = 3; % border width
iwant = I(bW:end-bW,bW:end-bW,:);
imshow(iwant);
It would be great if there was a robust edge detection method to use here. Thanks again for your suggestion!

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 16 Jan 2021
All black pixels cannot be removed since the image must remain rectangular. You can remove whole rows or whole columns, but not randomly scattered isolated groups of black pixels. With that colorbar image you have, you could delete everything up to and including the black outline of the colormap. Is that what you want to do?
  7 Comments
Luke G.
Luke G. on 17 Jan 2021
Thanks Image Analyst! Appreciate the info & support.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!