简体   繁体   English

在 python 上替换我的图像数组中的零元素

[英]Replacing zero elements in my image array on python

I am training my model with several images.我正在用几张图像训练我的 model。

When training my model I realized that I could increase my accuracy by replacing the zero elements in my image array with other values and so I replaced them with the median value of my image as shown with the following code.在训练我的 model 时,我意识到可以通过将图像数组中的零元素替换为其他值来提高准确性,因此我将它们替换为图像的中值,如下面的代码所示。

import cv2
import imutils
import numpy as np

r_val_all = np.zeros((2000,112,112))
for r in range(len(r_val)): 
    #LOAD IMAGES
    r_image_v = cv2.imread(r_val[r])
    r_gray_v = cv2.cvtColor(r_image_v, cv2.COLOR_BGR2GRAY)
    r_gray_v = imutils.resize(r_gray_v, width=112, height=112)
    n = np.median(r_gray_v[r_gray_v > 0])
    r_gray_v[r_gray_v == 0] = n
    r_val_all[r,:,:] = r_gray_v

The accuracy did improve however it is not quite there yet.准确性确实有所提高,但还没有完全达到。

What I actually require is something where the zero elements are replaced with a continuation of the pre-existent array values.我真正需要的是将零元素替换为预先存在的数组值的延续。

However I was not sure how to tackle such a problem are there any tools that perform the operation I require?但是我不知道如何解决这样的问题是否有任何工具可以执行我需要的操作?

I used the second answer from the link, tell me if this is close to what you want, because it appeared to be what you wanted.我使用了链接中的第二个答案,告诉我这是否接近您想要的,因为它似乎是您想要的。

Creating one sample image and center it, so it's somewhat close to your first example image.创建一个示例图像并将其居中,因此它有点接近您的第一个示例图像。

import numpy as np
import matplotlib.pyplot as plt
image = np.zeros((100, 100))
center_noise = np.random.normal(loc=10, size=(50, 50))
image[25:75, 25:75] = center_noise
plt.imshow(image, cmap='gray')

输出

Inspired by rr_gray = np.where(rr_gray==0, np.nan, rr_gray) #convert zero elements to nan in your code, I'm replacing the zeros with NaN .rr_gray = np.where(rr_gray==0, np.nan, rr_gray) #convert zero elements to nan在您的代码中的启发,我用NaN替换了零。

image_centered = np.where(image == 0, np.nan, image)
plt.imshow(image_centered, cmap='gray')

输出

Now I used the function in the second answer of the link, fill .现在我在链接的第二个答案中使用了 function, fill .

test = fill(image_centered)
plt.imshow(test, cmap='gray')

This is the result这是结果

有填充

I'm sorry I can't help you more.对不起,我不能帮助你更多。 I wish I could, I'm just not very well versed in image processing.我希望我能,我只是不太精通图像处理。 I looked at your code and couldn't figure out why it's not working, sorry.我查看了您的代码,但无法弄清楚为什么它不起作用,抱歉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM