Index exceeds matrix dimensions. Error i (line 39) I(:,:,2) = input_img(:,:,2);

1 view (last 30 days)
I got the above error while using the attached untitled2.m file
How can i correct the error
  1 Comment
KSSV
KSSV on 31 Mar 2017
You are converting RGB image into gray...and then trying to extract it as RGB...I have removed rgb2gray conversion...and considering RGB image .

Sign in to comment.

Accepted Answer

KSSV
KSSV on 31 Mar 2017
clc; clear all ;
clear all;
close all;
clc;
% -- Getting input file -- %
[filename pathname] = uigetfile({'*.jpg';'*.bmp'},'Select MRI');
inputimage=strcat(pathname, filename);
input=imread(inputimage);
% input=rgb2gray(imread(inputimage));
[m,n,p] = size(input) ;
if p==1
disp('Select RGB image')
break
end
% axes(handles.axes1)
figure,
imshow(input,[]);
axis off;
title('Input Image','fontsize',12,'fontname','Times New Roman','color','Black');
[row,col,cha] = size(input);
input_img = input;
if cha ==3
input = rgb2gray(input);
end
preim = medfilt2(input);
% axes(handles.axes2)
figure,
imshow(preim,[]);
axis off;
title('Preprocessed Image','fontsize',12,'fontname','Times New Roman','color','Black');
% ----------------------------------------------------------------
I(:,:,1) = input_img(:,:,1);
I(:,:,2) = input_img(:,:,2);
I(:,:,3) = input_img(:,:,3);
% Hematoxylin and eosin
text(size(I,2),size(I,1)+15,...
'Clustering process is done', ...
'FontSize',7,'HorizontalAlignment','right');
% cform = makecform('srgb2lab');
% lab_I = applycform(I,cform);
ab = double(I(:,:,2:3));
nrows = size(ab,1);
ncols = size(ab,2);
ab = reshape(ab,nrows*ncols,2);
nColors = 3;
[cluster_idx, cluster_center] = kmeans(ab,nColors,'distance','sqEuclidean', ...
'Replicates',2);
pixel_labels = reshape(cluster_idx,nrows,ncols);
figure,
imshow(pixel_labels,[]), title('Kmeans clustering');
segmented_images = cell(1,3);
rgb_label = repmat(pixel_labels,[1 1 3]);
for k = 1:nColors
color = I;
color(rgb_label ~= k) = 0;
segmented_images{k} = color;
end
Fig1 = segmented_images{1};
Fig2 = segmented_images{2};
Fig3 = segmented_images{3};
figure,
subplot(2,2,1),
imshow(Fig1);
subplot(2,2,2),
imshow(Fig2);
subplot(2,2,3),
imshow(Fig3);

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!