0

I am trying to threshold an image based on a hardcoded value.I am doing it by assigning the original image to a variable. This variable is used for thresholding. But, when I execute this, the original image is also getting thresholded. Am I doing something wrong? Or is there any other way to do this? The code is provided below:

import numpy as np
from scipy.misc import imread
import matplotlib.pyplot as plt 
img1 = imread('4.2.04.tiff')
imgx = img1
imgx[img1>=150] = 0
plt.figure()
plt.imshow(np.uint8(img1))
plt.show()
plt.title('Original Image after thresholding')
plt.figure()
plt.imshow(np.uint8(imgx))
plt.title('Thresholded Image')

The images are provided below: Original Image

Original Image after thresholding

thresholded image Thank you.

shreyas kamath
  • 425
  • 1
  • 5
  • 14

1 Answers1

2

imgx = img1

You're basically creating a reference to the already existing variable imgx. Now imgx and img1 point to the same address.

If you want to deep copy an array do this.

img1 = numpy.array(imgx)

See this post for details.

zindarod
  • 6,328
  • 3
  • 30
  • 58
  • 1
    Also `imgx = img1.copy()` can create a deep copy and is a little more explicit, same with `numpy.copyTo(imgx, img1)`. – alkasm Sep 16 '17 at 09:07