This is already the segmentation stage, and I want to find the fractal dimension but it has an error. Can someone help me?
image_folder = 'F:\kuliah\semester6\TA2\mencoba\output';
outfolder = 'F:\kuliah\semester6\TA2\mencoba\hasil';
if ~isdir(outfolder); mkdir(outfolder); end
files = dir(fullfile(image_folder, '*.jpg'));
%I presume it has the variable map in it
for iFiles = 1:numel(files)
thisfilename = fullfile(files(iFiles).folder,files(iFiles).name);
[~, basename, ext] = fileparts(image_folder);
citra = imread(thisfilename);
%change to grayscale
grayimg = rgb2gray(citra);
figure, fig2=imshow(grayimg);
%change to green channel
green_channel = citra(:,:,2);
figure, fig3=imshow(green_channel);
%change to CLAHE (Contrast Limited Adaptive Histogram Equalization)
CLAHE = adapthisteq(green_channel);
figure, fig4=imshow(CLAHE);
ER = edge(CLAHE, 'canny');
EG = edge(CLAHE, 'canny');
EB = edge(CLAHE, 'canny');
anyedge = ER | EG | EB;
figure, fig5=imshow(anyedge)
size(fig5)
fig = figure;
outfile = fullfile(outfolder, sprintf('%s-coba-%03d.jpg', basename, iFiles));
saveas(fig5, outfile);
% yang dihitung dimensinya yaitu citra hasil segmentasi
[n, r] = fboxcount(figure,'slope');
%Perhitungan dimensi
df = -diff(log(n))./diff(log(r));
%Menampilkan hasil dimensi
disp(['Df= ' num2str(mean(df(4:8)))]);
% % end
end

 Accepted Answer

Walter Roberson
Walter Roberson on 13 Mar 2021
you are passing in something that isn't an rgb image.

18 Comments

image_folder = 'F:\kuliah\semester6\TA2\mencoba';
outfolder = 'F:\kuliah\semester6\TA2\mencoba\output';
if ~isdir(outfolder); mkdir(outfolder); end
load mri %I presume it has the variable map in it
files = dir(fullfile(image_folder, '*.jpg'));
for iFiles = 1:numel(files)
thisfilename = fullfile(files(iFiles).folder,files(iFiles).name);
[~, basename, ext] = fileparts(image_folder);
citra = imread(thisfilename);
V = squeeze(citra);
fprintf('processing %s\n', basename);
fig = figure;
citra3 = montage(reshape(V,size(citra)), map, 'Indices', 3);
outfile = fullfile(outfolder, sprintf('%s-output-%03d.jpg', basename, iFiles));
saveas(citra3, outfile);
[n, r] = fboxcount(outfile,'slope');
%Perhitungan dimensi
df = -diff(log(n))./diff(log(r));
%Menampilkan hasil dimensi
disp(['Df= ' num2str(mean(df(4:8)))]);
end
can you help me?
I think fboxcount wants to be passed the content of the image, not the filename of the image.
So it has to be one image input, sir? Can't read lots of pictures right away?
I don't know. I was not able to find the source code for fboxcount.
this is sir.
function [n,r] = fboxcount(c,varargin)
c=rgb2gray(c);
ukuran=size(c)
error(nargchk(1,2,nargin));
dim = ndims(c)
width = max(size(c))
p = log(width)/log(2)
if p~=round(p) || any(size(c)~=width)
p = ceil(p)
width = 2^p
switch dim
case 2
mz = zeros(width, width);
mz(1:size(c,1), 1:size(c,2)) = c;
c = mz;
end
end
n=zeros(1,p+1)
n(p+1) = sum(c(:));
for g=(p-1):-1:0
siz = 2^(p-g);
siz2 = round(siz/2);
for i=1:siz:(width-siz+1);
for j=1:siz:(width-siz+1);
c(i,j) = ( c(i,j) || c(i+siz2,j) || c(i,j+siz2) || c(i+siz2,j+siz2) );
end
end
n(g+1) = sum(sum(c(1:siz:(width-siz+1),1:siz:(width-siz+1))));
end
n = n(end:-1:1);
r = 2.^(0:p); % box size (1, 2, 4, 8...)
if any(strncmpi(varargin,'slope',1))
s=-gradient(log(n))./gradient(log(r));
semilogx(r, s, 's-');
ylim([0 dim]);
xlabel('r, box size'); ylabel('- d ln n / d ln r, local dimension');
title([num2str(dim) 'D box-count']);
elseif nargout==0 || any(strncmpi(varargin,'plot',1))
loglog(r,n,'s-');
xlabel('r, box size'); ylabel('n(r), number of boxes');
title([num2str(dim) 'D box-count']);
end
if nargout==0
clear rn
end
That code would need revision in a number of places in order to be able to process multiple images a the same time, especially if they were different sizes. The code is not at all designed to be able to process images by name; it is also not designed for images that are already grayscale or indexed. It is designed only for one RGB images at a time, where the image array must be passed in (not the image file name.)
could you help me to fix it sir?
I could, but it would be pointless to do that, when you can simply loop processing the images one by one.
so it can not immediately read the images in one folder at a time, sir?
so if this code is concatenated into fboxcount to calculate fractal dimensions, is it possible sir?
if it can, could you help me to solve it?
image_folder = 'F:\kuliah\semester6\TA2\mencoba';
outfolder = 'F:\kuliah\semester6\TA2\mencoba\output';
if ~isdir(outfolder); mkdir(outfolder); end
load mri %I presume it has the variable map in it
files = dir(fullfile(image_folder, '*.jpg'));
for iFiles = 1:numel(files)
thisfilename = fullfile(files(iFiles).folder,files(iFiles).name);
[~, basename, ext] = fileparts(image_folder);
citra = imread(thisfilename);
V = squeeze(citra);
fprintf('processing %s\n', basename);
fig = figure;
citra3 = montage(reshape(V,size(citra)), map, 'Indices', 3);
outfile = fullfile(outfolder, sprintf('%s-output-%03d.jpg', basename, iFiles));
saveas(citra3, outfile);
[n, r] = fboxcount(outfile,'slope');
%Perhitungan dimensi
df = -diff(log(n))./diff(log(r));
%Menampilkan hasil dimensi
disp(['Df= ' num2str(mean(df(4:8)))]);
end
Walter Roberson
Walter Roberson on 17 Mar 2021
Edited: Walter Roberson on 17 Mar 2021
You are passing the file name to fboxcount.
Now for most purposes, MATLAB is happy to let you process a character vector as if it was number data (for example '1' treated as 49 and 'A' treated as 65), but a character vector is a vector, a 1 by something array, and fboxcount calls rgb2gray.
rgb2gray permits two different kinds of input parameter:
  1. the input can be a 3d array with third dimension being of length 3. That is, an rgb array. But a file name is 1 by something by 1, not 1 by something by 3, so rgb2gray is not willing to treat a file name as an rgb array
  2. the input can be a 2d array, something by 3, representing a color map, such as the jet colormap. The "something" (number of rows) can validly be 1, so rgb2gray is willing to consider treating a file name character vector as if it is a color map with a single row. However, colormap must have exactly 3 columns and your file name has more than 3 characters in it. If your file name happened to have exactly 3 characters including extension then rgb2gray would be willing to process it as a colormap with one row. Except that then after it passed the size check it would fail the check for the colormap being double precision.
