Plotting mean of the columns of image on that image

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 件のコメント

Guillaume
Guillaume 2014 年 9 月 1 日
編集済み: Guillaume 2014 年 9 月 1 日
What do you mean by plotting?
- superimpose a line graph of mean values with scale axes?
- add a row of pixels whose grey value is the mean?
- something else?
Yawar Rehman
Yawar Rehman 2014 年 9 月 1 日
編集済み: Yawar Rehman 2014 年 9 月 1 日
add a row of pixels whose grey value is the mean, which can divide the image in two portions horizontally.
Yawar Rehman
Yawar Rehman 2014 年 9 月 1 日
編集済み: Yawar Rehman 2014 年 9 月 1 日
actually, i want to make two portions of an image horizontally by taking the mean of the grey values of its columns some thing like this, the mean (red line) may not be a straight line rather it will be a curve i guess
Yawar Rehman
Yawar Rehman 2014 年 9 月 2 日
Can anyone help me out in this please!!
Guillaume
Guillaume 2014 年 9 月 2 日
編集済み: Guillaume 2014 年 9 月 2 日
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
Yawar Rehman 2014 年 9 月 3 日
I think i have been using the wrong term "plotting" to express myself.
mean function of Matlab returns with the mean of the columns of an image. for finding that where does that column's mean value lie, i guess we have to go through the column pixel search. when there is an exact match or relatively nearer match that pixel coordinate is stored.
finally, mean of the stored pixel coordinates may result in a straight line as i have shown.
so far i have been using this code to do this.
load clown
M = mean(X);
figure, imshow(X,[]);
hold on;
down=zeros(1,numel(M));
for columnIdx = 1:numel(M)
meanValue = M(columnIdx);
% find locations of gray-scale values equal to the mean
idx = find(X(:, columnIdx) == meanValue);
% if not exact match find relative near alues ~ to the mean
if (isempty(idx) == 1)
[~, idx] = min(dist(X(:,columnIdx),meanValue));
end
% select one value & make a vector for plotting
down(1,columnIdx)=idx(1);
end
% plot
plot([1, numel(M)], [mean(down), mean(down)], 'r-')
hold off;
it will result in the following output,
actually what i am interested in are the 'y' pixel coordinates so that i can divide the image into 2 parts. superimposing plots cannot do this i guess. please correct me if my intuition is wrong or if possible can you please suggest a simpler way to do this. thanks :)
Guillaume
Guillaume 2014 年 9 月 3 日
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
Yawar Rehman 2014 年 9 月 3 日
i am not sure it is the correct answer though it is doing what i want! + i am curious if someone could suggest me a way for avoiding for loop

サインインしてコメントする。

 採用された回答

Guillaume
Guillaume 2014 年 9 月 3 日

1 投票

To do what you want without a loop:
[~, down] = min(abs(bsxfun(@minus, X, mean(X))));
bsxfun just subtract the column mean from every value in X, and then you take the index of the minimum of the absolute value of that difference.

1 件のコメント

Yawar Rehman
Yawar Rehman 2014 年 9 月 3 日
kudos ... thank you guillaume

サインインしてコメントする。

その他の回答 (2 件)

Michael Haderlein
Michael Haderlein 2014 年 9 月 2 日

1 投票

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 件のコメント

Guillaume
Guillaume 2014 年 9 月 2 日
plot will always create a new axis unless you precede it with:
hold on
or
hold all
Michael Haderlein
Michael Haderlein 2014 年 9 月 2 日
ok, but why are the other axes deleted?
Michael Haderlein
Michael Haderlein 2014 年 9 月 2 日
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)])
Guillaume
Guillaume 2014 年 9 月 2 日
編集済み: Guillaume 2014 年 9 月 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
Guillaume 2014 年 9 月 2 日
All the high level plotting functions (plot, bar, surf, etc.) create a new axis.
Michael Haderlein
Michael Haderlein 2014 年 9 月 2 日
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
Yawar Rehman 2014 年 9 月 3 日
thank for the answer haderlein. i have commented on the question which i asked. can you please review it

サインインしてコメントする。

Guillaume
Guillaume 2014 年 9 月 2 日
編集済み: Guillaume 2014 年 9 月 2 日

1 投票

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 件のコメント

Michael Haderlein
Michael Haderlein 2014 年 9 月 2 日
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 ;-)

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeImages についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by