I want to reduce the contrast of my whole dataset as an experiment. The dataset is in a NumPy array of np.array(X).reshape(-1, 28, 28, 1) I tried to use the library Albumentations, which works really well for motion blur and Gaussian noise, but I didn't find a way to reduce the contrast with that library. How can I do this?
You would need to rescale the difference to the mean value, here is an example, using this image as source. Extra code using PIL and imshow for visuals:
import numpy as np
import PIL.Image
import matplotlib.pyplot as plt
im = PIL.Image.open('a-600-600-image-of-a-building.jpg')
np_im = np.array(im)
np_mean = np.mean(np.array(im))
for factor in [1.0, 0.9, 0.5, 0.1]:
plt.figure()
plt.title("{}".format(factor))
reduced_contrast=(np_im-np_mean)*factor + np_mean
new_im = PIL.Image.fromarray(reduced_contrast)
plt.imshow(np.array(list(new_im.convert('RGBA').getdata())).reshape(new_im.height, new_im.width, 4))
plt.savefig("{}.png".format(factor))
Output:
The relevant line is reduced_contrast=(np_im-np_mean)*factor + np_mean
Related
import nibabel as nib
import numpy as np
import torch
from skimage.transform import radon,iradon
dir = "/hdd1/Data/3D_CT/train/000000098656.nii.gz"
nib_loader = nib.load(dir).get_fdata()
theta = np.linspace(0,180,360)
slices = nib_loader[150,:,:]
rt = radon(slices,theta,circle=True)
print(slices.max())
print(slices.min())
print(rt.max())
print(rt.min())**
slices.max()=1220.0
slices.min()=-1024.0
rt.max()=0.0
rt.min()=-510128.35438634525
radon transform change image scale how to fix the radon transform scale?
i want radon transform image sclae same slices image scale
I want to apply Hough Transform on stock prices (array of numbers).
I read OpenCV and scikit-image docs and examples ,but got nothing how to apply the transformation to the arrays of numbers instead of images.
I created 2D array from data. First dimension is X(simply index of data) and second dimension is close prices.
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import pywt as wt
from skimage.transform import (hough_line, hough_line_peaks,probabilistic_hough_line)
from matplotlib import cm
path = "22-31May-100Tick.csv"
df = pd.read_csv(path)
y = df.Close.values
x = np.arange(0,len(y),1)
data = []
for i in x:
a = [i,y[i]]
data.append(a)
data = np.array(data)
How is it possible to apply the transformation with OpenCV or sickit-image?
Thank you
I am trying to cross-correlate two images, and thus locate the template image on the first image, by finding the maximum correlation value.
I drew an image with some random shapes (first image), and cut out one of these shapes (template). Now, when I use scipy's correlate2d, and locate point in the correlation with maximum values, several point appear. From my knowledge, shouldn't there only be one point where the overlap is at max?
The idea behind this exercise is to take some part of an image, and then correlate that to some previous images from a database. Then I should be able to locate this part on the older images based on the maximum value of correlation.
My code looks something like this:
from matplotlib import pyplot as plt
from PIL import Image
import scipy.signal as sp
img = Image.open('test.png').convert('L')
img = np.asarray(img)
temp = Image.open('test_temp.png').convert('L')
temp = np.asarray(temp)
corr = sp.correlate2d(img, temp, boundary='symm', mode='full')
plt.imshow(corr, cmap='hot')
plt.colorbar()
coordin = np.where(corr == np.max(corr)) #Finds all coordinates where there is a maximum correlation
listOfCoordinates= list(zip(coordin[1], coordin[0]))
for i in range(len(listOfCoordinates)): #Plotting all those coordinates
plt.plot(listOfCoordinates[i][0], listOfCoordinates[i][1],'c*', markersize=5)
This yields the figure:
Cyan stars are points with max correlation value (255).
I expect there to be only one point in "corr" to have the max value of correlation, but several appear. I have tried to use different modes of correlating, but to no avail.
This is the test image I use when correlating.
This is the template, cut from the original image.
Can anyone give some insight to what I might be doing wrong here?
You are probably overflowing the numpy type uint8.
Try using:
img = np.asarray(img,dtype=np.float32)
temp = np.asarray(temp,dtype=np.float32)
Untested.
Applying
img = img - img.mean()
temp = temp - temp.mean()
before computing the 2D cross-correlation corr should give you the expected result.
Cleaning up the code, for a full example:
from imageio import imread
from matplotlib import pyplot as plt
import scipy.signal as sp
import numpy as np
img = imread('https://i.stack.imgur.com/JL2LW.png', pilmode='L')
temp = imread('https://i.stack.imgur.com/UIUzJ.png', pilmode='L')
corr = sp.correlate2d(img - img.mean(),
temp - temp.mean(),
boundary='symm',
mode='full')
# coordinates where there is a maximum correlation
max_coords = np.where(corr == np.max(corr))
plt.plot(max_coords[1], max_coords[0],'c*', markersize=5)
plt.imshow(corr, cmap='hot')
I have some pretty straight forward code to segment out a blob and then do a total least squares line fit to it, but the estimate of the line (blue) is clearly not correct. Anybody know what I did wrong?
import skimage as s
import skimage.feature as feat
from skimage import measure
#dash is the original image of the dash as a numpy array
g= dash[2662:2800,3050:3263,0]
edges = feat.canny(g/255., 3.2, .01, .45)
fill = ndimage.binary_fill_holes(edges)
t = s.img_as_ubyte(flt.sobel_h(fill))/2+s.img_as_ubyte(flt.sobel_v(fill))/2
ptpairs = np.int0(np.where(t>160))
l = measure.LineModel()
l.estimate(ptpairs.T)
xdata = np.arange(60,80)
ydata = l.predict_y(xdata)
imshow(g+edges*100)
plot(xdata,ydata)
I am trying to use do some image analysis in python (I have to use python). I need to do both a global and local histogram equalization. The global version works well however the local version, using a 7x7 footprint, gives a very poor result.
This is the global version:
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from scipy import ndimage,misc
import scipy.io as io
from scipy.misc import toimage
import numpy as n
import pylab as py
from numpy import *
mat = io.loadmat('image.mat')
image=mat['imageD']
def histeq(im,nbr_bins=256):
#get image histogram
imhist,bins = histogram(im.flatten(),nbr_bins,normed=True)
cdf = imhist.cumsum() #cumulative distribution function
cdf = 0.6 * cdf / cdf[-1] #normalize
#use linear interpolation of cdf to find new pixel values
im2 = interp(im.flatten(),bins[:-1],cdf)
#returns image and cumulative histogram used to map
return im2.reshape(im.shape), cdf
im=image
im2,cdf = histeq(im)
To do the local version, I am trying to use a generic filter like so (using the same image as loaded previously):
def func(x):
cdf=[]
xhist,bins=histogram(x,256,normed=True)
cdf = xhist.cumsum()
cdf = 0.6 * cdf / cdf[-1]
im_out = interp(x,bins[:-1],cdf)
midval=interp(x[24],bins[:-1],cdf)
return midval
print im.shape
im3=ndimage.filters.generic_filter(im, func,size=im.shape,footprint=n.ones((7,7)))
Does anyone have any suggestions/thoughts as to why the second version will not work? I'm really stuck and any comments would be greatly appreciated! Thanks in advance!
You could use the scikit-image library to perform Global and Local Histogram Equalization. Stealing with pride from the link, below is the snippet. The equalization is done with a disk shaped kernel (or footprint), but you could change this to a square, by setting kernel = np.ones((N,M)).
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from skimage import data
from skimage.util import img_as_ubyte
from skimage import exposure
import skimage.morphology as morp
from skimage.filters import rank
# Original image
img = img_as_ubyte(data.moon())
# Global equalize
img_global = exposure.equalize_hist(img)
# Local Equalization, disk shape kernel
# Better contrast with disk kernel but could be different
kernel = morp.disk(30)
img_local = rank.equalize(img, selem=kernel)
fig, (ax_img, ax_global, ax_local) = plt.subplots(1, 3)
ax_img.imshow(img, cmap=plt.cm.gray)
ax_img.set_title('Low contrast image')
ax_img.set_axis_off()
ax_global.imshow(img_global, cmap=plt.cm.gray)
ax_global.set_title('Global equalization')
ax_global.set_axis_off()
ax_local.imshow(img_local, cmap=plt.cm.gray)
ax_local.set_title('Local equalization')
ax_local.set_axis_off()
plt.show()