在matlab中绘制一个简单的binaryThresholding函数图,如图和图所示

时间:2016-11-09 12:31:47

标签: matlab plot graph

我如何更正此代码,我一直试图从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

1 个答案:

答案 0 :(得分: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也从最小到最大等间距(从实际输出中可以看出)。尝试类似:

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