I guess this is a very basic question.
I have a graph which I created in MATLAB. This is a graph of Power (y-axis) versus Frequency (x-axis).
The range of my x-axis is from 0 to 1000. Now here is my problem. I want to draw a line from specific points on the x-axis to the graph. For example, for points 40, 400, 950.
By using set(gca, 'XTick', [40 400 950]); I am able to mark these particular points. But I want to make it more visible by drawing straight vertical lines from these points.
Any help will be greatly appreciated. Thank you.
Use plot with endpoints with the same x value and different y values. (and don't forget to use myaa to beautify the output).
x = 0:0.1:2*pi;
y = sin(x);
plot(x,y);
hold on;
plot([0.6 0.6], [-1 1], 'Color', [0.7 0.7 0.7], 'LineWidth', 2);
plot([3.6 3.6], [-1 1], 'Color', [0.7 0.7 0.7], 'LineWidth', 2);
If you do this often I would recommend you a great submission from the FileExchange:
hline and vline
Just do:
vline([40 400 950])
Read the function documentation, if you want the line to have different properties than default.
I typically do this using something like this (is powers is a row vector).
powers = randn(1,1000)+40;
plot([1;1]*[40 400 950], [[0 0 0]; [powers([40 400 950])]],'k-')
Related
I have a scatter plot on the left-hand side below, where there are lots of data points, and the figure on the right are corresponding density plot using seaborn.kdeplot(). But unfortunately since the variance of the density is so large that kdeplot fails to capture many details in other low-density area (e.g. there is basically no information about the density distribution on the top right).
Does anyone have any ways to fix this issue?
Thanks!
You can use the n_levels parameter, i.e.
f, axes = plt.subplots(1, 3, figsize=(15, 5), sharex=True, sharey=True)
x, y = np.random.normal(0, 1, (2, 1000))
axes[0].scatter(x, y, s=5, c=".1")
sns.kdeplot(x, y, n_levels=10, ax=axes[1])
sns.kdeplot(x, y, n_levels=30, ax=axes[2])
f.tight_layout()
My problem is as follows:
I have the picture of a half cylinder taken from a horizontal perspective and it has square grid lines on it, so I was wondering how can I implement in MATLAB to unwrap this half cylinder so all my grid cells become the same size? I know I will loose lots of resolution in the edge cells and a simple linear interpolation should do the trick, but I do not know how to tell MATLAB to do this. Also I know the geometrical properties of the cylinder, radius and height. Any help is greatly appreciated.
This is the approach I am using, but I am trying to find the transformation that will make the edges be same size as inner cells.
im=imread('Capture.png');
imshow(im);
impixelinfo
r = #(x) sqrt(x(:,1).^2 + x(:,2).^2);
w = #(x) atan2(x(:,2), x(:,1));
f = #(x) [sqrt(r(x)) .* cos(w(x)), sqrt(r(x)) .* sin(w(x))];
g = #(x, unused) f(x);
tform2 = maketform('custom', 2, 2, [], g, []);
im3 = imtransform(im, tform2, 'UData', [-1 1], 'VData', [-1 1], ...
'XData', [-1 1], 'YData', [-1 1]);
figure,
imshow(im3)
I think the transformation is much simpler than what you're trying to do. Take a look at the (forward) transformation to take a flat grid and wrap it around a cylinder. The coordinates along the axis of the cylinder (the y coordinates, in this case) are unchanged. If we take the range of the grid coordinates in the x direction to be [-1,1], the coordinates on the cylinder will be:
sin(x × π/2)
Since this is the forward transformation going from a grid to the cylinder, it is also the inverse transformation going from the cylinder to the grid.
f = #(x, unused) [sin(x (:, 1) * pi / 2), x(:, 2)]
tform2 = maketform('custom', 2, 2, [], f, []);
im3=imtransform(img, tform2, 'UData', [-1 1], 'VData', [-1 1], ...
'XData', [-1 1], 'YData', [-1 1]);
Result:
This still isn't perfect, primarily because the original image has borders around it that we're transforming along with the rest of the image. This could be improved by cropping the image to contain only the cylinder portion.
This code takes the coordinates of two rectangles and finds their intersection.
def rec_intersection(rect1, rect2)
x_min = [rect1[0][0], rect2[0][1]].max
x_max = [rect1[1][0], rect2[1][1]].min
y_min = [rect1[0][0], rect2[0][1]].max
y_max = [rect1[1][0], rect2[1][1]].min
return nil if ((x_max < x_min) || (y_max < y_min))
return [[x_min, y_min], [x_max, y_max]]
end
rec_intersection([[1, 1], [2, 2]],[[0, 0], [5, 5]])
I don't really understand it. Specifically I would like to know more about exactly what the coordinates mean (I know they are coordinates for the left-bottom and right-top) but can anybody elaborate more? What are they relative to? The size of the rectangle? Or it's placement? How does a rectangle with a bottom-left coordinate of [1,1] differ from one with a bottom-left of [0,0]?
Also I would like to know why in order to find the x_min, the max method is being used (and vice-versa). Any clarification is appreciated.
This is a comment, which I will delete after the OP has seen it. Here's a graph of the two rectangles, where rect1 is contained within rect2.
Earlier, where I referred to [1,1] and [2,2] as the "top-left" and "bottom-right" corners, respectively, of rect1, that should have been the "bottom-left" and "top-right" corners.
I am trying to create the following chart in Mathematica.
I would like to somehow generate this based on Ohm's law not just a raw data set.
I would like to be able to switch the resistance value to update the chart and increase the voltage in 5-V steps from 0 to 30 V
Any help would be greatly appreciated I'm totally stuck.
Try this. You will find that the line slope looks the same while the y axis scale changes as the value of r is varied. There are simple plot options you can use to change that effect.
i[v_, r_] := v/r;
Manipulate[
Plot[i[v, r], {v, 1, 5000}, PlotRange -> All], {r, 100, 50000}]
I have the following image on which i have applied bilateral filtering and subtracted it from my original image
Is it possible to outline the glass defect as shown
After apply Hough i got the following result not perfect :/
My matlab code :
im = imread('C:\Users\SUJIT\Desktop\image003.jpg');
im=rgb2gray(im);
h = fspecial('gaussian', size(im), 1.0);
g = imfilter(im, h);
im=im2double(g);
im=imadjust(im);
imgauss = imfilter(im, fspecial('gaussian',[7 7], 6),'conv');
imbi = bilateralfilter(im, [5 5], 3, 3);
imbi= im - imbi;
imshow(imbi,[]); title('Bilateral Filted Image');
I = imcrop(imbi, [30 30 450 350]);
J = imfilter(I, fspecial('gaussian', [17 17], 5), 'symmetric');
BW = edge(J, 'canny');
%# Hough Transform and show matrix
[H T R] = hough(BW);
imshow(imadjust(mat2gray(H)), [], 'XData',T, 'YData',R, ...
'InitialMagnification','fit')
xlabel('\theta (degrees)'), ylabel('\rho')
axis on, axis normal, hold on
colormap(hot), colorbar
%# detect peaks
P = houghpeaks(H, 10);
plot(T(P(:,2)), R(P(:,1)), 'gs', 'LineWidth',2);
%# detect lines and overlay on top of image
lines = houghlines(BW, T, R, P);
figure, imshow(I), hold on
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:,1), xy(:,2), 'g.-', 'LineWidth',2);
end
hold off
Please help am i doing something wrong here?
It is a bit hard to give a general answer based on only one image and no other information, but I can give a specific answer based on your sample image.
Assuming that what you want to find is the vertical blurry line in the middle of the image, this my approach. I won't go into specific implementation details, but only an outline on how I would do it.
Find the windows. There are multiple approaches to this. Some ideas are to either find the corners, or to find the rectangular structure itself. The Hough transform is a possible tool.
For each window, check if there are vertical structures in it.