[Python] I have some kind of bayer pattern. How to rearrange pixels of an image with optimized...

Discussão em 'Python' iniciado por Stack, Setembro 24, 2024.

  1. Stack

    Stack Membro Participativo

    I have a gray-scale image. I divided the image into blocks of size 128x128. I've implemented the following code, and it works correctly. However, it is highly time-consuming due to the use of four nested for loops. Is there a way to optimize it to achieve the same result but faster? The block size is set to 128, where blocksize[0] equals 128. The number of blocks in the row direction in the 'Image_input' is 68 (num_block_row), and in the column direction, it is 54 (num_block_col). In fact, I intended to arrange the first pixels of each block side by side. In the attached image, each block consists of four pixels, each with a distinct color. I aim to arrange the pixels with a red color side by side within each block. This process should be applied to other blocks as well.

    for t in range(blocksize[0]):

    for s in range(blocksize[0]):

    for i in range(num_block_row):

    for j in range(num_block_col):

    Image_result[(t*num_block_row) + i ,(s*num_block_col) + j] = Image_input[(i * blocksize[0]) + t, (j * blocksize[0]) + s]


    I tried to rearrange the pixels of the image, but it was very time-consuming. Is there an optimized method to do it?

    [​IMG]

    Continue reading...

Compartilhe esta Página