Error when remapping BW image to RGB after applying filter

2 views (last 30 days)
Hi All,
I get the following message when trying to turn 3 colour images into RGB after applying a filter.
Warning: Error occurred while executing the listener callback for event POST_REGION defined for class matlab.internal.editor.RegionEvaluator:
Error using getByteStreamFromArray
Error during serialization
Error in matlab.internal.editor.WorkspaceUtilities.serializeArray
Error in matlab.internal.editor.figure.SerializedFigureState/serialize
Error in matlab.internal.editor.FigureProxy/createWebFigureSnapshot
Error in matlab.internal.editor.FigureManager
Error in matlab.internal.editor.FigureManager
Error in matlab.internal.editor.FigureManager.saveSnapshot
Error in matlab.internal.editor.FigureManager.snapshotAllFigures
Error in matlab.internal.editor.OutputsManager/postRegionCallback
Error in matlab.internal.editor.OutputsManager>@(varargin)obj.postRegionCallback(varargin{:})
Error in matlab.internal.editor.RegionEvaluator/postRegion
Error in matlab.internal.editor.RegionEvaluator>@(varargin)obj.postRegion(varargin{:})
Error in matlab.internal.editor.Evaluator/doPostEval
Error in matlab.internal.editor.Evaluator/evaluateCodeForFile
Error in matlab.internal.editor.RegionEvaluator/evaluateRegions
Error in matlab.internal.editor.evaluateRegions
Error in matlab.internal.editor.EvaluationOutputsService.evalRegions
This is my code:
%Image reading and filter section
I = imread('29_11_2019_132519Mosaix.jpg');
rmat = I(:,:,1);
gmat = I(:,:,2);
bmat = I(:,:,3);
binaryImageR = rmat < 50;
rmat(binaryImageR) = 0;
binaryImageG = gmat < 50;
gmat(binaryImageG) = 0;
binaryImageB = bmat < 50;
bmat(binaryImageB) = 0;
%RGB reconstruction section
imgSum1 = (binaryImageR & binaryImageG & binaryImageB);
imgSum2 = (rmat & gmat & bmat);
rgbImage = cat(3,binaryImageR,binaryImageG,binaryImageB); % Make the RGB image after filter
%Plots
figure;
subplot(2,3,1), imshow(rmat);
title('Red Plane');
subplot(2,3,2), imshow(gmat);
title('Green Plane');
subplot(2,3,3), imshow(bmat);
title('Blue Plane');
subplot(2,3,4), imshow(I);
title('Original');
subplot(2,3,5), imshow(rgbImage);
title('Summed Image');
uicontrol('Visible','off')
imgSum1 and imgSum2 are me playing with 2 different outputs.
Any thoughts would be most appreciated!

Accepted Answer

Image Analyst
Image Analyst on 3 Jan 2020
One problem was the lack of good comments. But the main problem was not casting your logical (binary) images to uint8 before creating the output RGB image. Here is the fixed code:
% Read in the input RGB image.
inputRGBImage = imread('peppers.png');
% Extract separate color channels.
rmat = inputRGBImage(:,:,1);
gmat = inputRGBImage(:,:,2);
bmat = inputRGBImage(:,:,3);
% Threshold each color channel to select pixels darker than 50 gray levels.
% Then set those pixels to 0 (black) in the original color channels.
binaryImageR = rmat < 50;
rmat(binaryImageR) = 0;
binaryImageG = gmat < 50;
gmat(binaryImageG) = 0;
binaryImageB = bmat < 50;
bmat(binaryImageB) = 0;
% RGB reconstruction section - create sum images but ignore them and don't use them.
% Find pixels that are darker than 50 in all 3 color channels, but ignore this binary image.
imgSum1 = (binaryImageR & binaryImageG & binaryImageB);
% Now do something weird, but never use this image.
imgSum2 = (rmat & gmat & bmat); % ?????
% Now create an RGB image showing the binary masks for each color channel.
rgbImage = cat(3, uint8(255 * binaryImageR), uint8(255 * binaryImageG), uint8(255 * binaryImageB)); % Make the RGB image after filter
% Display images.
figure;
subplot(2,3,1), imshow(rmat);
title('Red Plane');
subplot(2,3,2), imshow(gmat);
title('Green Plane');
subplot(2,3,3), imshow(bmat);
title('Blue Plane');
subplot(2,3,4), imshow(inputRGBImage);
title('Original RGB Image');
subplot(2,3,5), imshow(rgbImage);
title('Binary Images');
0000 Screenshot.png

More Answers (0)

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!