how to add random values (random number to specific spot ) to x-ray image with tensorflow - tensorflow

I want to predict disease and I want to try to make the image have some noise or disruption in specific spot or randomly spot is there any method or solution for it??
is there any way to add noise (random value) to image with tensorflow
I read the image and convert it to array and make a copy of it and then add to it some number is that right??
and i have noticed that when convert it the array became values of zeros and ones even it in rgb form.
i expect the some value in the array or the value in the image change to another values so when imshow (the image) notice some noise (different from guassian noise) so when the input to the model become different from the original image

I have trying this but operand didn't match between(224,224,3) and (224,224)
but when set colormode to grayscal the operand work but i didnt see that much of change in image.
,when trying replace img.size with img.height did'nt work either
img = tf.keras.preprocessing.image.load_img("/content/person1_bacteria_2.jpeg",color_mode="rgb",target_size=(256, 256))
nois_factor = 0.3
n = nois_factor * np.random.randn(*img.size)
noise_image = img + n
plt.imshow(noise_image)

Related

Trying to understand what this code is doing

I have a 2D numpy array call the_array with shape (5,10)
I would like to make sure what this piece of code is doing
h,w = the_array.shape
mask = np.ones((h,w))
mask[:int(h*0.35),:] =0 #?? what??
the_array = the_array* mask
I see that mask is an array of the same dimensions all made of 1s but what after that? (it if it any help these arrays are gonna be used as images later)
mask[:int(h*0.35), :] = 0 is nothing but an assignment which turns the first 35% of rows into zeros. So basically your mask will be 35% zeros and the rest ones. Multiply it with your image (i.e the_array * mask) will make the top part of the image completely black, like a naive image filter.

RGB to gray filter doesn't preserve the shape

I have 209 cat/noncat images and I am looking to augment my dataset. In order to do so, this is the following code I am using to convert each NumPy array of RGB values to have a grey filter. The problem is I need their dimensions to be the same for my Neural Network to work, but they happen to have different dimensions.The code:
def rgb2gray(rgb):
return np.dot(rgb[...,:3], [0.2989, 0.5870, 0.1140])
Normal Image Dimension: (64, 64, 3)
After Applying the Filter:(64,64)
I know that the missing 3 is probably the RGB Value or something,but I cannot find a way to have a "dummy" third dimension that would not affect the actual image. Can someone provide an alternative to the rgb2gray function that maintains the dimension?
The whole point of applying that greyscale filter is to reduce the number of channels from 3 (i.e. R,G and B) down to 1 (i.e. grey).
If you really, really want to get a 3-channel image that looks just the same but takes 3x as much memory, just make all 3 channels equal:
grey = np.dstack((grey, grey, grey))
def rgb2gray(rgb):
return np.dot(rgb[...,:3], [[0.2989, 0.5870, 0.1140],[0.2989, 0.5870, 0.1140],[0.2989, 0.5870, 0.1140]])

How to convert RGB heatmap back to its original value using matplotlib?

Let's say I have an image which range in value from 0 to 255.
image = np.asarray([[i for i in range(256)] for j in range(256)])
Now if I want to visualize it I usually use
plt.imshow(image, cmap="jet")
But I've googled and found nothing to convert from the visualized image back to actual image value.
My attempt is to map every point in color-space back to its original value, and it is very time consuming. Then I (hopefully) can map every pixel color back it its original space.
cmap = matplotlib.cm.get_cmap('hsv')
# Creating inverse map of cmap
cmap_map = np.zeros((256, 256, 256))
step = 1/(256**3)
for i in tqdm(np.arange(0, 1 + step, step), position=0):
cmap_map[np.round(np.array(cmap(i)[:-1])*255).astype(dtype=np.int)] = i
Is there a better method to convert? (Noted that cmap should be arbitary)

One hot encoding variable n m mode numbers

I'm doing image classification of with image labels of mode numbers m and n. I'm also varying the convnet training to take in different maximum mode numbers to test it out, before fully training on a massive data set.
Silly question but, given a label (m,n) how do I one hot encode it into an array of length n*m?
Thanks.
Edit: Yikes this is actually really simple,
modenum = %some integer
def getLabel(n,m):
array = np.zeros((modeNum,modeNum),dtype='int8')
array[n,m] = 1
label = np.ndarray.flatten(array)
return label

Reshaping numpy 3D array

I have a dataset with dimensions: (32, 32, 73257) where 32x32 are pixels of a single image.
How do I reshape it to (73257, 1024) so that every image is unrolled in a row?
So far, I did:
self.train_data = self.train_data.reshape(n_training_examples, number_of_pixels*number_of_pixels)
and it looks like I got garbage instead of normal pictures. I am assuming that reshaping was performed across wrong dimension...??
As suggested in the comments, first get every image in a column, then transpose:
self.train_data = self.train_data.reshape(-1, n_training_examples).T
The memory layout of your array will not be changed by any of these operations, so two contiguous pixels of any image will lay 73257 bytes apart (assuming a uint8 image), which may not be the best of options if you want to process your data one image at a time. You will need to time and validate this, but creating a copy of the array may prove advantageous performance-wise:
self.train_data = self.train_data.reshape(-1, n_training_examples).T.copy()