I Create function to log transform an image in python. Below is my code..
#Compute Log Only
def logTransform(c, f):
g = c * m.log(float(1+f))
return g
def ImgLogarithmic (img_input, coldepth):
#logarithmatic transform
inputMax = 255
outputMax = 255
c = outputMax/m.log(inputMax+1)
if coldepth != 24:
img_input = img_input.convert('RGB')
img_output = Image.new('RGB', (img_input.size[1], img_input.size[0]))
pixels = img_output.load()
for i in range(img_output.size[0]):
for j in range(img_output.size[1]):
f = img_input.getpixel((i, j))
r = round(logTransform(c, f[0]))
g = round(logTransform(c, f[1]))
b = round(logTransform(c, f[2]))
#b = value * math.log(1 + b)
pixels[i,j] = (r,g,b)
if coldepth == 1:
img_output = img_output.convert("1")
elif coldepth == 8:
img_output = img_output.convert("L")
else:
img_output = img_output.convert("RGB")
return img_output
I create that function to call log transform. But, my code is not working. I think my algorithm is right. Anyone can help? Thank you
Related
I wanna run this code for a wide range instead of this range. So I wanna make it better to run faster.
Is it impossible to use something else instead of these loops?
z1=3
z2=HEIGHT-1
def myfunction(z1,z2):
for l in range(z1):
vector = np.zeros(WIDTH)
vector[WIDTH//2] = 1
result = []
result.append(vector)
for i in range(z2):
vector = doPercolationStep(vector, PROP, i)
result.append(vector)
result = np.array(result)
ss = result.astype(int)
ss = np.where(ss==0, -1, ss)
ww = (ss+(ss.T))/2
re_size = ww/(np.sqrt(L))
matr5 = re_size
np.savetxt('F:/folder/matr5/'+str(l)+'.csv', matr5)
and doPercolationStep is:
WIDTH = 5
HEIGHT = 5
L=5
PROP = 0.6447
def doPercolationStep(vector, PROP, time):
even = time%2 # even is 1 or 0
vector_copy = np.copy(vector)
WIDTH = len(vector)
for i in range(even, WIDTH, 2):
if vector[i] == 1:
pro1 = random.random()
pro2 = random.random()
if pro1 < PROP:
vector_copy[(i+WIDTH-1)%WIDTH] = 1 # left neighbour of i
if pro2 < PROP:
vector_copy[(i+1)%WIDTH] = 1 # right neighbour of i
vector_copy[i] = 0
return vector_copy
I've an issues to implement PixelRL. PixelRL github. I'd like to reading and saving an image to raw data with int16 ( -32768 ~ 32767 ). But I don't find how to make cv2.imwrite() support it. When using p = (p[0]*65535+0.5).astype(np.int16), saving 8bit images. Is there a way to write the image to int16 and raw data?
Thank you.
class MiniBatchLoader(object):
def __init__(self, train_path, test_path, image_dir_path, crop_size):
# load data paths
self.training_path_infos = self.read_paths(train_path, image_dir_path)
self.testing_path_infos = self.read_paths(test_path, image_dir_path)
self.crop_size = crop_size
# test ok
#staticmethod
def path_label_generator(txt_path, src_path):
for line in open(txt_path):
line = line.strip()
src_full_path = os.path.join(src_path, line)
if os.path.isfile(src_full_path):
yield src_full_path
# test ok
#staticmethod
def count_paths(path):
c = 0
for _ in open(path):
c += 1
return c
# test ok
#staticmethod
def read_paths(txt_path, src_path):
cs = []
for pair in MiniBatchLoader.path_label_generator(txt_path, src_path):
cs.append(pair)
return cs
def load_training_data(self, indices):
return self.load_data(self.training_path_infos, indices, augment=True)
def load_testing_data(self, indices):
return self.load_data(self.testing_path_infos, indices)
# test ok
def load_data(self, path_infos, indices, augment=False):
mini_batch_size = len(indices)
in_channels = 1
if augment:
xs = np.zeros((mini_batch_size, in_channels, self.crop_size, self.crop_size)).astype(np.float32)
for i, index in enumerate(indices):
path = path_infos[index]
img = cv2.imread(path,0)
if img is None:
raise RuntimeError("invalid image: {i}".format(i=path))
h, w = img.shape
if np.random.rand() > 0.5:
img = np.fliplr(img)
if np.random.rand() > 0.5:
angle = 10*np.random.rand()
if np.random.rand() > 0.5:
angle *= -1
M = cv2.getRotationMatrix2D((w/2,h/2),angle,1)
img = cv2.warpAffine(img,M,(w,h))
rand_range_h = h-self.crop_size
rand_range_w = w-self.crop_size
x_offset = np.random.randint(rand_range_w)
y_offset = np.random.randint(rand_range_h)
img = img[y_offset:y_offset+self.crop_size, x_offset:x_offset+self.crop_size]
xs[i, 0, :, :] = (img/255).astype(np.float32)
elif mini_batch_size == 0:
for i, index in enumerate(indices):
path = path_infos[index]
img = cv2.imread(path,0)
if img is None:
raise RuntimeError("invalid image: {i}".format(i=path))
h, w = img.shape
xs = np.zeros((mini_batch_size, in_channels, h, w)).astype(np.float16)
xs[0, 0, :, :] = (img/255).astype(np.float16)
else:
raise RuntimeError("mini batch size must be 1 when testing")
return xs
def test(loader, agent, fout):
sum_psnr = 0
sum_reward = 0
test_data_size = MiniBatchLoader.count_paths(TESTING_DATA_PATH)
current_state = State.State((TEST_BATCH_SIZE,1,CROP_SIZE,CROP_SIZE), MOVE_RANGE)
for i in range(0, test_data_size, TEST_BATCH_SIZE):
raw_x = loader.load_testing_data(np.array(range(i, i+TEST_BATCH_SIZE)))
raw_n = np.random.normal(MEAN,SIGMA,raw_x.shape).astype(raw_x.dtype)/255
current_state.reset(raw_x,raw_n)
reward = cp.zeros(raw_x.shape, raw_x.dtype)*255
for t in range(0, EPISODE_LEN):
previous_image = current_state.image.copy()
action, inner_state = agent.act(current_state.tensor)
current_state.step(action, inner_state)
reward = np.square(raw_x - previous_image)*255 - np.square(raw_x - current_state.image)*255
sum_reward += cp.mean(reward)*cp.power(GAMMA,t)
agent.stop_episode()
p = np.maximum(0,current_state.image)
p = np.minimum(1,p)
p = (p[0]*65535+0.5).astype(np.int16)
p = cp.transpose(p,(1,2,0))
cv2.imwrite(output_save_path+'/'+str(i)+'.tiff',p)
is there a way ... ?
No.
https://docs.opencv.org/3.4/d4/da8/group__imgcodecs.html#gabbc7ef1aa2edfaa87772f1202d67e0ce
In general, only 8-bit single-channel or 3-channel ... images can be saved using this function, with these exceptions:
16-bit unsigned (CV_16U) images can be saved ...
You might want to munge your signed 16-bit values into CV_16U format.
Alternatively, you might consider writing 32-bit images, and then invoking a post-processing program to shrink them to your favorite 16-bit format.
I am new to Python and I am trying to convert and modify the following code from Matlab to python:
function A = alg_a(RGBimage, n)
[row, column, d] = size(RGBimage);
if (d==3)
HSVimage = rgb2hsv(RGBimage);
V = HSVimage(:,:,3);
else
V =double(RGBimage)/255;
end
V=V(:);
[Vsorted, ix] = sort(V);
s = (row*column)/n;
i=0;
h=[];
while (i < n)
i=i+1;
z = Vsorted(((floor(s*(i-1))+1)):floor(s*i));
Vstart = (s*(i-1))/(row*column);
Vstop = (s*i)/(row*column);
r=z-z(1);
f = (1/n)/(r(size(r,1)));
g = r*f;
if(isnan(g(1)))
g = r + Vstop;
else
g = g + Vstart;
end
h=vertcat(h,g);
end
m(ix)=h;
m=m(:);
if(d==3)
HSVimage(:,:,3) = reshape(m,row,column);
A=hsv2rgb(HSVimage);
else
A=reshape(m,row,column);
end
return;
end
This function implements the SMQT image preprocessing algorithm, and my problem is that I don't really understand how it works, which is why I'm stuck here:
import cv2
import numpy as np
import math
img = cv2.imread("16.bmp")
n = 8
(row, column, dim) = img.shape
if dim == 3:
hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
V = hsv_img[:, :, 2]
else:
V = img / 255
V = V.flatten(order='F')
Ix = V.argsort()[::-1]
V_sorted = V[Ix]
s = int(row*column/n)
i = 0
h = []
while i < n:
i += 1
z = V_sorted[math.floor(s*(i-1))+1:math.floor(s*i)]
z = np.array(z)
V_start = (s*(i-1))/(row*column)
V_stop = (s * i) / (row * column)
r = z - z[0]
f = (1 / n) / r[len(r)-1]
g = r * f
if math.isnan(g[0]):
g = r + V_stop
else:
g = g + V_start
if len(h) == 0:
h = g
else:
h = np.vstack((h, g))
M = np.array([])
M[Ix] = h
M = M.flatten(order='F')
if dim == 3:
hsv_img = np.reshape(M, row, column)
img_res = cv2.cvtColor(img, cv2.COLOR_HSV2BGR)
else:
img_res = np.reshape(M, row, column)
cv2.imwrite('16_smqt', img_res)
It seems that I wrote a code similar in functionality, but it does not work, and I haven't any idea why. The Matlab code was taken by me from the article, so I have no doubt that it works. Please help me find inaccuracies in my translation from Matlab to python.
In the above image, how to get a clean line block, remove the block upper and down around the middle long strip broadband? I have tried projection, but failed.
def hProject(binary):
h, w = binary.shape
hprojection = np.zeros(binary.shape, dtype=np.uint8)
[red is the result][2]
h_h = [0]*h
for j in range(h):
for i in range(w):
if binary[j,i] == 255:
h_h[j] += 1
return h_h
def creat_T_rectangle(h, w, mode='x_up'):
assert mode in ['x_up', 'x_down', 'y_left', 'y_right']
if mode == 'x_up':
up_t = np.ones((h*2, w*3), np.uint8)
up_t[:h, :w] = 0
up_t[:h, 2*w:] = 0
return up_t, (0, h, w, 2*w) # y1, y2, x1, x2
elif mode == 'y_left':
left_t = np.ones((h*3, w*2), np.uint8)
left_t[:h, :w] = 0
left_t[2*h:, :w] = 0
return left_t, (h, 2*h, 0, w)
elif mode == 'x_down':
down_t = np.ones((h*2, w*3), np.uint8)
down_t[h:2*h, :w] = 0
down_t[h:2*h, 2*w:] = 0
return down_t, (h, 2*h, w, 2*w)
elif mode == 'y_right':
right_t = np.ones((h*3, w*2), np.uint8)
right_t[:h, w:2*w] = 0
right_t[2*h:, w:] = 0
return right_t, (h, 2*h, w, 2*w)
else:
raise NotImplementedError
def remove_around_rectangle(markers, bh, bw):
'''
markers:binary image, bh, bw = 5, 5 ...
'''
up_t, up_rect = creat_T_rectangle(bh, bw, mode='x_up')
down_t, down_rect = creat_T_rectangle(bh, bw, mode='x_down')
one_nums = up_t.sum()
i = bh
j = bw
while i < markers.shape[0]-bh:
while j < markers.shape[1]-2*bw:
block = markers[i-bh:i+bh, j-bw:j+2*bw]
inner_up = block * up_t
inner_down = block * down_t
if inner_down.sum()//255 == inner_up.sum()//255 == one_nums:
markers[down_rect[0]+i-bh:down_rect[1]+i-bh, down_rect[2]+j-bw:down_rect[3]+j-bw] = 0
else:
if inner_up.sum()//255 == one_nums:
markers[up_rect[0]+i-bh:up_rect[1]+i-bh, up_rect[2]+j-bw:up_rect[3]+j-bw] = 0
if inner_down.sum()//255 == one_nums:
markers[down_rect[0]+i-bh:down_rect[1]+i-bh, down_rect[2]+j-bw:down_rect[3]+j-bw] = 0
j += bw
i += 1
j = bw
left_t, left_rect = creat_T_rectangle(bh, bw, mode='y_left')
one_nums = left_t.sum()
right_t, right_rect = creat_T_rectangle(bh, bw, mode='y_right')
i = bh
j = bw
while i < markers.shape[0] - 2*bh:
while j < markers.shape[1] - bw:
block = markers[i-bh:i+2*bh, j-bw:j+bw]
inner_left = block * left_t
inner_right = block * right_t
if inner_left.sum()//255 == one_nums == inner_right.sum()//255 :
markers[left_rect[0]+i-bh:left_rect[1]+i-bh, left_rect[2]+j-bw:left_rect[3]+j-bw] = 0
else:
if inner_right.sum()//255 == one_nums:
markers[right_rect[0]+i-bh:right_rect[1]+i-bh, right_rect[2]+j-bw:right_rect[3]+j-bw] = 0
if inner_left.sum()//255 == one_nums :
markers[left_rect[0]+i-bh:left_rect[1]+i-bh, left_rect[2]+j-bw:left_rect[3]+j-bw] = 0
j += bw
i += 1
j = bw
return markers
the above is my code, but it is so slow.
We have a code to draw circles on the Location on the map with the name of each category. Now the circles and text are one color. How do we get them in different color's by category? Example: Category Garden: Blue, Category Stone: Grey.
So far the code:
size(1500,800)
background(1)
nofill()
stroke('#f91')
pen(.2)
fill('#f91', 0.05)
rotate(90)
font("Avenir", "bold", 10)
align('left')
def mapValue(value, fromMin, fromMax, toMin, toMax):
# Figure out how 'wide' each range is
fromSpan = fromMax - fromMin
toSpan = toMax - toMin
# Convert the from range into a 0-1 range (float)
valueScaled = float(value - fromMin) / float(fromSpan)
# Convert the 0-1 range into a value in the to range.
return toMin + (valueScaled * toSpan)
def xOfDot(lon):
return mapValue(lon, -100, 100, 0, WIDTH)
def yOfDot(lat):
return mapValue(lat, -90, 90, HEIGHT, 0)
with open('theft-alerts.json', 'r') as inputFile:
data = json.load(inputFile)
print len(data)
artworksPerCity = {}
for stolenArt in data:
if stolenArt.has_key('Category'):
city = stolenArt['Category']
if stolenArt.has_key('nItemsStolen'):
numbersStolen = int(stolenArt['nItemsStolen'])
if artworksPerCity.has_key(city):
# Adjust the value stored for this city
artworksPerCity[city] = artworksPerCity[city] + numbersStolen
else:
# Create new key with new value
artworksPerCity[city] = numbersStolen
# Draw circle on the map
radius = artworksPerCity[city] /2
x = xOfDot(stolenArt['Lon'])
y = yOfDot(stolenArt['Lat'])
arc(x, y, radius)
text(city, x, y)
print artworksPerCity
Here is a sketch of what I intend to include in my pure python data utility.
def hexidecimalDiget(n,deHex = false):
if(n<0):
print "negitive values not supported by call to hexidecimalDiget("+str(n)+")"
return None
elif(n < 10):
return str(n)
elif(n < 15):
return ["a","b","c","d","e"][n-10]
elif(n in ["a","b","c","d","e"]):
if deHex:
return ["a","b","c","d","e"].index(n)
return n
else:
print "call to hexidecimalDiget("+str(n)+") not supported!"
return None
def colorFormHexArray(arr):
if len(arr)!=3 and len(arr)!=6:
print "invalid length for color on call to colorFormHexArray("+str(arr)+")"
return None
elif None in arr:
print "cannot make color from None arguments in "+str(arr)
return None
else:
ret = "#"
for k in arr:
if(type(k) == list):
for k2 in k:
ret+=hexidecimalDiget(k)
else:
ret+=hexidecimalDiget(k)
return ret
def arrayFromColor(c):
c = c.replace("#","")
col = []
for n,k in enumerate(c):
if(len(c) == 3):
col.append([hexidecimalDiget(k,deHex = True)])
elif(len(c) == 6):
col.append([hexidecimalDiget(c[(n+1)*2-2],deHex = True),hexidecimalDiget(c[(n+1)*2-2],deHex = True)])
return(col)
def intFromHexPair(hp):
ret = 0
for n,k in enumerate(hp):
digBase = 16**(len(hp)-n-1)
ret+=digBase*hexidecimalDiget(hp[0],deHex = True)
return ret
def hexPairFromInt(I,minDigits = 1,maxDigits = 256):
if I<0:
print "negitive numbers not supported by hexPairFromInt"
k= 0
while(16**(k+1) <= I):
k+=1
if k < minDigits:
k = minDigits
if k > minDigits:
print("maxDigitsExceeded")
ret = []
while k>=0
dig = 16**k
ret.append(hexidecimalDiget(int(I)%(dig))
I -= dig
k-=1
return ret
def specColor(start,end,bottom,top):
start = arrayFromColor(start)
end = arrayFromColor(end)
def ret(v):
if( v<start or c>end ):
print("value out of range "+str([start,end]))
return('#aa0000') #eyo <- error red
else:
starts = [intFromHexPair(k) for k in start]
ends = [intFromHexPair(hp) for k in end]
normalized = (v-bottom)/(top-bottom)
return colorFormHexArray([hexPairFromInt(int((starts[n]-ends[n])*normalized),minDigits = 1,maxDigits = 256) for n,k in enumerate(starts)])
return ret
This seems excessive and hasn't even been slightly tested yet (just a stetch up atm) but I'll be testing and incorporating this code here tonight :: http://krewn.github.io/KPlot/