Need to write two categories to a file

1 次查看(过去 30 天)
Hello. I need to have this code write the entropy and edge of each image using kmeans to a file. Here is my code so far. Ignore or add some of the code i have commented out with %.
files = dir('*.jpg'); % specigy the extension of your image file
C=zeros(20,2);
for i = 1:numel(files)
filename = files(i).name;
image = imread(filename);
% apply processing to the loaded image
% save the image to an array or back to the folder using
imshow(image);
title('Original Image');
%Calculating Smoothness and filtering noise.
gs = im2gray(image);
gs = imadjust(gs);
BW = imbinarize(gs, "adaptive", "ForegroundPolarity", "dark");
imshowpair(gs, BW, "montage");
H = fspecial("average", 3);
gssmooth = imfilter(gs,H);
BWsmooth = imbinarize(gssmooth,"adaptive", "ForegroundPolarity", "dark");
gssmooth = imfilter(gs, H, "replicate");
BWsmooth = imbinarize(gssmooth,"adaptive","ForegroundPolarity","dark");
imshow(BWsmooth);
%Edge detection
%S = edge(BWsmooth);
%S1 = edge(BWsmooth, 'canny');
%imshowpair(S, S1, "montage");
%SedgeSum = sum(S(:))/(800*800)
%S1edgeSum = sum(S1(:))/(800*800)
%Texture Filters
%X = entropyfilt(image);
%Y = stdfilt(image, ones(11));
%Z = rangefilt(image, ones(11));
%E = entropy(gs)
%E1 = entropy(BWsmooth)
%SumEntropy = sum(E(:))
%Xim = rescale(X);
%Yim = rescale(Y);
%montage({Xim, Yim, Z}, 'Size', [1,3],'BackgroundColor', 'w', 'BorderSize', 20)
%title('Texture Images Showing Local Entropy, Local Standard Deviation, and Local Range');
test=rgb2gray(image);
test=imresize(370,560);
C(i,1)=sum(sum(edge(test,'canny')));
C(i,1)=entropy(test);
J=entropyfilt(test,ones(11));
len=length(J(:));
C(i,2:len+1)=J(:);
[idx, centers] = kmeans(C,3);
end
%test=rgb2gray(image);
%test=imresize(370,560);
%C(i,1)=sum(sum(edge(test,'canny')));
%C(i,1)=entropy(test);
%J=entropyfilt(test,ones(11));
%len=length(J(:));
%C(i,2:len+1)=J(:);
%
%[idx, centers] = kmeans(C,3);
filename = 'testdata.xlsx';
for j=1:length(idx)
name=files(i).name;
writematrix(name,filename,'Sheet',1,'Range',strcat('A',num2str(j)));
writematrix(name,filename,'Sheet',1,'Range',strcat('B',num2str(idx(j))));
end

采纳的回答

Pranjal Kaura
Pranjal Kaura 2021-10-25
编辑:Pranjal Kaura 2021-10-25
Hey,
It is my understanding that you want to compute:
  • the sum of the grey-scaled pixel intensities on the edges of the Input image - value 'X'
  • the entropy of the grey-scaled version of the Input image - value 'Y'
  • the clusters that the pair 'X' and 'Y' would belong to, using kmeans algorithm.
You then want to store the 'X', 'Y' values and the cluster number to a file.
There are a few issues in your code, which I have listed below:
  • Unecessary computations on Line 16 & 17. The values 'gssmooth' and 'HWsmooth' are re-computed on Line 19&20, making Line 16&17 computations null and void.
  • Incorrect usage of 'imresize' function on Line 44. Please refer the documentation to learn more about using the function.
  • Incorrect array indexing on Line 45 and 46. Value for 'X' computed on line 45 is stored in the first index in array 'C', however the value 'Y', computed on Line 46, is then also stored in the first index in array 'C', overwriting the value 'X'.
  • Incorrect indexing on Line 49. I'm not sure what you are trying to save or compute here, however this doesn't agree with the size of array 'C' that has been declared at the top of the code, before the for loop.
  • Computation of kmeans can be done outsiode the for loop, and need not be done for every iteration.
  • Incorrect usage of writeMatrix on Lines 64&65. Please refer the documentation to learn more about using the function
I have attached the updated code below:
files = dir('*.jpg'); % specigy the extension of your image file
C=zeros(20,2);
for i = 1:numel(files)
filename = files(i).name;
image = imread(filename);
% apply processing to the loaded image
% save the image to an array or back to the folder using
imshow(image);
title('Original Image');
%Calculating Smoothness and filtering noise.
gs = im2gray(image);
gs = imadjust(gs);
BW = imbinarize(gs, "adaptive", "ForegroundPolarity", "dark");
imshowpair(gs, BW, "montage");
H = fspecial("average", 3);
% gssmooth = imfilter(gs,H);
% BWsmooth = imbinarize(gssmooth,"adaptive", "ForegroundPolarity", "dark");
gssmooth = imfilter(gs, H, "replicate");
BWsmooth = imbinarize(gssmooth,"adaptive","ForegroundPolarity","dark");
imshow(BWsmooth);
%Edge detection
%S = edge(BWsmooth);
%S1 = edge(BWsmooth, 'canny');
%imshowpair(S, S1, "montage");
%SedgeSum = sum(S(:))/(800*800)
%S1edgeSum = sum(S1(:))/(800*800)
%Texture Filters
%X = entropyfilt(image);
%Y = stdfilt(image, ones(11));
%Z = rangefilt(image, ones(11));
%E = entropy(gs)
%E1 = entropy(BWsmooth)
%SumEntropy = sum(E(:))
%Xim = rescale(X);
%Yim = rescale(Y);
%montage({Xim, Yim, Z}, 'Size', [1,3],'BackgroundColor', 'w', 'BorderSize', 20)
%title('Texture Images Showing Local Entropy, Local Standard Deviation, and Local Range');
test_Img=rgb2gray(image);
test_Img=imresize(test_Img, [370,560]);
C(i,1)=sum(sum(edge(test_Img,'canny')));
C(i,2)=entropy(test_Img);
% J=entropyfilt(test_Img,ones(11));
% len=length(J(:));
% C(i,2:len+1)=J(:);
% [idx, centers] = kmeans(C,3);
end
%test=rgb2gray(image);
%test=imresize(370,560);
%C(i,1)=sum(sum(edge(test,'canny')));
%C(i,1)=entropy(test);
%J=entropyfilt(test,ones(11));
%len=length(J(:));
%C(i,2:len+1)=J(:);
%
[idx, centers] = kmeans(C,3);
filename = 'testdata.xlsx';
for j=1:length(idx)
% name=files(i).name;
writematrix([C idx],filename,'Sheet',1);
end
Hope this helps!
  3 个评论

请先登录,再进行评论。

更多回答(0 个)

标签

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by