Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a set of images in a numpy array. After some processing and applying a threshold I turned them into images that have either value 0 or 1 in each xy coordinate. I want to use a for loop and nonzero to turn the xy coordinates of the original image that are not in the nonzero array to zero and leave the pixels in the nonzero array with their original intensity. Im a complete noob in programming and I have been given this task.

This is what I have so far but the last part doesn't work:

Python
<pre>
import cv2
# Taking the first image of the data
image = series_copy2[0,:,:]

# Mean total background of the image
print('Mean total background = ' +str(np.mean(image)) + ' counts.')



# Threshold for background removal
threshold =30



# Setting all pixels below a threshold to zero to remove the background
image[image[:,:] < threshold] = 0
image[image[:,:]>threshold]=1

# Plotting the result for checking
fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(111)
data = image
plt.tight_layout()
im = plt.imshow(data, interpolation = 'nearest')

np.transpose(np.nonzero(data))

nz_arrays=np.transpose(np.nonzero(data))

########################################this doesn't work
for x in data[:,:]:
    if data[data[:,:] not in nz_arrays]:
        data[:,:]=0
##############################################

# Plotting the result for checking
fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(111)
data = image
plt.tight_layout()
im = plt.imshow(data, interpolation = 'nearest')



What I want the code to do is to compare the original and the "binary" image and put a zero where the binary image has a zero and leave the original image as it is where the binary image has a 1. I hope this helps


What I have tried:

Im a noob and I was given this task at the university
Posted
Updated 11-Mar-19 5:27am
Comments
[no name] 11-Mar-19 10:51am    
You'll be waiting a long time for an answer. Better make yourself a flow chart, or something.

1 solution

I actually solved it in another way.
I made an empty array and used the coordinates to just put the values in the new array. If someone is interested i post the code below:

<pre lang="Python"><pre>##########################################

import cv2
# Taking the first image of the data
image = series_copy2[0,:,:]

# Mean total background of the image
print('Mean total background = ' +str(np.mean(image)) + ' counts.')



# Threshold for background removal
threshold =15



# Setting all pixels below a threshold to zero to remove the background
image[image[:,:] < threshold] = 0
image[image[:,:]>threshold]=1

# Plotting the result for checking
fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(111)
plt.tight_layout()
im = plt.imshow(image, interpolation = 'nearest')



np.transpose(np.nonzero(image))

nz_arrays=np.transpose(np.nonzero(image))

empty = np.zeros(shape=(256,256))





for i,j in nz_arrays:
    
    empty[i,j]=series[0,i,j]


    
    
# Plotting the result for checking
fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(111)
plt.tight_layout()
im = plt.imshow(empty, interpolation = 'nearest')
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900