Numpy array not copy - python

I am working with numpy and images. I have a big image which i want to process bit by bit.
So I want to create a reference to the original image, do something with it and move on. But when I change something in the frame the change does not transfer to the original, which is the opposite of everything I read online
for example here : https://stackoverflow.com/a/53939444/11333604 if you don't use copy if you change one array the other changes. That does NOT HAPPEN to me
here is some pseudo code that demonstrates my problem
import numpy as np
#create a "big" imgae total black
matrix = np.full((5,5),255,np.uint8)
# the frame that i want to process
h = 3
w = 3
sub = matrix[:h, :w]
#mask to make everything 0
mask = np.zeros((h,w),np.uint8)
sub = sub & mask
#also change something radom so i know the problem is not the & or the mask
sub[0][0] = 12
#sub is changes
print(sub)
#matrix is not
print(matrix)
output
[[12 0 0]
[ 0 0 0]
[ 0 0 0]]
[[255 255 255 255 255]
[255 255 255 255 255]
[255 255 255 255 255]
[255 255 255 255 255]
[255 255 255 255 255]]
I suspect it has something to do with my arrays being 2d but i cant think of how

That's because in this line
sub = sub & mask
you're making sub to "look" at some new array formed with sub & mask and it loses its connection to matrix. If you do this in-place instead
sub &= mask
then matrix will be affected too:
>>> matrix
array([[ 12, 0, 0, 255, 255],
[ 0, 0, 0, 255, 255],
[ 0, 0, 0, 255, 255],
[255, 255, 255, 255, 255],
[255, 255, 255, 255, 255]], dtype=uint8)

Related

How get unique pixels from 2d numpy array?

I have 2d array with rgb pixel data (2 row with 3 pixel in a row).
[[[255, 255, 255],[3, 0, 2],[255, 255, 255]],[[255, 255, 255],[3, 0, 2],[255, 255, 255]]]
How can I get unique pixel? I want to get
[[255, 255, 255], [3, 0, 2]]
I am trying to use np.unique and np.transpose with np.reshape but I wasn't able to get the desired result.
Reshape the array to 2D and then use np.unique with axis=0
arr = np.array([[[255, 255, 255],[3, 0, 2],[255, 255, 255]],[[255, 255, 255],[3, 0, 2],[255, 255, 255]]])
shape = arr.shape
arr = arr.reshape((shape[0] * shape[1], shape[2]))
print(np.unique(arr, axis=0))
Output
[[ 3 0 2]
[255 255 255]]
How about this?
import itertools
np.unique(np.array(list(itertools.chain(*arr))), axis=0)
array([[ 3, 0, 2],
[255, 255, 255]])

Finding perimeter coordinates of a mask

