I appreciate any advice or method to solve this problem. Preferably if you can guide me with the code to execute. Greetings and thanks
2 次查看(过去 30 天)
显示 更早的评论
Greetings community. I am working with image processing. In some photos I added some details in the ''paint'' application and in others I did not modify. The issue is that the modified images go from having a color space: Gray to a color space: RGB. In addition to the alpha channel being activated. My query is, how can I change the images with RGB color space to Gray? I try with im2gray and rgb2gray and I get this error: Invalid argument at position 1. For RGB inputs, I must be a m x n x 3 array. RGB must be a m x n x 3 array.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1174163/image.png)
I appreciate any advice or method to solve this problem. Preferably if you can guide me with the code to execute. Greetings and thanks
0 个评论
回答(1 个)
DGM
2022-10-30
编辑:DGM
2022-10-31
Regarding the RGB/gray issues. The older rgb2gray() tool is oblivious and will simply fail if fed an image that's already single-channel. The newer im2gray() should just pass inputs which are already single-channel.
inpict = imread('cameraman.tif'); % 256x256x1 (gray)
B = im2gray(inpict); % passes gray inputs
C = rgb2gray(inpict); % breaks because input is gray
That said, this doesn't look like the error you're getting. I imagine what's going on is that you're getting MxNx4 (RGBA) images back from imread(). Nothing in base MATLAB/IPT knows what to do with an RGBA image. If the alpha content is solid or inconsequential, you can just strip it off.
inpict = imread('bananaa.tif'); % this is an RGBA image with alpha content
size(inpict)
inpict = inpict(:,:,1:3); % keep only the RGB channels, discard alpha
size(inpict)
Obviously, blindly doing this to all files will cause addressing errors if some of the files are only single-channel (I) or two-channel (IA). You'll have to check size(inpict,3) and then conditionally reduce the image back to a single-channel image.
inpict = imread('somerandompicture.tif'); % this might be anything
% if we can't presume how many channels there are
nchans = size(inpict,3);
if ~any(nchans == [1 2 3 4])
error('expected inpict to be an I/IA/RGB/RGBA image. what is this?')
end
% image can now only be I/IA/RGB/RGBA
if any(nchans == [2 4])
% strip alpha (the last channel)
inpict = inpict(:,:,1:end-1);
end
% image can now only be I/RGB
% either case should be fine for im2gray()
inpict = im2gray(inpict); % reduce to BT601 luma
% image can now only be I
If the alpha content is important, then how to handle it depends entirely on what you're doing with the image.
Depending on the behavior of the application that was used to modify the files and what exactly you did when editing them, the alpha content may be more important than you expect. This may be the case if you tried to use an "erase" tool on parts of the image. Long story short, the advice above is only a best guess. If you find that edits you made are being undone or being rendered unexpectedly after importing into MATLAB, it would be best if you could supply an example image and a description of the observed discrepancy.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!