Wie vereinfache ich den Erzeugungsprozess dieser booleschen Bilder?Python

Python-Programme
Anonymous
 Wie vereinfache ich den Erzeugungsprozess dieser booleschen Bilder?

Post by Anonymous »

Ich habe Code geschrieben, der Thue-Morse-Sequenz generiert, seine Ausgabe ist ein numpiges 2D-Array, das nur 0s und 1s enthält, die Höhe des Arrays 2 n und die Breite n. Insbesondere wird jedes Zwischenergebnis als Spalte in der endgültigen Ausgabe aufbewahrt, wobei die Elemente so wiederholt werden, dass jede Spalte gleich Länge ist. 00111100, und schließlich ist die letzte Spalte 01101001, sodass die Ausgabe: < /p>
lautet

Code: Select all

array([[0, 0, 0],
[0, 0, 1],
[0, 1, 1],
[0, 1, 0],
[1, 1, 1],
[1, 1, 0],
[1, 0, 0],
[1, 0, 1]], dtype=uint8)
Jetzt ist die erste Art von Bild, die ich erzeugen möchte, einfach. Wir wiederholen jede Spalte 2 n - 1 - i Zeiten, so dass jeder Lauf von 1s zu einem Rechteck wird, dessen Höhe die Länge des Laufs ist, und in jeder nachfolgenden Säule sind die Rechtecke in der Breite der Breite der Breite der Breite des Rechtecks. Abbabaab Beispiel für ein solches Bild:

und die zweite Art von Bild, die ich generieren möchte: Die BRAUT wird das BRACTAL -Squarieren und die Politik konvertieren. /> 7-Bit Abbabaab Fractal Polar: < /p>
< /p>

import cv2
import numpy as np
from enum import Enum
from PIL import Image
from typing import Generator, List, Literal

def validate(n: int) -> None:
if not (n and isinstance(n, int)):
raise ValueError(f"Argument {n} is not a valid bit width")

def abba_codes(n: int) -> np.ndarray:
validate(n)
power = 1 > 1] * 2, half, 16)
< /code>
Leistung < /h2>

Code: Select all

In [2]: %timeit abba_square_img(10, 1024)
10.3 ms ± 715 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [3]: %timeit abba_polar(10, 1024)
27.2 ms ± 5.41 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
Sie sehen, ich musste Pil und cv2 mischen, da nur CV2 Polar -Koordinaten -Transformation anbietet, und nur Pil lässt ich ohne Interpolation ohne Interpolation.

Code: Select all

In [30]: cv2.imwrite('D:/garbage.png', cv2.resize(abba_squares(4), (1024, 1024), cv2.INTER_NEAREST))
Out[30]: True

In [31]: cv2.imwrite('D:/garbage1.png', cv2.resize(abba_squares(4), (1024, 1024), cv2.INTER_NEAREST_EXACT))
Out[31]: True

In [32]: cv2.imwrite('D:/garbage2.png', cv2.resize(abba_squares(4), (1024, 1024), cv2.INTER_AREA))
Out[32]: True

Egal welchen Interpolationsmodus ich versuche, es gibt immer ein blurriges Bild. Ich möchte, dass die Kanten scharf sind und alles rechtecke bleibt. Ich musste also Pil verwenden, um die Bilder zu ändern. Natürlich kann ich eine zweistufige Stufe verwenden, um die Pixel mit Ergebnis [i*n: i*n+n, j*n: j*n+n] = img [i, j] < /code> zu übertragen, aber das wäre langsam. n < /code> größer als 14 bis bool_squares < /code> Es hängt nur. Diese Frage befasst sich immer noch mit zwei Arten von Bildern, dh zwei Dinge, 1, ich weiß immer noch nicht, wie man Rechtecke in einem Array mit einer vollständig vektorisierten Art und Weise effizient füllt, und ich muss immer noch die fraktalen Quadrate erzeugen, um zuerst zuerst die Polarbild zu generieren. Rechtecke und verwendeten diese Rechtecke, um das Array zu füllen: < /p>

Code: Select all

def find_transitions(line: np.ndarray) -> np.ndarray:
if not line.size:
return np.array([])

return np.concatenate(
[
np.array([0] if line[0] else [], dtype=np.uint64),
((line[1:] != line[:-1]).nonzero()[0] + 1).astype(np.uint64),
np.array([line.size] if line[-1] else [], dtype=np.uint64),
]
)

