# binary matrices with directional non-zero elements

This question is in the context of image processing.

Assume I have a matrix of size 64 by 64. Assume even rows are all zero and odd rows are all one. I want to rotate the matrix d degrees. For example if I rotate it 90 degrees, then I will have a matrix with even columns equal to zero and odd columns equal to one. Is there an algorithm to do that easily? Given the matrix and d I want to rotate the elements. Matlab's imrotate won't work since after rotation I get some zeros at the corners. I know one way is to use repmat, rotate it and then crop it in the center. But that does not seem straightforward. Any smart ideas? For example, is it possible to define a formula to give the values of each element given d (and the fact that the matrix was originally 1 0 1 0...?

• Rotation for a discretized image are simple only in a few cases: typically multiples of $45°$. Also, what is your center of rotation? Depending on the precisions given, options could be proposed Apr 19, 2017 at 6:07
• center is at (0,0). I need one degree precision. So it should round the indices to the closest index to avoid a fractional index.
– jose
Apr 19, 2017 at 6:22
• Is $(0,0)$ the top-left? Rounding is likely to produce "weird slanted lines". Off the top of my head, a $1°$ precision sounds illusory for a $64\times 64$. If you tell us the kind of processing you are looking at, this could help us further Apr 19, 2017 at 6:27

If you assume that your image origins from sampling a continuous-spatial-domain 2-dimensional function, you can perform the sampling of this function with the rotated coordinates. HAve a look at this example:

N = 32  # 32 for simplicity
N2 = N/2
x, y = np.meshgrid(np.arange(N), np.arange(N))

# exemplary rotation around 5 degrees
angle = 5*np.pi/180
# perform rotation around the image center
nx = np.cos(angle)*(x-N2) - np.sin(angle)*(y-N2) + N2
ny = np.sin(angle)*(x-N2) + np.cos(angle)*(y-N2) + N2

# three functions that fulfill your constraint of even-odd 1-0
I_func1 = lambda x, y: 0.5*(1-np.cos(np.pi*y))
I_func2 = lambda x, y: (abs((y%2)-1)<=0.5).astype(float)
I_func3 = lambda x, y: (1-abs((y%2)-1)).astype(float)

# the original image
I = I_func1(x, y)

plt.figure()
t = np.linspace(0, N-1, 16*N)
t2 = np.arange(N)
plt.plot(t, I_func1(0, t))
plt.plot(t, I_func2(0, t))
plt.plot(t, I_func3(0,t))
plt.stem(t2, I_func(0, t2))
plt.xlim((0, 5))
plt.title('underlying continuous function')

plt.figure(figsize=(12,12))
plt.subplot(221)
plt.imshow(I, interpolation='none', cmap='gray')

plt.subplot(222)
plt.imshow(I_func1(nx, ny), interpolation='none', cmap='gray')
plt.title("Cos")

plt.subplot(223)
plt.imshow(I_func2(nx, ny), interpolation='none', cmap='gray')
plt.title("rect")

plt.subplot(224)
plt.imshow(I_func3(nx, ny), interpolation='none', cmap='gray')
plt.title("triang") As you see, different functions yield different rotation results. Maybe this can help you to find an explicity formula?