graph cuts result produce two image
2 views (last 30 days)
Show older comments
nur hanani
on 10 Nov 2019
Commented: Walter Roberson
on 20 Nov 2019
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/247573/image.png)
from the graph cuts codes above, it will produce two image.. i want to ask is the first 'if' for the first image and the second 'if' for second image? or how?
5 Comments
Walter Roberson
on 10 Nov 2019
Your code already produces two plots in two different figures -- or would if you fixed the spacing.
Accepted Answer
KALYAN ACHARJYA
on 10 Nov 2019
Edited: KALYAN ACHARJYA
on 10 Nov 2019
Your question is still not clear, but I am trying to answer In the code, "if condition" true, then only it shows first figure window.
When this section of the code execute, (if condition must be true), then only it show the first figure. Please ensure that "im" must be gray level image.
if size(im,3)==1 % Gray-level image
img=zeros(sz(1),sz(2));
j=1;
% First Figure here
figure, imagesc(im); axis off; hold on; colormap gray;
fori=0:k_max-1
LL=(L_global_min==i);
is_zero=sum(sum(LL));
if is_zero
img(:,:,1)=img(:,:,1)+LL*c(j,1);
j=j+1;
end
if i~=i_ground
color=[rand rand rand];
contour(LL,[1 1],'LineWidth',2.5,'Color',color); hold on;
end
end
#2nd figure, it seems ok.
% Second Figure here
figure, imagesc(img); axis off;
end
Or try with this one
if size(rgb2gray(im),3)==1 % Gray-level image
5 Comments
KALYAN ACHARJYA
on 10 Nov 2019
Edited: KALYAN ACHARJYA
on 10 Nov 2019
As per your original code:
Figure 1
imagesc(im); axis off; hold on; colormap gray;
Figure 2
figure(2),
imagesc(img); axis off;
More Answers (1)
Walter Roberson
on 11 Nov 2019
No.
At the beginning of the code, either no figure or active or some unspecified figure is active.
At some point you reach
if size(im, 3)==1 % Gray-level image
If that statement is false then none of the rest of your code that you posted gets executed, and nothing at all gets plotted. So it is not that if statement that controls whether you are plotting to figure 1 or figure 2.
Assuming the statement is true (because you end the code if it is not) then you reach
imagesc(im); axis off; hold on; colormap gray;
If there is no active figure then when the imagesc() call goes to find out which axes it is drawing into, the MATLAB graphics library will create a new figure. It will have the lowest available (unused) integer figure number, which would usually be 1, but there are ways in which it is possible for any number of figures to already exist but for none of them to be happen to be active, so we cannot promise that the newly created figure is figure 1... it would just usually be figure 1. An axes would be created inside this new figure, and that would become the current axes.
If there is an active figure when imagesc() is called, then the MATLAB graphics library would look for the current axes in it; if there is no current axes (either because no axes exist in the figure or none of them are active) then MATLAB would create a new axes to draw in, making that the current axes. If there does happen to be a current axes already in the current figure, then it remains the current axes. It is entirely possible that a current figure exists that is not figure 1; if it happened to be figure 17 that was active, then it would be figure 17 that would be drawn on, because the graphics output location was all left to the default.
imagesc() and the other commands on that line would then apply their work to the current axes, no matter what figure it is on. Often that would be part of figure 1, but not always.
This drawing is done conditional only on the image being grayscale (and no drawing is done if it is not grayscale.) It is not conditional upon iszero .
Then you have the for i=0:k_max-1 loop. You have an if is_zero inside that loop, but that if statement only changes data and does not itself trigger any drawing.
After the if is_zero you arrive at the if i~=i_ground where i_ground was initialized to 0 and never changed. The first iteration of the for i=0:k_max-1 loop has i of 0, which would fail the if i~=0 test, so the first iteration of the for i=0:k_max-1 loop would skip the body of the if statement, after which you would be finished with the iteration for i=0 and would loop back and proceed with the i=1 iteration. That time, if i~=i_ground is true because 1~=0 so the body of the if statement will be run, leading to contour() being called. That contour call will go looking for an active axes and will find it -- the same one that the imagesc() was called in; it would draw in there. You would continue this process for the rest of the for i iterations, drawing several contours in that active axes (again, probably in figure 1, but that is not certain.) At no point in this process has if is_zero had any influence as to whether graphics are drawn or not or where there are drawn.
Eventually the for i=0:k_max-1 loop terminates.
After the for i loop has been run completely, figure(2) is called. If there is no figure 2 existing then a figure 2 is created and made the active figure; otherwise an existing figure 2i s made the active figure.
At this point, imagesc(img) is called. That is going to look for a current axes in the current figure, which is figure 2. If figure 2 had no current axes then an axes will be created and made the current axes. The imagesc() will be run in the current axes, drawing in figure 2.
Notice this drawing into figure 2 is done unconditionally provided that we passed the test about img being grayscale (and if we did not pass that test then no graphics are drawn at all.) Whether is_zero is true or not has no effect on whether figure 2 is drawn into.
So, provided that im was grayscale, we have drawn into whatever the active figure was (creating a figure if needed), which would commonly be figure 1 but not always. And then we specifically drew into figure 2.
Now there is an important subtle point to notice: provided that im was grayscale, then the last figure we drew into was figure 2. And that means that after the code, figure 2 is the active figure.
Why does this matter? Well, suppose we run the code again without deleting any figures. We get to the
imagesc(im); axis off; hold on; colormap gray;
line that earlier we said would probably go into figure 1 but not always. In particular we said it will go into the current axes of the current figure if there is a current figure. Well, the second time running the code, the current figure is figure 2, so no matter where the code drew to the first time around, the second time you run it, it is going to draw into figure 2, the same figure that is drawn into by the imagesc(img); axis off; It so happens that hold on is left in effect after the for i loop that creates the contours, so the imagesc(ig) is going to draw on top of all of those contours.
None of this depends upon any if statement other than whether im is grayscale or not. What it does depend upon is the fact that you left yourself defaulting to the current figure and current axes for the imagesc(im) and contours instead of having specifically selected a particular figure and particular axes.
6 Comments
Walter Roberson
on 20 Nov 2019
LL is a matrix of logical values. sum() of a 2D matrix of logical values is a vector of the number of true values in each column. sum() of that vector gives the total number of true values in the matrix. When you use if on a variable without doing any relation tests on the variable, what you are testing is whether the variable is zero (in which case the test is false) or non-zero (in which case the test is true.)
So... what is being tested there is really whether there is even one value in LL that is true.
Another way of writing the same test, without doing the sum, would be
if any(LL(:))
See Also
Categories
Find more on Line Plots in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!