0

I'm trying create below kernel an image(but without any libraries, only numpy), I read how to do it, but I'm new in this field. If you help me i would be happy.

These below codes give me right result for 3x3 matrix.

How can I make to adapt my codes for 487x487 matrix?

I will apply below image. This image 487x487. enter image description here

At first I created the image as matrix with asarray method;

picMatrix = np.asarray(pic)

After that i make its a copy;

picMatrixCopy = picMatrix.copy()

And for tour into matrix a 3x3 zeros matrix;

rep = np.zeros((3,3))

And the kernel (1/25);

kernel = [[-1,0,1],
          [-1,0,1],
          [-1,0,1]]

And for loop(it's not exactly, i will do other math processing in this for loop);

for i in range(3):
    for j in range(3):
        rep[i][j] = kernel[i][j] + picMatrixCopy [i][j]

  • Actually not exactly, how can I shift the 3x3 matrix inside my 487x487 matrix(one by one to the right) – Mustafa Tokat Mar 12 '23 at 12:35
  • You could use a sliding window view: https://numpy.org/doc/stable/reference/generated/numpy.lib.stride_tricks.sliding_window_view.html , but as the notes mention, this is often suboptimal. I do think it would make your numpy code quite clean and loop-less – Chrysophylaxs Mar 12 '23 at 13:10
  • 1
    What do you mean with “apply”? In your code, you are just adding the 3x3 image and the 3x3 kernel together. All of you code together is the same as `rep = kernel + picMatrix`. There is no way to extend this operation to a larger `picMatrix`. – Cris Luengo Mar 12 '23 at 14:48
  • @CrisLuengo I meant I need to seperate to the image, and need to apply each 3x3 matrix the kernel. – Mustafa Tokat Mar 12 '23 at 16:38
  • So you want to split the image into 3x3 blocks and add your kernel to each? What would that accomplish? Are you sure you understand your task correctly? The kernel looks like a convolution kernel, a convolution with that kernel would approximate the derivative operator. But a convolution is very different from what you are doing. Are you sure it is not a convolution that you need to implement? – Cris Luengo Mar 12 '23 at 16:45
  • @CrisLuengo Yes, exactly... I'm so sorry because I couldn't tell you.. – Mustafa Tokat Mar 12 '23 at 16:50

0 Answers0