So... no filename can pass the size and type requirements of rgb2gray. And therefore fboxcount is not going to work when you pass it a file name.
so sir it can't be in the form of a filename call. just calling one by one
You convinced me that you really really want to do that even though I have mentioned numerous ways it is a bad idea, so here you go:
if ispc()
image_folder = 'F:\kuliah\semester6\TA2\mencoba';
else
image_folder = fullfile(matlabroot, 'toolbox', 'images', 'imdata');
end
files = dir(fullfile(image_folder, '*.jpg'));
filenames = fullfile({files.folder}, {files.name});
filenames = char(filenames).';
filenames(end+1:ceil(size(filenames,1)/3)*3, :) = ' ';
filenames = rescale(double(filenames), 0, 1, 'InputMin', 32, 'InputMax', 127);
names_cmap = reshape(filenames, 3, []).';
[n, r] = fboxcount(names_cmap,'slope');
ukuran = 1×2
722 3
dim = 2
width = 722
p = 9.4959
p = 10
width = 1024
n = 1×11
0 0 0 0 0 0 0 0 0 0 0
This code will do the entire directory at the same time, in a single call.
%the below is copied directly from your posting except that an "end" was
%added
function [n,r] = fboxcount(c,varargin)
c=rgb2gray(c);
ukuran=size(c)
error(nargchk(1,2,nargin));
dim = ndims(c)
width = max(size(c))
p = log(width)/log(2)
if p~=round(p) || any(size(c)~=width)
p = ceil(p)
width = 2^p
switch dim
case 2
mz = zeros(width, width);
mz(1:size(c,1), 1:size(c,2)) = c;
c = mz;
end
end
n=zeros(1,p+1)
n(p+1) = sum(c(:));
for g=(p-1):-1:0
siz = 2^(p-g);
siz2 = round(siz/2);
for i=1:siz:(width-siz+1);
for j=1:siz:(width-siz+1);
c(i,j) = ( c(i,j) || c(i+siz2,j) || c(i,j+siz2) || c(i+siz2,j+siz2) );
end
end
n(g+1) = sum(sum(c(1:siz:(width-siz+1),1:siz:(width-siz+1))));
end
n = n(end:-1:1);
r = 2.^(0:p); % box size (1, 2, 4, 8...)
if any(strncmpi(varargin,'slope',1))
s=-gradient(log(n))./gradient(log(r));
semilogx(r, s, 's-');
ylim([0 dim]);
xlabel('r, box size'); ylabel('- d ln n / d ln r, local dimension');
title([num2str(dim) 'D box-count']);
elseif nargout==0 || any(strncmpi(varargin,'plot',1))
loglog(r,n,'s-');
xlabel('r, box size'); ylabel('n(r), number of boxes');
title([num2str(dim) 'D box-count']);
end
if nargout==0
clear rn
end
end
its getting error sir
I did not change your fboxcount function (other than to add an end statement to it). I included it with my post so that I could show you the output of executing the new code that I posted at the top.
So the code above and below is combined, sir?
Store the following code in the file named box_the_names.m
image_folder = 'F:\kuliah\semester6\TA2\mencoba';
files = dir(fullfile(image_folder, '*.jpg'));
filenames = fullfile({files.folder}, {files.name});
filenames = char(filenames).';
filenames(end+1:ceil(size(filenames,1)/3)*3, :) = ' ';
filenames = rescale(double(filenames), 0, 1, 'InputMin', 32, 'InputMax', 127);
names_cmap = reshape(filenames, 3, []).';
[n, r] = fboxcount(names_cmap,'slope');
and use your existing fboxcount.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!