[[ 0, 0, 0, 0, 255, 0, 0, 0, 0],
[ 0, 0, 255, 255, 255, 255, 255, 0, 0],
[ 0, 255, 255, 255, 255, 255, 255, 255, 0],
[ 0, 255, 255, 255, 255, 255, 255, 255, 0],
[255, 255, 255, 255, 255, 255, 255, 255, 255],
[ 0, 255, 255, 255, 255, 255, 255, 255, 0],
[ 0, 255, 255, 255, 255, 255, 255, 255, 0],
[ 0, 0, 255, 255, 255, 255, 255, 0, 0],
[ 0, 0, 0, 0, 255, 0, 0, 0, 0]]
I have a mask array like the one above. I would like to get the x and y coordinates belonging to the perimeter of the mask. The perimeter points are the ones shown in the array below:
[[ 0, 0, 0, 0, 255, 0, 0, 0, 0],
[ 0, 0, 255, 255, 0, 255, 255, 0, 0],
[ 0, 255, 0, 0, 0, 0, 0, 255, 0],
[ 0, 255, 0, 0, 0, 0, 0, 255, 0],
[255, 0, 0, 0, 0, 0, 0, 0, 255],
[ 0, 255, 0, 0, 0, 0, 0, 255, 0],
[ 0, 255, 0, 0, 0, 0, 0, 255, 0],
[ 0, 0, 255, 255, 0, 255, 255, 0, 0],
[ 0, 0, 0, 0, 255, 0, 0, 0, 0]]
In the array above, I could just use numpy.nonzero() but I was unable to apply this logic to the original array because it returns a tuple of arrays each containing all the x or y values of all non zero elements without partitioning by row.
I wrote the code below which works but seems inefficient:
height = mask.shape[0]
width = mask.shape[1]
y_coords = []
x_coords = []
for y in range(1,height-1,1):
for x in range(0,width-1,1):
val = mask[y,x]
prev_val = mask[y,(x-1)]
next_val = mask[y, (x+1)]
top_val = mask[y-1, x]
bot_val = mask[y+1, x]
if (val != 0 and prev_val == 0) or (val != 0 and next_val == 0) or (val != 0 and top_val == 0) or (val != 0 and bot_val == 0):
y_coords.append(y)
x_coords.append(x)
I am new to python and would like to learn a better way to do this. Perhaps using Numpy?
I played a bit with your problem and found a solution and I realized you could use convolutions to count the number of neighboring 255s for each cell, and then perform a filtering of points based on the appropriate values of neighbors.
I am giving a detailed explanation below, although one part was trial and error and you could potentially skip it and get directly to the code if you understand that convolutions can count neighbors in binary images.
First observation: When does a point belong to the perimeter of the mask?
Well, that point has to have a value of 255 and "around" it, there must be at least one (and possibly more) 0.
Next: What is the definition of "around"?
We could consider all four cardinal (i.e. North, East, South, West) neighbors. In this case, a point of the perimeter must have at least one cardinal neighbor which is 0.
You have already done that, and truly, I cannot thing of a faster way by this definition.
What if we extended the definition of "around"?
Now, let's consider the neighbors of a point at (i,j) all points along an N x N square centered on (i,j). I.e. all points (x,y) such that i-N/2 <= x <= i+N/2 and j-N/2 <= y <= j+N/2 (where N is odd and ignoring out of bounds for the moment).
This is more useful from a performance point of view, because the operation of sliding "windows" along the points of 2D arrays is called a "convolution" operation. There are built in functions to perform such operations on numpy arrays really fast. The scipy.ndimage.convolve works great.
I won't attempt to fully explain convolutions here (the internet is ful of nice visuals), but the main idea is that the convolution essentially replaces the value of each cell with the weighted sum of the values of all its neighboring cells. Depending on what weight matrix, (or kernel) you specify, the convolution does different things.
Now, if your mask was 1s and 0s, to count the number of neighboring ones around a cell, you would need a kernel matrix of 1s everywhere (since the weighted sum will simply add the original ones of your mask and cancel the 0s). So we will scale the values from [0, 255] to [0,1].
Great, we know how to quickly count the neighbors of a point within an area, but the two questions are
What area size should we choose?
How many neighbors do the points in the perimeter have, now that we are including diagonal and more faraway neighbors?
I suppose there is an explicit answer to that, but I did some trial and error. It turns out, we need N=5, at which case the number of neighbors being one for each point in the original mask is the following:
[[ 3 5 8 10 11 10 8 5 3]
[ 5 8 12 15 16 15 12 8 5]
[ 8 12 17 20 21 20 17 12 8]
[10 15 20 24 25 24 20 15 10]
[11 16 21 25 25 25 21 16 11]
[10 15 20 24 25 24 20 15 10]
[ 8 12 17 20 21 20 17 12 8]
[ 5 8 12 15 16 15 12 8 5]
[ 3 5 8 10 11 10 8 5 3]]
Comparing that matrix with your original mask, the points on the perimeter are the ones having values between 11 and 15 (inclusive) [1]. So we simply filter out the rest using np.where().
A final caveat: We need to explicilty say to the convolve function how to treat points near the edges, where an N x N window won't fit. In those cases, we tell it to treat out of bounds values as 0s.
The full code is following:
from scipy import ndimage as ndi
mask //= 255
kernel = np.ones((5,5))
C = ndi.convolve(mask, kernel, mode='constant', cval=0)
#print(C) # The C matrix contains the number of neighbors for each cell.
outer = np.where( (C>=11) & (C<=15 ), 255, 0)
print(outer)
[[ 0 0 0 0 255 0 0 0 0]
[ 0 0 255 255 0 255 255 0 0]
[ 0 255 0 0 0 0 0 255 0]
[ 0 255 0 0 0 0 0 255 0]
[255 0 0 0 0 0 0 0 255]
[ 0 255 0 0 0 0 0 255 0]
[ 0 255 0 0 0 0 0 255 0]
[ 0 0 255 255 0 255 255 0 0]
[ 0 0 0 0 255 0 0 0 0]]
[1] Note that we are also counting the point itself as one of its own neighbors. That's alright.
I think this would work, I edit my answer based on my new understanding of the problem you want the outer pixel of 255 circle
What I did here is getting all the axis of where pixels are 255 items (print it to see) and then selecting the first occurrence and last occurrence that's it easy
result=np.where(pixel==255)
items=list(zip(result[0],result[1]))
unique=[]
perimiter=[]
for index in range(len(items)-1):
if items[index][0]!=items[index+1][0] or items[index][0] not in unique:
unique.append(items[index][0])
perimiter.append(items[index])
perimiter.append(items[-1])
Output
[(0, 4),
(1, 2),
(1, 6),
(2, 1),
(2, 7),
(3, 1),
(3, 7),
(4, 0),
(4, 8),
(5, 1),
(5, 7),
(6, 1),
(6, 7),
(7, 2),
(7, 6),
(8, 4)]

