I appreciate any advice or method to solve this problem. Preferably if you can guide me with the code to execute. Greetings and thanks

3 ビュー (過去 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.
I appreciate any advice or method to solve this problem. Preferably if you can guide me with the code to execute. Greetings and thanks

回答 (1 件)

DGM
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)
ans = 1×3
720 911 4
inpict = inpict(:,:,1:3); % keep only the RGB channels, discard alpha
size(inpict)
ans = 1×3
720 911 3
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.

Community Treasure Hunt

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

Start Hunting!

Translated by