MATLAB Answers

0

graph cuts result produce two image

Asked by nur hanani on 10 Nov 2019 at 6:57
Latest activity Commented on by Walter Roberson
on 12 Nov 2019 at 8:55
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 at 9:20
Your code needs some repairs. You need a space between if and what follows it, and you need a space between for and the variable that follows it.
Your indentation is not good.
What purpose does your variable area serve?
Walter Roberson
on 10 Nov 2019 at 9:21
Your code already produces two plots in two different figures -- or would if you fixed the spacing.
nur hanani on 10 Nov 2019 at 14:55
can i know from where it produces two plots in two different figures? i mean in the codes where it states two figure will produce? sorry i'm not really good in matlab:)

Sign in to comment.

Tags

2 Answers

Answer by KALYAN ACHARJYA on 10 Nov 2019 at 8:46
Edited by KALYAN ACHARJYA on 10 Nov 2019 at 15:42
 Accepted Answer

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

actually this codes i got from online so i tried to understand,
i want to ask, which codes line that refers to figure 1 and figure 2?
from my understanding the "if is_zero" will produce figure 1 and "if i=i_ground" will produce figure 2, am i correct or wrong.
As per your original code:
Figure 1
imagesc(im); axis off; hold on; colormap gray;
Figure 2
figure(2),
imagesc(img); axis off;
nur hanani on 11 Nov 2019 at 4:14
so, the 'if' condition after figure 1 codes line is refer to figure 1 or figure 2?

Sign in to comment.


Answer by Walter Roberson
alrededor de 1 hora ago

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.

  2 Comments

nur hanani on 12 Nov 2019 at 3:25
thanks for your answer.
but i want to ask, if whatever in figure 2 will be drawn in imagesc(img) which is figure 1,why the result produce different, where the image still same but different in color.
Walter Roberson
on 12 Nov 2019 at 8:55
You do not imagesc(img) in the first figure (whatever the active figure happens to be.) You imagesc(im) which is a different set of data.
If the question is why the set of colors for imagesc(im) in one figure differs from the set of colors for the imagesc(img) in figure 2, then I can think of a few possibilities.
It would help to have a copy of your image to test with.

Sign in to comment.