2016-11-09 2 views
1

Как исправить этот код, я пытался сделать это с 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

Actual output

+0

'y = x', так что это нормально, что у вас есть диагональная линия. вместо этого выполняем 'y (x = m) = 1;'. Вы также должны избавиться от циклов: 'G = I; G (I = m) = 1; 'Также учтите, что если ваше изображение полностью черное, тогда Max будет 0, поэтому' x' будет иметь неправильные значения, так как (Min/0 = Inf) –

+0

также в 'imwrite' путь D: похоже, что вы используете систему Windows, поэтому вы должны использовать обратную косую черту \, так что отдельные папки вместо косой черты/используемые в Linux-системах. –

+0

@SembeiNorimaki Я писал ответ самостоятельно, но похоже, что вы были быстрее, я не хотел копировать :) – marcoresk

ответ

1

Из названия выходов я думаю, что вы хотите, чтобы исправить эту строку

subplot(1,3,3); plot(x,y); title('Plot of Thresholding Transformation Function'); 

, что означает только исправить эти пару строк

x = (Min/Max):(Max/Max); %input range. 
y = x; 

это означает: x равномерно распределен от min до max ... и Y также равномерно распределен от min до max (как вы можете видеть из вашего фактического выхода). Попробуйте что-нибудь вроде:

x = (Min/Max):(Max/Max); %input range. 
y = zeros(length(x)); 
for i=1:length(x) 
if (x > m) 
    y(i) = 1; 
end 
end 
+0

позвольте мне проверить его :) – Ahsan9981

Смежные вопросы