Getting all the pixel coordinates of a vector inside an image

I have an intensity / grayscale image and I selected a pixel inside this image. I want to send vectors starting from this pixel in all directions / angles, and I want to sum all the intensities of pixels affecting one vector for all vectors.

After this step, I would like to plot a histogram with intensities on one axis and an angle on the other axis. I think I can take this last step myself, but I donโ€™t know how to create these vectors inside my image in grayscale and how to get the coordinates of the pixels that the vector touches.

I previously did this in C ++, which required a lot of code. I am sure that this can be done with less effort in MATLAB, but I am completely new to MATLAB, so any help would be appreciated since I did not find anything useful in the documentation.

+4
source share
2 answers

This may not be the best way to solve it, but you can do it using a little algebra, like ... We know the Point-Slope formula of a line passing through a point (a, b) with theta angle:

y = tan(theta) * (xa) + b 

Therefore, a simple idea is to calculate the intersection of this line with y = const for all const and read the intensity values โ€‹โ€‹at the intersection. You would repeat this for all angles ...
Sample code to illustrate the concept:

 %% input point = [128 128]; % pixel location I = imread('cameraman.tif'); % sample grayscale image %% calculations [rc] = size(I); angles = linspace(0, 2*pi, 4) + rand; angles(end) = []; clr = lines( length(angles) ); % get some colors figure(1), imshow(I), hold on figure(2), hold on for i=1:length(angles) % line equation f = @(x) tan(angles(i))*(x-point(1)) + point(2); % get intensities along line x = 1:c; y = round(f(x)); idx = ( y<1 | y>r ); % indices of outside intersections vals = diag(I(x(~idx), y(~idx))); figure(1), plot(x, y, 'Color', clr(i,:)) % plot line figure(2), plot(vals, 'Color', clr(i,:)) % plot profile end hold off 
+4
source

This example will be similar to Amro , but this is a slightly different implementation that should work for an arbitrary coordinate system assigned to the image ...

Suppose you have matrices with regular x and y coordinates that match the size of your image, so that the pixel coordinates (i,j) are given by the symbol (x(i,j),y(i,j)) . As an example, I will create a 5-by-5 โ€‹โ€‹sample of sets of integer coordinates using MESHGRID :

 >> [xGrid,yGrid] = meshgrid(1:5) xGrid = 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 yGrid = 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5 

Next, we can determine the line y = m*(x - a) + b passing through the coordinate system, selecting some values โ€‹โ€‹for the constants and calculating y using the x coordinates of the grid:

 >> a = 0; >> b = 1; >> m = rand m = 0.5469 >> y = m.*(xGrid(1,:)-a)+b y = 1.5469 2.0938 2.6406 3.1875 3.7344 

Finally, we find y points in the grid that differ from the points calculated above less than the grid size:

 >> index = abs(yGrid-repmat(y,size(yGrid,1),1)) <= yGrid(2,1)-yGrid(1,1) index = 1 0 0 0 0 1 1 1 0 0 0 1 1 1 1 0 0 0 1 1 0 0 0 0 0 

and use this index matrix to get the x and y coordinates for the pixels intersected by the line:

 >> xCrossed = xGrid(index); >> yCrossed = yGrid(index); 
+3
source

All Articles