필터 지우기
필터 지우기

Unable to perform assignment because the size of the left side is 128-by-128 and the size of the right side is 128-by-128-by-3. Error in ind2rgb (line 34) rout(:,:,1) = r;

조회 수: 2 (최근 30일)
Hellow all, Hope you are fine. I am writing code to convert grayscale image dataset folder to RGB image and then store it into another folder.
but i face an error:
Unable to perform assignment because the size of the left side is 128-by-128 and the size of
the right side is 128-by-128-by-3.
Error in ind2rgb (line 34)
rout(:,:,1) = r;
Error in Untitled (line 36)
RGB = ind2rgb(Img, colormap);
My Question is
What is the solution of that problem my code is below.
and the second thing also please check the coversion code of grayscale image to rgb as i think it is not Okay. also help me in that.
I am very thankfull to you.
myFolder='C:\Users\STAR\OneDrive\Desktop\New folder (2)\Wintrim.BX';
OutputFolder='C:\Users\STAR\OneDrive\Desktop\New folder (2)\OutputFolder'
filePattern = fullfile(myFolder, '*.png'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
Img1 = imread(fullFileName);
Img = imresize(Img1,[128 128]);
RGB = ind2rgb(Img, colormap);
imwrite(RGB, fullfile(OutputFolder, baseFileName));
drawnow; % Force display to update immediately.
end

채택된 답변

Image Analyst
Image Analyst 2021년 7월 8일
편집: Image Analyst 2021년 7월 8일
Try this:
myFolder='C:\Users\STAR\OneDrive\Desktop\New folder (2)\Wintrim.BX';
OutputFolder='C:\Users\STAR\OneDrive\Desktop\New folder (2)\OutputFolder'
if ~isfolder(OutputFolder)
mkdir(OutputFolder);
end
filePattern = fullfile(myFolder, '*.png'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
colormap = jet(256); % Whatever you want.
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
RGB = imread(fullFileName);
imshow(RGB);
drawnow; % Force display to update immediately.
[rows, columns, numberOfColorChannels] = size(RGB)
%-------------------------------------------------------------------------------------------------------------------------------------
% Convert to RGB, only if necessary (if it's gray scale).
if numberOfColorChannels == 1
% It's gray scale. Need to convert to color.
RGB = ind2rgb(RGB, colormap);
end
% Resize the image.
RGB = imresize(RGB, [128, 128]);
% Save the image in the output folder.
outputFileName = fullfile(OutputFolder, baseFileName);
fprintf('Saving %s\n', outputFileName);
imwrite(RGB, outputFileName);
end
A general purpose demo is attached.

추가 답변 (1개)

DGM
DGM 2021년 7월 7일
편집: DGM 2021년 7월 7일
Okay, after looking a few things up, it seems you're running some version prior to ~R2018x. Given that, the input dimensionality handling and errors are a bit different than in later versions. I would suggest that your images (at least some of them) are not actually single-channel images (i.e. mxnx1). Make sure your input images are actually single-channel images. If they are 3-ch gray images or actually RGB images, and you just want to treat them the same, just convert them with rgb2gray() to get a single-channel image out.
A few other things to note:
When specifying the colormap, be explicit. When calling colormap() without any arguments, it returns the map in use by gca. There's no way for anyone looking at this code to know what map was being used by whatever unrelated figure window you had open at the time. Just specify the desired map like so:
ind2rgb(myimg, parula(numberoflevels))
Look up the help synopsis for parula() to see a list of the other built-in maps.
There shouldn't be any need to run drawnow(), since nothing is being drawn.
Lastly, I'm not sure why you're trying to apply an arbitrary map to grayscale images unless it's for aesthetic purposes. If the method of making it RGB is unimportant, there are also other ways to "colorize" a grayscale image:

카테고리

Help CenterFile Exchange에서 Blue에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by