How can I solve the following error of the code segment?
Show older comments
% function main
clc;
clear all;
close all;
im = imread('3.jpg');
subplot(2,1,1),imshow(im);
subplot(2,1,2),imhist(im(:,:,1));
title('INPUT IMAGE HISTOGRAM');%figure,imhist(im(:,:,2)),title('blue');figure,imhist(im(:,:,3)),title('Green');
figure;
% j=);
I = imnoise(im,'salt & pepper',0.02);
subplot(1,2,1),imshow(I);
title('Noise adition and removal using median filter');
K = medfilt2(I);
subplot(1,2,2),imshow(K);
im = double(im);
s_img = size(im);
r = im(:,:,1);
g = im(:,:,1);
b = im(:,:,1);
% [c r] = meshgrid(1:size(i,1), 1:size(i,2));
data_vecs = [r(:) g(:) b(:)];
k= 4;
[ idx C ] = kmeansK( data_vecs, k );
% d = reshape(data_idxs, size(i,1), size(i,2));
% imagesc(d);
palette = round(C);
%Color Mapping
idx = uint8(idx);
outImg = zeros(s_img(1),s_img(2),3);
temp = reshape(idx, [s_img(1) s_img(2)]);
for i = 1 : 1 : s_img(1)
for j = 1 : 1 : s_img(2)
outImg(i,j,:) = palette(temp(i,j),:);
end
end
cluster1 = zeros(size(r));
cluster2 = zeros(size(r));
cluster3 = zeros(size(r));
cluster4 = zeros(size(r));
figure;
cluster1(find(outImg(:,:,1)==palette(1,1))) = 1;
subplot(2,2,1), imshow(cluster1);
cluster2(find(outImg(:,:,1)==palette(2,1))) = 1;
subplot(2,2,2), imshow(cluster2);
cluster3(find(outImg(:,:,1)==palette(3,1))) = 1;
subplot(2,2,3), imshow(cluster3);
cluster4(find(outImg(:,:,1)==palette(4,1))) = 1;
subplot(2,2,4), imshow(cluster4);
cc = imerode(cluster4,[1 1]);
figure,imshow(imerode(cluster4,[1 1]));
title('eroded image');
[label_im, label_count] = bwlabel(cc,8);
stats = regionprops(label_im, 'Centroid');
for i=1:label_count
area(i) = stats(i).Centroid;
end
[maxval, maxid] = max(area);
label_im(label_im ~= maxid) = 0;
label_im(label_im == maxid) = 1;
figure,imshow(label_im);
title('tumour');
% outImg = uint8(outImg);
% imtool(outImg);
code_end = 1;
ERROR:
Subscripted assignment dimension mismatch.
Error in main (line 70)
area(i) = stats(i).Centroid;
6 Comments
per isakson
on 18 Jan 2019
Edited: per isakson
on 18 Jan 2019
I fail to reproduce your problem, I get
Undefined function or variable 'kmeansK'.
Error in cssm (line 25)
[ idx, C ] = kmeansK( data_vecs, k );
>>
What's kmeansK
Geoff Hayes
on 18 Jan 2019
Syed - stats(i).Centroid could be of different dimension on each iteration of your loop, so consider making area a cell array. Also, consider renaming this variable to avoid conflicts with the MATLAB built-in function of the same name (see area).
Syed Zenith Rhyhan
on 19 Jan 2019
Edited: Syed Zenith Rhyhan
on 19 Jan 2019
Walter Roberson
on 19 Jan 2019
We do not have 1.bmp or 3.bmp
Syed Zenith Rhyhan
on 19 Jan 2019
Edited: Syed Zenith Rhyhan
on 19 Jan 2019
Walter Roberson
on 19 Jan 2019
Centroid is a vector of at least 2 elements (it will have the same number of entries as ndims applied to the image.) You cannot store a vector into a scalar location area(i)
Answers (1)
Walter Roberson
on 19 Jan 2019
0 votes
The line of code with the error does not occur in the code you attached. Your current code calls upon Area, which does not exist in the field because your current code asks regionprops for Centroid.
Centroid is a vector of at least 2 elements (it will have the same number of entries as ndims applied to the image.) You cannot store a vector into a scalar location area(i)
3 Comments
Syed Zenith Rhyhan
on 19 Jan 2019
Walter Roberson
on 19 Jan 2019
area(i) = stats(i).Centroid(1) + 1i * stats(i).Centroid(2);
You are now able to store the two pieces of information into a single location, one coded into the real() of the location and the other coded into the imag() of the location.
Syed Zenith Rhyhan
on 19 Jan 2019
Categories
Find more on ROI-Based Processing 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!