MATLAB: How to convert segmented image composed of edges back to RGB image

edgeMATLABmatlab edge segmentation rgbsegmentation

So I have an original RGB image. I got the edges using sobel edge detection. I then segmented the image such that one part of the image is color black (which i don't need anymore) while the other part is the remaining image composed of edges. How can I convert this image back to the RGB image? Also, I need the black part to be removed from the RGB image. please please please help me!!!!!

Best Answer

Your edge image sounds like it is a logical mask. If so then
maskedImage = repmap(EdgeMask, 1, 1, 3) .* TheRGBImage;
This will set locations to 0, which will show up as black. If your image class is double() then you could set the locations to NaN instead:
maskedImage = TheRGBImage;
nanidx = find(~EdgeMask);
pane_size = size(EdgeMask);
maskeImage(nanidx) = nan; %zap red
maskedImage(nanidx + pane_size) = nan; %zap green
maksedImage(nanidx + 2*pane_size) = nan; %zap blue