Plotting mean of the columns of image on that image
Show older comments
I am struggling to plot the mean of the columns of the gray scale image on the same image. Mean of the columns of an image can be found by using Mean command in Matlab, but i don't have any idea how to plot it in image coordinates. Any idea or help is appreciated! Thank you :)
8 Comments
Yawar Rehman
on 1 Sep 2014
Edited: Yawar Rehman
on 1 Sep 2014
Yawar Rehman
on 1 Sep 2014
Edited: Yawar Rehman
on 1 Sep 2014
Yawar Rehman
on 2 Sep 2014
Adding "a row of pixels whose grey value is the mean" is trivial. However, I don't understand "which can divide the image in two portions horizontally". What do you mean by that?
Similarly, with the image you've shown, I don't understand what the red line is supposed to represent. How do you choose the row where the red points go?
Yawar Rehman
on 3 Sep 2014
Guillaume
on 3 Sep 2014
Well, you seem to have answered your own question there. Doesn't the algorithm you wrote do what you want?
The only thing I would change is the inside of your for loop to:
meanValue = M(columnIdx);
[~, idx] = min(abs(X(:, columnIdx) - meanValue);
down(1, columnIdx) = idx;
Yawar Rehman
on 3 Sep 2014
Accepted Answer
More Answers (2)
Michael Haderlein
on 2 Sep 2014
Actually, I believe he just gave exactly contradicting answers on Guillaume's question. As I get it, this is what the result should look like:

I did it with the following code, however, I have no idea why a more straight-forward way didn't work (maybe someone can tell me?):
img=imread('grayscaleexample.bmp');
figure, imshow(img)
ha=axes('color','none','position',get(gca,'position'));
tf=figure;
hp=plot(1:size(img,2),mean(img));
set(hp,'parent',ha)
set(ha,'xlim',[1,size(img,2)])
delete(tf)
So the idea is to create another axes above the ones showing the image and plot the mean of the grayscale there. For some reason (please help) I have to draw the plot somewhere else first and then copy it to the axes where it's shown finally. When I plot directly in the axes on top of the ones with the image, the image axes will be deleted - I have no idea why.
What I mean is the following:
>> figure, imshow(img)
>> axes('position',get(gca,'position'))
>> get(gcf,'children')
ans =
1.0e+03 *
1.4420
1.4370
>> plot(1:size(img,2),mean(img));
>> get(gcf,'children')
ans =
1.4370e+03
7 Comments
Guillaume
on 2 Sep 2014
plot will always create a new axis unless you precede it with:
hold on
or
hold all
Michael Haderlein
on 2 Sep 2014
ok, but why are the other axes deleted?
Michael Haderlein
on 2 Sep 2014
Anyway, thanks for reminding me on hold. I wasn't aware that plot actually creates new axes but thought it would delete the children of the current axes. Thus, it goes much easier with:
figure, imshow(img)
axes('position',get(gca,'position'))
hold all
plot(1:size(img,2),mean(img));
set(gca,'color','none','xlim',[1 size(img,2)])
It's not deleted. You just can't see the old one because of the white background of the new one. If you used:
axes('position',get(gca,'position'), 'Color', 'none')
You'd see the old axis since the new one is transparent.
Anyway, you don't need to create a new axis.
Guillaume
on 2 Sep 2014
All the high level plotting functions (plot, bar, surf, etc.) create a new axis.
Michael Haderlein
on 2 Sep 2014
Yes, they create new axes if no axes are there and they also reset part of the axes properties in case there are some already. However, in this example, the old axes really were deleted as you see in my example. It has nothing to do with the background.
Yawar Rehman
on 3 Sep 2014
If you do indeed want to plot the mean on the graph (i.e. the other option I offered) then:
imshow(img);
hold on
plot(1:size(img, 2), mean(img), 'r');
I'm not sure what you mean by having two portions of the image though.
1 Comment
Michael Haderlein
on 2 Sep 2014
oops, you're totally right with that, of course plotting in the same axes is possible. This axes deleting behavior confused me so much that I didn't think about this anymore ;-)
Categories
Find more on Annotations 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!
