如何使用for循环从数组中删除点? [英] How do I remove points from an array using a for loop?

查看:98
本文介绍了如何使用for循环从数组中删除点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组numpy数组中的图像。在一些处理并应用阈值之后,我将它们变成在每个xy坐标中具有值0或1的图像。我想使用for循环和非零来将不在非零数组中的原始图像的xy坐标转为零,并将非零数组中的像素保留为原始强度。我是编程中的一个完整的菜鸟,我已经完成了这项任务。



这是我到目前为止所做的,但最后一部分不起作用:



< pre> 
import cv2
Take数据的第一个图像
image = series_copy2 [ 0 ,:,:]

图片的平均总背景
print ' 平均总背景=' + str(np.mean(image))+ ' 计数。'



背景删除阈值
threshold = 30



将阈值以下的所有像素设置为零以删除背景
image [image [ :,:]< threshold] = 0
image [image [:,]]> threshold] = 1

绘制结果以进行检查
fig = plt.figure(figsize =( 10 10 ))
ax = fig.add_subplot( 111
data = image
plt.tight_layout()
im = plt.imshow(data,interpolation = ' 最近的'

np.transpose(np.nonzero(data))

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

############### ########################这不起作用
for x in data [:,:]:
if data [data [:, :] nz_arrays]:
data [:,] = 0
########## ###################################

绘制结果以进行检查
fig = plt.figure(figsize =( 10 10 ))
ax = fig.add_subplot( 111
data = image
plt.tight_layout()
im = plt.imshow(data,interpolation = ' nearest'







我想要的代码是比较原始图像和二进制图像,并在二进制图像为零的位置放置零,并保留原始图像,因为二进制图像的位置为1.我希望这样帮助





我的尝试:



我是一个菜鸟,我在大学里面得到了这个任务

解决方案

我实际上用另一种方式解决了它。

我做了一个空数组并使用坐标将值放在新数组中。如果有人有兴趣,我发布以下代码:



< pre lang =Python>< pre> #### ###################################### $ 

import cv2
#拍摄数据的第一张图片
image = series_copy2 [0,:,]]

#图像的平均总背景
print('平均总背景= '+ str(np.mean(图片))+'计数。')



#背景去除阈值
threshold = 15



#将低于阈值的所有像素设置为零以移除背景
image [image [:,]]< threshold] = 0
image [image [:,:]> threshold] = 1

#绘制结果以检查
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]




#绘制结果以检查
fig = plt.figure (figsize =(10,10))
ax = fig.add_subplot(111)
plt.tight_layout()
im = plt.imshow(empty,interpolation ='nearest')


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:

<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

解决方案

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')


这篇关于如何使用for循环从数组中删除点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