How to extract and/or average diagonal pixels in an image?
Show older comments
Looking for some advice here. I have a 2D image which contains a slightly tilted (theta ~ 3 degrees) sinusoidal pattern (e.g. monochromatic interference fringes), and would like to take the mean across each diagonal of the fringe pattern, for a large range in one of the image dimensions. Is there a way to extract each diagonal? And maybe scan the diagonal extraction across the image? I've attached a simple concept and code to generate the pattern. Thanks in advance.

% Tilted Sinusoidal Pattern
N = 256;
x = 1:N; y = x;
[x0,y0] = meshgrid(x,y); % Set image grid
theta = pi/2 - 3*pi/180; % angle of tilt in pattern
I = 1 + cos(2*pi*(x0*cos(theta)+y0*sin(theta))); % Generate interference pattern
imagesc(I),
colormap(gray),
title('Simulated Fringe Pattern')
1 Comment
Walter Roberson
on 24 Mar 2016
How do you define "diagonal" for this purpose?
Answers (1)
1.- Acquiring image
A=imread('fringes1.jpg')
figure(1);imshow(A)

what you call a diagonal is a cross section. You want a horizontal cross section, but it can be defined for any 2 points:
p=ginput(2)
check
figure(1);hold all; plot(p(:,1),p(:,2),'ro')

2.- find the amount of pixels between points p(1,:) and p(2,:) in straight line
N=max(abs(p(1,1)-p(2,1)),abs(p(2,1)-p(2,2)))
3.- cross section points:
Lx=linspace(p(1,1),p(2,1),N)
Ly=linspace(p(1,2),p(2,2),N)
check
figure(1);hold all; plot(Lx,Ly,'r')
4.- extract the image values across the cross section line:
indexing images requires inputs of type uint
Lx2=uint64(floor(Lx))
Ly2=uint64(floor(Ly))
CS1_R=A(sub2ind(size(A(:,:,1)),Lx2,Ly2))
CS1_G=A(sub2ind(size(A(:,:,2)),Lx2,Ly2))
CS1_B=A(sub2ind(size(A(:,:,3)),Lx2,Ly2))
nCS1=[1:length(CS1_R)]
and the result are 3 cross sections, one each primary color
figure(3);subplot(3,1,1);plot(nCS1,CS1_R,'r')
subplot(3,1,2);plot(nCS1,CS1_G,'g')
subplot(3,1,3);plot(nCS1,CS1_B,'b')

To get horizontal cross sections set y constant.
If you find this answer of any help solving your question, please click on the thumbs-up vote link,
thanks in advance
John
Categories
Find more on Object Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!