How to overlay a scatter plot over an image (png,jpg) with correct marker colors?
50 次查看(过去 30 天)
显示 更早的评论
I am unable to overlay a scatter plot over a png/jpg format image for correct marker colors. The color of all the markers remain the same somehow. I am doing following.
figure;
imshow('image');
hold on;
scatter(x,y,size,color,'filled');
%color is an array of the same size as x and y and represent the power in this case
hold off;
The result without the image is as follows
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/153970/image.png)
The result including the image and incorrect marker color is as follows.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/153973/image.png)
I would greatly appreciate for assistance in this matter. The available solutions in the other answer threads doesn't work in my case unfortunately.
0 个评论
采纳的回答
Walter Roberson
2016-2-3
Your "color" array is a vector of values the same length as x and y. That indicates that the actual color is to be interpolated according to caxis and the active color map. caxis by default scales according to all of the data in the axes. When you have the image in the axes as well as the scatter plot, you are potentially changing the range of data in the axes.
If you are happy with the colors at present, then I suggest that you get the File Exchange contribution "freezeColors" and then use
ax = gca();
scatter(ax, x,y,size,color,'filled');
freezeColors(ax);
hold(ax, 'on');
imh = imshow(ax, 'image');
hold(ax, 'off')
uistack(imh, 'bottom')
Note:
If you look carefully at your combined graph that you posted, you will see that your scatter plot is upside down. This is because image() and imagesc() and imshow() helpfully set "axis image" for you, one of the effects of which is to set the axis Ydir property to 'reverse' so that the first row of the image goes into the top of the display and the rows increase downwards -- as opposed to the normal X Y system where the Y increases upwards.
One of the side effects of the order that I show here is that with the "hold on" done before the imshow() command, the YDir property will be held, so that the scatter plot will not be flipped upside down. But that means that the image will be flipped relative to what you would have seen before. You need to decide which coordinate system you want; if you want the image the "usual" way and the scatter plot upside down, then
set(ax, 'YDir', 'reverse')
before the first "hold" call.
When you are combining image and data, it is common to need to read the image into an array with imread() and then to flipud() the array before using imshow() on it, and to not use YDir reverse
3 个评论
更多回答(1 个)
Ian Hunter
2018-3-21
编辑:Walter Roberson
2018-3-26
Thanks Walter!
This helped me get a scatter plot on the same axis as my image plot.
When using your suggested library (downloaded today on 3/21/18) with Matlab 2017B, running the code:
figure;
ax = gca();
colormap jet
c = linspace(0,1,length(gridcoords(:,1)));
scatter(ax,(double(gridcoords(:,1)))*double(width)/size(projImg,2),double(gridcoords(:,2))*(double(height)/size(projImg,1)),[],c,'filled'); %,[],c);
freezeColors(ax);
hold(ax,'on');
imh = imshow((double(imT)/255.0 + double(projImResized(:,:,3)) )/2.0);
hold(ax, 'off')
uistack(imh, 'bottom')
The freezeColors(ax) command returns the err:
Error using matlab.graphics.chart.primitive.Scatter/set
Error setting property 'CData' of class 'Scatter':
Value must be a scalar, vector or array of numeric type
Error in freezeColors (line 157)
set(hh,'CData',realcolor);
Error in GUI_rectangle_bdygradient/recVid_callback (line 310)
freezeColors(ax);
Error while evaluating UIControl Callback.
The only factor besides freezeColors no longer working as you figured include:
1. I have defined c and colormap in a way incompatible with freezeColors
2. I am using this within a GUI in a static environment.
Thanks for your help thus far. If you have any further ideas, I'd be a thankful person.
Cheers,
Ian
2 个评论
Walter Roberson
2018-3-26
This is a bug in freezeColors for scatter plots. I have written the author with an explanation of the problem.
Walter Roberson
2018-6-13
See https://www.mathworks.com/matlabcentral/answers/405427-two-colormaps-on-map-projection#answer_324574 where I work around this problem with freezeColors on scatter plots in R2014b and later.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Convert Image Type 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!