Remove black pixels from RGB image.

조회 수: 49 (최근 30일)
Luke G.
Luke G. 2021년 1월 15일
댓글: DGM 2023년 1월 31일
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
Mario Malic
Mario Malic 2021년 1월 15일
Hi Luke,
I'll chip in my suggestions in here, while someone else with more knowledge comes by.
The border that you want to remove consists of different colors, not only of RGB [0, 0, 0], but RGB [0, 1, 0], and many other different colors. So, I guess the solution is to find all the colors that border consists of. Unfortunatelly, I can't help with this.
Luke G.
Luke G. 2021년 1월 15일
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!

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

채택된 답변

Image Analyst
Image Analyst 2021년 1월 16일
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
Image Analyst
Image Analyst 2021년 1월 17일
See my attached demo. Make the necessary adaptations.
But the best solution is to just get a thermal camera that outputs the temperature image. I think it's only the very cheapest FLIR model that gives you only a pseudocolored image. Pay a few bucks more and get one that has the image directly in degrees Celsius.
Luke G.
Luke G. 2021년 1월 17일
Thanks Image Analyst! Appreciate the info & support.

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

추가 답변 (1개)

Jeffrey Daniels
Jeffrey Daniels 2023년 1월 31일
img_2d = reshape(img,[numrows*numcols],3);
% Find the indices of the rows that are remove_color
remove_color = [0,0,0];
color_idx = ~ismember(img_2d,remove_color, 'rows');
% Replace remove_color with white
img_2d(color_idx) = [255,255,255];
img = reshape(img_2d,[numrows,numcols,3]);
% Remove the rows that are remove_color (Optional, if you really want to "remove" them", but as stated it will no longer be rectanglar. This only works if you want statistics on your colors.)
img_2d_nw = img_2d(~color_idx, :);
  댓글 수: 1
DGM
DGM 2023년 1월 31일
Improvements to make a better answer:
  • Format the code
  • Make the example complete enough that it can be demonstrated in the editor
  • Make sure the example does what you say it does
% a uint8 RGB image with black border
img = imread('peppers.png');
img = padarray(img,[10 10],0,'both');
imshow(img)
% get the size
[numrows,numcols,nchan,~] = size(img);
% reshape the image into a 3-column matrix
img_2d = reshape(img,numrows*numcols,3);
% Find the indices of the rows that are remove_color
remove_color = [0,0,0];
color_idx = ismember(img_2d,remove_color,'rows'); % select the correct region
% Either replace remove_color with a new color ...
new_color = [255 255 0]; % white would be invisible against the webpage background
img_2d(color_idx,:) = repmat(new_color,[nnz(color_idx) 1]); % make sure addressing works
img = reshape(img_2d,[numrows,numcols,3]);
imshow(img)
% ... or remove the rows that are remove_color
% (Optional, if you really want to "remove" them", but as stated it will no longer be rectanglar.
% This only works if you want statistics on your colors.)
img_2d_nw = img_2d(~color_idx, :);

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

카테고리

Help CenterFile Exchange에서 Get Started with Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by