How can I fix this code to work?

1 次查看(过去 30 天)
I am using the following code to extract mean feature and texture features using GLCM for every lesion in the input image and append them to an existing matrix then save it into a csv file. The code works when there is only one record in the matrix created but if there are more one record in the matrix created then it gives the following error.
'The size of the right hand side did not match the size of the indexing on the left hand side'.
clc;
clear all;
I = imread('Capture.JPG');
gray_image = rgb2gray(I);
binary_image = gray_image > 40;
imshow(binary_image);
roundBlobs = regionprops(binary_image, 'BoundingBox');
for k = 1 : length(roundBlobs)
thisBB = roundBlobs(k).BoundingBox;
crop_blob=imcrop(I,[thisBB(1),thisBB(2),thisBB(3),thisBB(4)]);
resize_blob=imresize(crop_blob,[thisBB(3) thisBB(4)]);
figure,imshow(resize_blob);
% Calculate mean of color spaces
redChannel = resize_blob(:, :, 1);
greenChannel = resize_blob(:, :, 2);
blueChannel = resize_blob(:, :, 3);
% Get mean of R, G, and B
meanR = mean2(redChannel);
meanG = mean2(greenChannel);
meanB = mean2(blueChannel);
HSVimage = rgb2hsv(resize_blob);
H_component = HSVimage(:,:,1);
S_component = HSVimage(:,:,2);
I_component = HSVimage(:,:,3);
% Get mean of Hue, Saturation, and Value
meanH = mean(H_component(:));
meanS = mean(S_component(:));
meanV = mean(I_component(:));
YCbCrimage = rgb2ycbcr(resize_blob);
Y_component = YCbCrimage(:,:,1);
Cb_component = YCbCrimage(:,:,2);
Cr_component = YCbCrimage(:,:,3);
% Get mean of Y, Cb, and Cr
meanY = mean2(Y_component);
meanCb = mean2(Cb_component);
meanCr = mean2(Cr_component);
Lab = rgb2lab(resize_blob);
L = Lab(:,:,1);
a = Lab(:,:,2);
b = Lab(:,:,3);
% Get mean of L, a, and b
meanL = mean2(L);
meanLA = mean2(a);
meanLB = mean2(b);
% Calculate GLCM and extract texture features
gray_blob = rgb2gray(resize_blob);
offsets = [0 1; -1 1;-1 0;-1 -1];
glcm = graycomatrix(gray_blob, 'GrayLimits', [], 'Offset',offsets, 'Symmetric', true);
stats = graycoprops(glcm);
avgCont = mean(stats.Contrast);
avgCorrel = mean(stats.Correlation);
avgEnergy = mean(stats.Energy);
avgHomogen = mean(stats.Homogeneity);
Feats = [avgCont, avgCorrel, avgEnergy, avgHomogen, meanR, meanG, meanB, meanH, meanS, meanV, meanY, meanCb, meanCr, meanL, meanLA, meanLB];
glcm_data(k, :) = Feats(:);
end
m = matfile('gl_data.mat', 'Writable', true);
if isprop(m, 'gl_data')
s = size(m, 'gl_data');
m.gl_data(s(1)+1, :) = glcm_data;
else
m.gl_data = glcm_data;
end
load('gl_data.mat');
writematrix(gl_data, 'gl_data.csv');
I want to be able to append the extracted features for one lesion or multiple lesions to the existing matrix. For example if the input image has one lesion then the extracted data is only one record or if the input image has multiple lesions then the extracted data consists of multiple records so in both cases, I have to be able to append them to an existing matrix.
Any help is greatly appreciated!

回答(1 个)

Asvin Kumar
Asvin Kumar 2020-4-7
Try replacing:
m.gl_data(s(1)+1, :) = glcm_data;
with
m.gl_data(s(1)+(1:k), :) = glcm_data;
The code was trying to assign the entire matrix glcm_data to the one row specified at index s(1)+1. This should work now.

类别

Help CenterFile Exchange 中查找有关 Image Processing Toolbox 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by