How to get matrix from plot ???

Hi,I plotted a CYY and CXX having certain values .....
plot(CYY,CXX,'g*');
Now I want to get those plotted values marked '*' in plot in an matrix, can it be done???

2 comentarios

Jan
Jan el 24 de Feb. de 2018
It is still not clear to me, what your inputs are. John BG's solution assumes, that you have an image array of the plot, e.g. as if it is imported by imread. Is this correct? Or do you have the values of CYY and CXX available and want to create the image array e.g. to write it to a PNG file?
Mohammad Bhat
Mohammad Bhat el 24 de Feb. de 2018
I have CXX and CYY....

Iniciar sesión para comentar.

 Respuesta aceptada

John BG
John BG el 23 de Feb. de 2018

0 votos

Hi Mohammad
1.
Allow me to start with the same image you have supplied op.jpg
close all;clc;clear all
input_image='001.jpg';
A=imread(input_image);
figure(1);imshow(A);
.
.
Attached both start image and script to help reproduce result.
.
2. Amplify the image to avoid some too close stars overlapping and then alias would happen
.
Ar=imresize(A,20);
.
3. Green minus Blue shows a good response to blur a bit the starts yet keeping sharpness of each peak
.
A21=Ar(:,:,2)-Ar(:,:,3); % green - blue
figure(2);h1=imshow(A21);
.
.
4. Comment, at this point imcontrast can be used to show what range of pixel values are most useful
.
imcontrast(h1); % thresholds 59 and 123
.
.
instead one can manually perform the same pixel selection without imcontrast
th1=100;th2=59;
A21(A21<th2)=0;
A21(A21>=th1)=255;
figure(3);imshow(A21);
.
.
5. Reducing the size of the image a bit to save time i next step finding 2D peaks
.
A2=imresize(A21,.1);
figure(4);imshow(A2);
6.
Use Natan's function FastPeakFind.m available here:
.
pk3=FastPeakFind(A2);
figure(5);imagesc(A2); hold on
figure(5);plot(pk3(1:2:end),pk3(2:2:end),'r+')
.
.
7.
FastPeakFind returns a single file list of coordinates that are the [x y] coordinates interleaved, to obtain the paired coordinates:
x_peak=pk3(1:2:end);
y_peak=pk3(2:2:end);
p_stars=[x_peak y_peak];
8.
The amount of stars found is:
size(p_stars,1)
ans =
152
Mohammad
if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance for time and attention
John BG
A couple additional comments:
9.1. tried *imfindcircles* but all tested parameters did not return any result as close as the previous steps.
[centers, radii, metric] = imfindcircles(A21,[8 15],'ObjectPolarity','bright','Sensitivity',.94);
[centers, radii, metric] = imfindcircles(A21,[15 31],'Method','PhaseCode','ObjectPolarity','bright','Sensitivity',.94);
radii=floor(radii);
viscircles(centers, radii,'EdgeColor','r');
9.2. tried *findpeaks2.m* available from here:
% https://www.mathworks.com/matlabcentral/fileexchange/46806-findpeaks2-m?s_tid=srchtitle
% [pk2,id2]=findpeaks2(A2);
2 minutes waiting and still busy.
radiiStrong5 = radii(1:5);
metricStrong5 = metric(1:5);
viscircles(centers, radii,'EdgeColor','r');

14 comentarios