How to create a discrete RGB colourmap with N colours using numpy

I am trying to create a really simple colourmap using RGB triples. That has a discrete number of colours.
Here is the shape that I am trying to make it follow:
I know it is probably not a true RGB spectrum, but i wanted it to be as easy to compute as possible.
I want to be able to generate an array of N 8-bit RGB tuples that are evenly spaced along the line.
For easy instances where all of the points in the image above fall on colours, it is easy to generate using numpy.linspace() using the following code:
import numpy as np
# number of discrete colours
N = 9
# generate R array
R = np.zeros(N).astype(np.uint8)
R[:int(N/4)] = 255
R[int(N/4):int(2*N/4)+1] = np.linspace(255,0,num=(N/4)+1,endpoint=True)
# generate G array
G = 255*np.ones(N).astype(np.uint8)
G[0:int(N/4)+1] = np.linspace(0,255,num=(N/4)+1,endpoint=True)
G[int(3*N/4):] = np.linspace(255,0,num=(N/4)+1,endpoint=True)
# generate B array
B = np.zeros(N).astype(np.uint8)
B[int(2*N/4):int(3*N/4)+1] = np.linspace(0,255,num=(N/4)+1,endpoint=True)
B[int(3*N/4)+1:] = 255
# stack arrays
RGB = np.dstack((R,G,B))[0]
This code works fine for 5 colours:
r 255 255 0 0 0
g 0 255 255 255 0
b 0 0 0 255 255
9 colours:
r 255 255 255 127 0 0 0 0 0
g 0 127 255 255 255 255 255 127 0
b 0 0 0 0 0 127 255 255 255
13 colours:
r 255 255 255 255 170 85 0 0 0 0 0 0 0
g 0 85 170 255 255 255 255 255 255 255 170 85 0
b 0 0 0 0 0 0 0 85 170 255 255 255 255
etc.. but I'm having trouble working out how to make it work for an arbitrary N number of colours because the linspace trick only works if it is going from one endpoint to another.
Can someone please help me with working out how to do it? Any ideas for how to make my code above more efficient would be great as well, I am just learning how to use numpy after putting it off for ages..
Here is one reasonably convenient method using np.clip:
def spec(N):
t = np.linspace(-510, 510, N)
return np.round(np.clip(np.stack([-t, 510-np.abs(t), t], axis=1), 0, 255)).astype(np.uint8)
It avoids the problem you describe by only relying on the only two points that are guaranteed to be on the grid for any N which are the first and last points.
Examples:
>>> spec(5)
array([[255, 0, 0],
[255, 255, 0],
[ 0, 255, 0],
[ 0, 255, 255],
[ 0, 0, 255]], dtype=uint8)
>>> spec(10)
array([[255, 0, 0],
[255, 113, 0],
[255, 227, 0],
[170, 255, 0],
[ 57, 255, 0],
[ 0, 255, 57],
[ 0, 255, 170],
[ 0, 227, 255],
[ 0, 113, 255],
[ 0, 0, 255]], dtype=uint8)
If you just want RGB at given levels, the diagram that you have posted itself serves as the answer -
R = [255] * 256
R.extend(list(reversed(range(256))))
R.extend([0] * 256)
R.extend([0] * 256)
G = list(range(256))
G.extend([255] * 256)
G.extend([255] * 256)
G.extend(list(reversed(range(256))))
B = [0] * 256
B.extend([0] * 256)
B.extend(list(range(256)))
B.extend([255] * 256)
level = 5
step = 1024 // (level -1)
print(R[::step])
print(G[::step])
print(B[::step])