def fractal_squares_helper(arr: np.ndarray, n: int, scale: int) -> List[np.ndarray]:
x_starts = []
x_ends = []
y_starts = []
y_ends = []
widths = np.concatenate([[0], ((1 > 1
y_starts.append(line[::2])
y_ends.append(line[1::2])
x_starts.append(np.tile([start], half))
x_ends.append(np.tile([end], half))

return [np.concatenate(i) for i in (x_starts, x_ends, y_starts, y_ends)]

def fill_rectangles(
length: int,
x_starts: np.ndarray,
x_ends: np.ndarray,
y_starts: np.ndarray,
y_ends: np.ndarray,
) -> np.ndarray:
img = np.full((length, length), 255, dtype=np.uint8)
x = np.arange(length)
y = x[:, None]
mask = (
(y >= y_starts[:, None, None])
& (y < y_ends[:, None, None])
& (x >= x_starts[:, None, None])
& (x < x_ends[:, None, None])
)
img[mask.any(axis=0)] = 0
return img

def fill_rectangles1(
length: int,
x_starts: np.ndarray,
x_ends: np.ndarray,
y_starts: np.ndarray,
y_ends: np.ndarray,
) -> np.ndarray:
img = np.full((length, length), 255, dtype=np.uint8)
for x0, x1, y0, y1 in zip(x_starts, x_ends, y_starts, y_ends):
img[y0:y1, x0:x1] = 0

return img

def fractal_squares(n: int, length: int, func: bool) -> np.ndarray:
arr = abba_codes(n)
scale, mod = divmod(length, total := 1 
In [4]: %timeit fractal_squares(10, 1024, 0)
590 ms ± 19.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [5]: %timeit fractal_squares(10, 1024, 1)
1.65 ms ± 56.6 μs per loop (mean ± std. dev.  of 7 runs, 1,000 loops each)
< /code>
The first method I used to fill the rectangles is completely vectorized, but it is very slow and memory consuming, it is the only way I could make it work.
The for loop based method is much faster, but it isn't completely vectorized, I want to vectorize it completely, to do away with the loop.
Now the polar images can be generated similarly, instead of filling Cartesian rectangles, we fill "polar rectangles", I have calculated the coordinates, but I cannot fill the rectangles:
def rectangularize(y: np.ndarray, x: np.ndarray) -> np.ndarray:
l = y.shape[0]
h = l // 2
return np.stack([np.tile(y, (2, 1)).T.flatten(), np.tile(x, l)]).T.reshape(
(h, 4, 2)
)

def radial_rectangles(n: int, length: int) -> np.ndarray:
arr = abba_codes(n)
radii = np.concatenate([[0], (length >> np.arange(1, n + 1)).cumsum()])
rectangles = []
total = 1 
In [6]: radial_rectangles(4, 1024)
Out[6]:
array([[[3.14159265e+00, 0.00000000e+00],
[3.14159265e+00, 5.12000000e+02],
[6.28318531e+00, 0.00000000e+00],
[6.28318531e+00, 5.12000000e+02]],

[[1.57079633e+00, 5.12000000e+02],
[1.57079633e+00, 7.68000000e+02],
[4.71238898e+00, 5.12000000e+02],
[4.71238898e+00, 7.68000000e+02]],

[[7.85398163e-01, 7.68000000e+02],
[7.85398163e-01, 8.96000000e+02],
[2.35619449e+00, 7.68000000e+02],
[2.35619449e+00, 8.96000000e+02]],

[[3.14159265e+00, 7.68000000e+02],
[3.14159265e+00, 8.96000000e+02],
[3.92699082e+00, 7.68000000e+02],
[3.92699082e+00, 8.96000000e+02]],

[[5.49778714e+00, 7.68000000e+02],
[5.49778714e+00, 8.96000000e+02],
[6.28318531e+00, 7.68000000e+02],
[6.28318531e+00, 8.96000000e+02]],

[[3.92699082e-01, 8.96000000e+02],
[3.92699082e-01, 9.60000000e+02],
[1.17809725e+00, 8.96000000e+02],
[1.17809725e+00, 9.60000000e+02]],

[[1.57079633e+00, 8.96000000e+02],
[1.57079633e+00, 9.60000000e+02],
[1.96349541e+00, 8.96000000e+02],
[1.96349541e+00, 9.60000000e+02]],

[[2.74889357e+00, 8.96000000e+02],
[2.74889357e+00, 9.60000000e+02],
[3.53429174e+00, 8.96000000e+02],
[3.53429174e+00, 9.60000000e+02]],

[[4.31968990e+00, 8.96000000e+02],
[4.31968990e+00, 9.60000000e+02],
[4.71238898e+00, 8.96000000e+02],
[4.71238898e+00, 9.60000000e+02]],

[[5.10508806e+00, 8.96000000e+02],
[5.10508806e+00, 9.60000000e+02],
[5.89048623e+00, 8.96000000e+02],
[5.89048623e+00, 9.60000000e+02]]])
< /code>
The output is of the shape (n, 4, 2)
, jeweils (4, 2) Form ist ein "radiales Rechteck". Das erste Element der innersten Paare ist der Winkel aus der X-Achse, gemessen in Radians, der Radius. /> Was ist eine effizientere Möglichkeit, Rechtecke zu füllen, und wie kann ich "radiale Rechtecke" füllen? < /p>

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post