Mohammad Bhat
Mohammad Bhat el 24 de Feb. de 2018
Thank You Sir for this helpful answer, in the meanwhile I found some answer I am posting in a comment for your approval , please see it and give me some comments on it:-
plot(CYY,CXX,'k*'); % plot the figure
%gcf=figure;
% hAxes = gca;
% %hAxes.XRuler.Axle.LineStyle = 'none';
axis off % get rid of axes
F = getframe(gcf);
[xF, map] = frame2im(F);
Bpl = im2bw(xF,map,th); % binaraise the image 'th'= 0.839
baseFileName=sprintf('%d.jpg',il);
Bpl=flipud(Bpl); % flips the image upside down
AdjMat=Bpl;
folder1=sprintf('%s',tarGetFolder);
imwrite(Bpl,[folder1,baseFileName]);
clf('reset');
John BG
John BG el 24 de Feb. de 2018
Hi Mohammad
the key point that other readers of your question did not understand was that you start with the plot only, not the location of the points: getting the points is the objective.
Because of the way you simulate the data, with
plot(CYY,CXX,'g*')
they assumed that because you already had the data of interest, all you had to do is to pull it back, let me explain:
this
plot(CYY,CXX,'g*')
is the same as
h=plot(CYY,CXX,'g*')
Let's say that you have the following CYY CXX:
h=plot([1:1:10],[2:2:20])
h is a handle to the plot containing the following:
AlignVertexCenters: 'off'
Annotation: [1×1 matlab.graphics.eventdata.Annotation]
BeingDeleted: 'off'
BusyAction: 'queue'
ButtonDownFcn: ''
Children: [0×0 GraphicsPlaceholder]
Clipping: 'on'
Color: [1×3 double]
CreateFcn: ''
DeleteFcn: ''
DisplayName: ''
HandleVisibility: 'on'
HitTest: 'on'
Interruptible: 'on'
LineJoin: 'round'
LineStyle: '-'
LineWidth: 0.500000000000000
Marker: 'none'
MarkerEdgeColor: 'auto'
MarkerFaceColor: 'none'
MarkerIndices: [1 2 3 4 5 6 7 8 9 10]
MarkerSize: 6
Parent: [1×1 Axes]
PickableParts: 'visible'
Selected: 'off'
SelectionHighlight: 'on'
Tag: ''
Type: 'line'
UIContextMenu: [0×0 GraphicsPlaceholder]
UserData: []
Visible: 'on'
XData: [1 2 3 4 5 6 7 8 9 10]
XDataMode: 'manual'
XDataSource: ''
YData: [2 4 6 8 10 12 14 16 18 20]
YDataSource: ''
ZData: [1×0 double]
ZDataSource: ''
if you key in
h.XData
h.YData
=
Columns 1 through 9
1 2 3 4 5 6 7 8 9
Column 10
10
=
Columns 1 through 9
2 4 6 8 10 12 14 16 18
Column 10
20
you get back the data used to plot.
But you clarified that you don't have this data, this precisely what we are after.
Without the locations of the points one cannot plot.
Function plot doesn't start unless you supply the precise locations of each point to plot, no guessing: no points? no plot.
But if you already have the location of the points there wouldn't be any need to ask the question, would it?
You added the comment to a now deleted attempted answer that all you had as start point is the plotting itself, probably the actual print-out or a .pdf file or .jpg with the resulting graph, but not the precise coordinates, and that is exactly what you need.
If instead of capturing the handle of the plot you choose to capture the handle to the axes,
ax=gca
ALim: [0 1]
ALimMode: 'auto'
ActivePositionProperty: 'outerposition'
AmbientLightColor: [1 1 1]
BeingDeleted: 'off'
Box: 'on'
BoxStyle: 'back'
BusyAction: 'queue'
ButtonDownFcn: ''
CLim: [0 1]
CLimMode: 'auto'
CameraPosition: [1×3 double]
CameraPositionMode: 'auto'
CameraTarget: [5.500000000000000 11 0]
CameraTargetMode: 'auto'
CameraUpVector: [0 1 0]
CameraUpVectorMode: 'auto'
CameraViewAngle: 6.608610360311923
CameraViewAngleMode: 'auto'
Children: [1×1 Line]
Clipping: 'on'
ClippingStyle: '3dbox'
Color: [1 1 1]
ColorOrder: [7×3 double]
ColorOrderIndex: 2
CreateFcn: ''
CurrentPoint: [2×3 double]
DataAspectRatio: [4.500000000000000 9 1]
DataAspectRatioMode: 'auto'
DeleteFcn: ''
FontAngle: 'normal'
FontName: 'Helvetica'
FontSize: 10
FontSmoothing: 'on'
FontUnits: 'points'
FontWeight: 'normal'
GridAlpha: 0.150000000000000
GridAlphaMode: 'auto'
GridColor: [1×3 double]
GridColorMode: 'auto'
GridLineStyle: '-'
HandleVisibility: 'on'
HitTest: 'on'
Interruptible: 'on'
LabelFontSizeMultiplier: 1.100000000000000
Layer: 'bottom'
Legend: [0×0 GraphicsPlaceholder]
LineStyleOrder: '-'
LineStyleOrderIndex: 1
LineWidth: 0.500000000000000
MinorGridAlpha: 0.250000000000000
MinorGridAlphaMode: 'auto'
MinorGridColor: [1×3 double]
MinorGridColorMode: 'auto'
MinorGridLineStyle: ':'
NextPlot: 'replace'
OuterPosition: [0 0 1 1]
Parent: [1×1 Figure]
PickableParts: 'visible'
PlotBoxAspectRatio: [1×3 double]
PlotBoxAspectRatioMode: 'auto'
Position: [1×4 double]
Projection: 'orthographic'
Selected: 'off'
SelectionHighlight: 'on'
SortMethod: 'childorder'
Tag: ''
TickDir: 'in'
TickDirMode: 'auto'
TickLabelInterpreter: 'tex'
TickLength: [1×2 double]
TightInset: [1×4 double]
Title: [1×1 Text]
TitleFontSizeMultiplier: 1.100000000000000
TitleFontWeight: 'bold'
Type: 'axes'
UIContextMenu: [0×0 GraphicsPlaceholder]
Units: 'normalized'
UserData: []
View: [0 90]
Visible: 'on'
XAxis: [1×1 NumericRuler]
XAxisLocation: 'bottom'
XColor: [1×3 double]
XColorMode: 'auto'
XDir: 'normal'
XGrid: 'off'
XLabel: [1×1 Text]
XLim: [1 10]
XLimMode: 'auto'
XMinorGrid: 'off'
XMinorTick: 'off'
XScale: 'linear'
XTick: [1 2 3 4 5 6 7 8 9 10]
XTickLabel: {10×1 cell}
XTickLabelMode: 'auto'
XTickLabelRotation: 0
XTickMode: 'auto'
YAxis: [1×1 NumericRuler]
YAxisLocation: 'left'
YColor: [1×3 double]
YColorMode: 'auto'
YDir: 'normal'
YGrid: 'off'
YLabel: [1×1 Text]
YLim: [2 20]
YLimMode: 'auto'
YMinorGrid: 'off'
YMinorTick: 'off'
YScale: 'linear'
YTick: [1×10 double]
YTickLabel: {10×1 cell}
YTickLabelMode: 'auto'
YTickLabelRotation: 0
YTickMode: 'auto'
ZAxis: [1×1 NumericRuler]
ZColor: [1×3 double]
ZColorMode: 'auto'
ZDir: 'normal'
ZGrid: 'off'
ZLabel: [1×1 Text]
ZLim: [-1 1]
ZLimMode: 'auto'
ZMinorGrid: 'off'
ZMinorTick: 'off'
ZScale: 'linear'
ZTick: [-1 0 1]
ZTickLabel: ''
ZTickLabelMode: 'auto'
ZTickLabelRotation: 0
ZTickMode: 'auto'
from the axes handle you can pull the figure handle
hf=ax.Parent
a broader structure wrapped around the data.
Or again read the data:
ax.Children.XData
=
Columns 1 through 9
1 2 3 4 5 6 7 8 9
Column 10
10
ax.Children.YData
=
Columns 1 through 9
2 4 6 8 10 12 14 16 18
Column 10
20
Now you can see that plot handles, axes handles, and figure handles deliver more or less complex structures that are really useful when solving problems, but in essence they are all wrapped around the critical crucial data, the lack of which originated the question, if we already have the data, no need to ask for the data, right?
Hope it helps, all the best
John BG
John BG
John BG el 24 de Feb. de 2018
the other functions getframe assumes figure, that in turn assumes the data is already available, for the plot.
frame2im, im2bw, sprintf, all revolve around the data that we don't have, at least at the start point of the question.
Mohammad Bhat
Mohammad Bhat el 24 de Feb. de 2018
I have CXX and CYY , basically these, from them I am plotting graph for Handwritten words,when these r taken in Isolation they don't have meaning
Mohammad Bhat
Mohammad Bhat el 24 de Feb. de 2018
I have computed CXX and CYY by some croteria aftet ploting I am getting desired figure or graph , but I needed Adjacencey matrix of that graph ,so that I will transfer that Adjacency Matrix to some other function for further processing...CXX and CYY are not giving me adjacency matrix....i.e., why I plotted graph from that graph I am attempting to extract adjacency matrix of that graph
Jan
Jan el 24 de Feb. de 2018
@Mohammed: Then creating an image and parsing it by the very complicated methods John BG suggest is rather indirect and a waste of time.
The actual question is: "How can I create a Adjacency Matrix when I have the X and Y coordinates?"
Mohammad Bhat
Mohammad Bhat el 24 de Feb. de 2018
Yeah, actually I am making a grid of (5 x 5) of 100 x 100 binary image...from each 5 x 5 I am computing centre of mass of x and y coordinates respectively...and the center of mass represents node of a graph...grid which contains no information of original image is represented by NaN...the CXX and CYY are actually center-of-mass of 5 x 5 grids...to obtain graph I am plotting
plot(CYY,CXX,'k*');
after that I should get Adjacency matrix that is why I am following such a lengthy process I am getting by following the below procedure...
axis off % get rid of axes
F = getframe(gcf);
[xF, map] = frame2im(F);
Bpl = im2bw(xF,map,th); % binaraise the image 'th'= 0.839
baseFileName=sprintf('%d.jpg',il);
Bpl=flipud(Bpl); % flips the image upside down
AdjMat=Bpl;
folder1=sprintf('%s',tarGetFolder);
imwrite(Bpl,[folder1,baseFileName]);
clf('reset');
Mohammad Bhat
Mohammad Bhat el 24 de Feb. de 2018
My question is now could it be done in easier steps...I mean How can I get Adjacency matrix from CXX and CYY
Jan
Jan el 24 de Feb. de 2018
Editada: Jan el 24 de Feb. de 2018
As far as I see, you have X and Y values and John BG showed how to draw them in a picture and get them by image registration, such that all you get are the X and Y values with some noise. I do not see, how this helps you.
But you have accepted this very complicated and indirect method using image processing already. This means, that the problem is solved. Threads with an accepted answer get less attention, because the readers care more about open problems.
Stephen23
Stephen23 el 24 de Feb. de 2018
Editada: Stephen23 el 25 de Feb. de 2018
"My question is now could it be done in easier steps..."
You have accepted an answer with the most indirect and complex way of accessing data that you already have. You wrote that "I have CXX and CYY....", in which case why not just calculate the adjacency matrix directly? Converting to a bitmap image and performing some image processing is a total waste of time and pointlessly complex when you have that data already.
"I mean How can I get Adjacency matrix from CXX and CYY"
It would have been a lot simpler if you has asked this in your question.
See Jan Simon's answer: it will actually help you. You should accept the answer that helps you most to solve your problem.
John BG
John BG el 25 de Feb. de 2018
Editada: John BG el 25 de Feb. de 2018
Mohammad
you asked:
Now I want to get those plotted values marked '*' in plot in an matrix,
You didn't have CXX CYY values, you got CXX CYY, and now you would like to further process the obtained data.
Would you please be so kind to start another question regarding whatever processing you would to do once CXX CYY available?
Regards
John BG
Jan
Jan el 25 de Feb. de 2018
Editada: Jan el 25 de Feb. de 2018
@John BG: Mohammad wrote:
I plotted a CYY and CXX having certain values
and
I have computed CXX and CYY by some croteria aftet ploting I am
getting desired figure or graph , but I needed Adjacencey matrix.
Your interpretation:
You didn't have CXX CYY values, ...
is off-track. Mohammad did not ask for obtaining X and Y coordinates from a the diagram, because their values are known already. All he needs is the adjacency matrix, as he has explained clearly and repeatedly.
John BG
John BG el 25 de Feb. de 2018
the sentence
I have computed CXX and CYY by some croteria aftet ploting I am getting desired figure or graph
means that the points are probably on a piece of paper, or scanned file.
Mohammad has the points, on the map, paper, but want the MATLAB matrix. HEADLINE OF THE QUESTION:
How to get matrix from plot ?
Why would any one be asking for the matrix if the matrix is already available, precisely to use the MATLAB function plot?
I don't know you, but I am not aware of any plotting command, in MATLAB or for the case in any programming language, plotting that guesses data to plot When No Such Data Has Been Yet Supplied.
For the record, to me those green stars look like street lamp posts along roads, the positions have probably been marked with a marker on paper and now, obviously, further processing is needed, and MATLAB is the perfect tool, but Mohammad asks the question, because?
How to get matrix from plot ?
Ist es jetz klar?
Viele Gruße
Jan
Jan el 25 de Feb. de 2018
Editada: Jan el 26 de Feb. de 2018
@John BG: See Mohammad's refined question, where he explains again, that he has the CXX and CYY coordinates, and whats the adjacency matrix: https://www.mathworks.com/matlabcentral/answers/384657-how-to-form-adjacency-matrix-from-cxx-and-cyy-where-cxx-and-cyy-contains-collectively-some-informati#comment_539237.
Your answer shows how to digitize the plot, but this does not help in any way to get the adjacency matrix for the available coordinates.

Iniciar sesión para comentar.

Más respuestas (1)

Jan
Jan el 24 de Feb. de 2018

1 voto

1 comentario

Mohammad Bhat
Mohammad Bhat el 25 de Feb. de 2018
Editada: John Kelly el 26 de Feb. de 2018
No help, I have started new question see: Link to the new question

Iniciar sesión para comentar.

Categorías

Preguntada:

el 23 de Feb. de 2018

Editada:

Jan
el 26 de Feb. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by