Как исправить этот код, я пытался сделать это с 2-х дней, но не смог сделать это. Пожалуйста помоги.Построение простого двоичного графика функции определения порога в матрице, как показано на рисунке и рисунке
функция BinaryThresholding (I)
%Reading minimum and maximum intensity values of Image I.
Min = min(I(:));
Max = max(I(:));
%Finding the middle value (thresholding) A.K.A m below.
m = (Min+Max)/2;
%For ploting the thresholding tranformation function we will also
%define X and Y (Ranges) parameters based upon min and max range and the
%process them according to our transformation algoritm as below.
x = (Min/Max):(Max/Max); %input range.
y = x;
% Now we will apply alogithm to threshold the threshold I at
% the middle intensity,thresholdingValue, of its dynamic
% range [minValue maxValue]. G is our processed image.
[Rows, Columns, Channels] = size(I);
%First we will check if the image is gray-scale and conver it if not.
if(Channels==3)
I = rgb2gray(I);
end
%Processing Image.
for i=1:1:Rows
for j=1:1:Columns
if(I(i,j)< m)
G(i,j) = 0;
else
G(i,j) = 1;
end
end
end
% Algorithm works great :D --> Testingw with : figure, imshow(G);
%Displaying image on a new figure window.
figure('Name','Image Thresholding','NumberTitle','on'),
subplot(1,3,1); imshow(I); title(['Input Image - Dynamic Range: [',num2str(Min),' ',num2str(Max),']']);
subplot(1,3,2); imshow(G); title(['Output Image - Threshold:' num2str(m)]);
subplot(1,3,3); plot(x,y); title('Plot of Thresholding Transformation Function');
%Let pixel info to be shown on the figure.
impixelinfo;
%Writing the image G as a .png file to the current folder (Drive D:/).
% imwrite (G, 'D: /G.png');
конец Desired output
'y = x', так что это нормально, что у вас есть диагональная линия. вместо этого выполняем 'y (x = m) = 1;'. Вы также должны избавиться от циклов: 'G = I; G (I = m) = 1; 'Также учтите, что если ваше изображение полностью черное, тогда Max будет 0, поэтому' x' будет иметь неправильные значения, так как (Min/0 = Inf) –
также в 'imwrite' путь D: похоже, что вы используете систему Windows, поэтому вы должны использовать обратную косую черту \, так что отдельные папки вместо косой черты/используемые в Linux-системах. –
@SembeiNorimaki Я писал ответ самостоятельно, но похоже, что вы были быстрее, я не хотел копировать :) – marcoresk