Converting a NumPy array to a PIL image

I want to create a PIL image from a NumPy array. Here is my attempt:
# Create a NumPy array, which has four elements. The top-left should be pure
# red, the top-right should be pure blue, the bottom-left should be pure green,
# and the bottom-right should be yellow.
pixels = np.array([[[255, 0, 0], [0, 255, 0]], [[0, 0, 255], [255, 255, 0]]])
# Create a PIL image from the NumPy array
image = Image.fromarray(pixels, 'RGB')
# Print out the pixel values
print image.getpixel((0, 0))
print image.getpixel((0, 1))
print image.getpixel((1, 0))
print image.getpixel((1, 1))
# Save the image
image.save('image.png')
However, the print out gives the following:
(255, 0, 0)
(0, 0, 0)
(0, 0, 0)
(0, 0, 0)
And the saved image has pure red in the top-left, but all the other pixels are black. Why are these other pixels not retaining the colour I have assigned to them in the NumPy array?
The RGB mode is expecting 8-bit values, so just casting your array should fix the problem:
In [25]: image = Image.fromarray(pixels.astype('uint8'), 'RGB')
...:
...: # Print out the pixel values
...: print image.getpixel((0, 0))
...: print image.getpixel((0, 1))
...: print image.getpixel((1, 0))
...: print image.getpixel((1, 1))
...:
(255, 0, 0)
(0, 0, 255)
(0, 255, 0)
(255, 255, 0)
Your numpy array should be of the form:
[[[248 248 248] # R G B
[248 248 248]
[249 249 249]
...
[ 79 76 45]
[ 79 76 45]
[ 78 75 44]]
[[247 247 247]
[247 247 247]
[248 248 248]
...
[ 80 77 46]
[ 79 76 45]
[ 79 76 45]]
...
[[148 121 92]
[149 122 93]
[153 126 97]
...
[126 117 100]
[126 117 100]
[125 116 99]]]
Assuming you have your numpy array stored in np_arr, here is how to convert it to a pillow Image:
from PIL import Image
import numpy as np
new_im = Image.fromarray(np_arr)
To show the new image, use:
new_im.show()

Converting a PNG image to 2D array

I have a PNG file which when I convert the image to a numpy array, it is of the format that is 184 x 184 x 4. The image is 184 by 184 and each pixel is in RGBA format and hence the 3D array.
This a B&W image and the pixels are either [255, 255, 255, 255] or [0, 0, 0, 255].
I want to convert this to a 184 x 184 2D array where the pixels are now either 1 or 0, depending upon if it is [255, 255, 255, 255] or [0, 0, 0, 255].
Any ideas how to do a straightforward conversion of this.
There would be several ways to do the comparison to give us a boolean array and then, we just need to convert to int array with type conversion. So, for the comparison, one simple way would be to compare against 255 and check for ALL matches along the last axis. This would correspond to checking for [255, 255, 255, 255]. Thus, one approach would be like so -
((arr == 255).all(-1)).astype(int)
Sample run -
In [301]: arr
Out[301]:
array([[[255, 255, 255, 255],
[ 0, 0, 0, 255],
[ 0, 0, 0, 255]],
[[ 0, 0, 0, 255],
[255, 255, 255, 255],
[255, 255, 255, 255]]])
In [302]: ((arr == 255).all(-1)).astype(int)
Out[302]:
array([[1, 0, 0],
[0, 1, 1]])
If there are really only two values in the array as you say, simply scale and return one of the dimensions:
(arr[:,:,0] / 255).astype(int)

Categories

Resources