Related
canonical=[]
purple = [181, 126, 212]
red = [242, 0, 86]
white = [229, 229, 229]
brown = [109, 59, 24]
black = [37, 23, 40]
pink = [254, 180, 218]
orange = [255, 97, 20]
grey = [97, 97, 97]
blue = [0, 104, 149]
green = [0, 231, 160]
yellow = [227, 239, 79]
element=[]
purple1 = [160, 32, 240]
red1 = [255, 0, 0]
white1 = [255, 255, 255]
brown1 = [165, 42, 42]
black1 = [0, 0, 0]
pink1 = [255, 192, 203]
orange1 = [255, 165, 0]
grey1 = [190, 190, 190]
blue1 = [0, 0, 255]
green1 = [0, 255, 0]
yellow1 = [255, 255, 0]
start = time.time()
euclidean[element[3]] = math.sqrt((canonical[0]-element[0])**2 + (canonical[1]-element[1])**2 + (canonical[2]-element[2])**2)
end = time.time()
times[element[3]] = end-start
I wish the formula to be apply to every colour, and I wish an output similar to the following:
euclidean: {'black': 46.3033476111609, 'blue': 136.24610086163935, 'brown': 41.916583830269374, 'green': 118.86547017532047, 'orange': 104.75686135046239, 'pink': 106.68645649753299, 'purple': 45.98912915026767, 'red': 76.2954782408499, 'white': 41.53311931459037, 'yellow': 127.45587471748802}
The number could be different.
I can't quite understand exactly what your colour differencing is doing, but broadly speaking I would approach this with different data structures. For example, put the colours in a dictionary:
colours = {
'white1': [255, 255, 255] ,
'brown1': [165, 42, 42],
}
...and so on.
Then write a function for the distance:
import math
def distance(r, g, b):
return math.sqrt(r**2 + g**2 + b**2)
Then you can do something like this:
distances = {}
for colour, rgb in colours.items():
distances[colour] = distance(*rgb)
That *rgb 'explodes' the rgb tuple into the three arguments the distance() function wants. Later on you can try replacing this explicit loop with a dictionary comprehension.
You will eventually also want to learn about NumPy, which provides n-dimensional arrays and makes computing vector norms and so on much easier.
I'm trying to calculate area of polygon bounding boxes and the coordinates (x and y of polygon vertices) are saved in mask variable. This is my code:
f = open('train_labels.json')
data = json.load(f)
mask = []
for i in data.keys(): # iterate over all images in dataset
for j in data[i]: # iterate over json to access points (coordinates)
mask.append(j['points'])
print(mask)
area = (mask[:, 3] - mask[:, 1]) * (mask[:, 2] - mask[:, 0])
print(area)
Error that shows me is: TypeError: list indices must be integers or slices, not tuple
When I print mask, the output is:
[[[141, 199], [237, 201], [313, 236], [357, 283], [359, 300], [309, 261], [233, 230], [140, 231]], [[25, 13], [57, 71], [26, 92], [0, 34]], [[264, 21], [296, 0], [300, 9], [272, 31]], [[322, 0], [351, 25], [340, 31], [317, 9]]] [[[141, 199], [237, 201], [313, 236], [357, 283], [359, 300], [309, 261], [233, 230], [140, 231]], [[25, 13], [57, 71], [26, 92], [0, 34]], [[264, 21], [296, 0], [300, 9], [272, 31]], [[322, 0], [351, 25], [340, 31], [317, 9]], [[287, 71]]] etc...
So between tripple square brackets ...,[317, 9]]] [[[141, 199],... doesn't exist comma (,) and is that the problem? If is how can I solve this?
Try:
xy = list(zip(*mask[-1]))
area = (max(xy[0])-min(xy[0]))*(max(xy[1])-min(xy[1]))
mask[-1] will get the last item appended to mask (if you need the mask storing all the items).
xy is a two element list with all x-coordinates in first element and all y-coordinates of the polygon in second element.
area of the bounding box goes respectively from min, to max of x and y coordinates. The 'polygon' can have any number of points, but at least one.
I suggest you choose another name for mask as the name 'mask' suggest a use as mask which is not the case. For example polygons_points_xy will better express what value is represented by the name.
My code combines values from two matrices and lists them side by side. T works as I need properly.
We are trying to remove the field where 2 identical values are located. This can be better seen in the example below
my code
import os
import numpy as np
import sys
b=np.array([[13,14,15],
[22,23,24],
[31,32,33]])
#print(b)
d=np.array([100,200,300,400,500])
b[-1,:] = d[:b.shape[1]] # last row
b[:-1,-1] = d[b.shape[1]:]
val1 = np.hstack(b[::-1])
val2 = np.hstack([d[i:i+b.shape[1]] for i in range(b.shape[0])])
res = zip(val1, val2)
for i, j in res:
l=[i, j]
print(l)
my output
[100, 100]
[200, 200]
[300, 300]
[22, 200]
[23, 300]
[500, 400]
[13, 300]
[14, 400]
[400, 500]
My code combines values from two matrices and lists them side by side. T works as I need properly.
We are trying to remove the field where 2 identical values are located. This can be better seen in the example below
I would need to remove matrices in my output that contain the same numbers. As you can see in the output below
The matrices do not always have to be the same and do not have to match the same iterations
required output
[22, 200]
[23, 300]
[500, 400]
[13, 300]
[14, 400]
[400, 500]
Find where the values are different and only concatenate those values.
>>> # using val1 and val2 from the question
>>> mask = np.where(val1!=val2)
>>> mask
(array([3, 4, 5, 6, 7, 8], dtype=int64),)
>>> np.vstack((val1[mask],val2[mask]))
array([[ 22, 23, 500, 13, 14, 400],
[200, 300, 400, 300, 400, 500]])
>>> np.vstack((val1[mask],val2[mask])).T
array([[ 22, 200],
[ 23, 300],
[500, 400],
[ 13, 300],
[ 14, 400],
[400, 500]])
>>>
It is as simple as comparing the two arrays and using the result as a boolean index:
np.stack([val1, val2], axis=1)[val1 != val2]
I want to create an array with the following structure when printed:
1: (10,20),
2: (20,30),
3: (30,40),
4: (40,50),
and so on...
Really new to python so anything helps! Using python 3.
you can simply do by python range function range(start, end, stepsize)
final_lst = []
for i in range(10, 200, 10):
final_lst.append([i, i+10])
print(final_lst)
output
[[10, 20], [20, 30], [30, 40], [40, 50], [50, 60], [60, 70], [70, 80], [80, 90], [90, 100], [100, 110], [110, 120], [120, 130], [130, 140], [140, 150], [150, 160], [160, 170], [170, 180], [180, 190], [190, 200]]
::edit::
for i in range(1, 10):
print('%s: (%s,%s),'%(i, i*10, (i+1)*10))
output
1: (10,20),
2: (20,30),
3: (30,40),
4: (40,50),
5: (50,60),
6: (60,70),
7: (70,80),
8: (80,90),
9: (90,100),
take two variables cnt1 and cnt2
import numpy as np
myList = []
cnt1=1
cnt2=2
for i in range(n): #n is desired length
myList.append([10*cnt1,10*cnt2])
cnt1+=1
cnt2+=1
myArray = np.array(myList)
You can write a function that uses range to get a range of values and a list comprehension (basically an inline for-loop) to generate a list of tuples. Something like this:
def get_repeated_list(max_n):
values = range(10, max_n, 10)
return [(i, i+10) for i in values]
Example usage:
>>> get_repeated_list(50)
[(10, 20), (20, 30), (30, 40), (40, 50)]
Looks like what you want to do is is transform a list into another list of previous/next elements.
listA = [ 10 * i for i in range( N ) ]
listB = [ (listA[ i ], listA[ i + 1 ]) for i in range( len( listA ) - 1 ) ];
Let l be your list. It doesn't matter what is in it. Use zip to make a new list, with wanted feature. zip, matches elements of two list together. if you zip a list with itself, you'll have a new list that each element in first list is repeated twice in it.
l = [1, 2, 5, 11, 12]
for element in zip(l, l):
print(element)
output:
(1, 1)
(2, 2)
(5, 5)
(11, 11)
(12, 12)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I want to print a color on the screen using RGB values and the output should be just a single color. For example if I give RGB values of red, I want the output to show me a red color. But when I try this code, it isn't working. What am I missing?
import matplotlib.pyplot as plt
plt.imshow([(255, 0, 0)])
plt.show()
The output is:
The issue is that you are trying to display a 2D color array with 1 row and 3 columns. The pixel values from left to right are 255, 0and 0. As #Ben K. correctly pointed out in the comments, by doing so the intensity values are scaled to the range 0..1 and displayed using the current colormap. That's why your code displays one yellow pixel and two violet pixels.
If you wish to specify the RGB values you should create a 3D array of m rows, n columns and 3 color channels (one chromatic channel for each RGB component).
Demo
The snippet below generates a random array of indices of a color palette and displays the result:
In [14]: import numpy as np
In [15]: import matplotlib.pyplot as plt
In [16]: from skimage import io
In [17]: palette = np.array([[255, 0, 0], # index 0: red
...: [ 0, 255, 0], # index 1: green
...: [ 0, 0, 255], # index 2: blue
...: [255, 255, 255], # index 3: white
...: [ 0, 0, 0], # index 4: black
...: [255, 255, 0], # index 5: yellow
...: ], dtype=np.uint8)
...:
In [18]: m, n = 4, 6
In [19]: indices = np.random.randint(0, len(palette), size=(4, 6))
In [20]: indices
Out[20]:
array([[2, 4, 0, 1, 4, 2],
[1, 1, 5, 5, 2, 0],
[4, 4, 3, 3, 0, 4],
[2, 5, 0, 5, 2, 3]])
In [21]: io.imshow(palette[indices])
Out[21]: <matplotlib.image.AxesImage at 0xdbb8ac8>
You could also generate a random color pattern rather than using a color palette:
In [24]: random_colors = np.uint8(np.random.randint(0, 255, size=(m, n, 3)))
In [24]: random_colors
Out[27]:
array([[[137, 40, 84],
[ 42, 142, 25],
[ 48, 240, 90],
[ 22, 27, 205],
[253, 130, 22],
[137, 33, 252]],
[[144, 67, 156],
[155, 208, 130],
[187, 243, 200],
[ 88, 171, 116],
[ 51, 15, 157],
[ 39, 64, 235]],
[[ 76, 56, 135],
[ 20, 38, 46],
[216, 4, 102],
[142, 60, 118],
[ 93, 222, 117],
[ 53, 138, 39]],
[[246, 88, 20],
[219, 114, 172],
[208, 76, 247],
[ 1, 163, 65],
[ 76, 83, 8],
[191, 46, 53]]], dtype=uint8)
In [26]: io.imshow(random_colors)
Out[26]: <matplotlib.image.AxesImage at 0xe6c6a90>
This is the output produced by
import matplotlib.pyplot as plt
plt.imshow([(3,0,0),(0,2,0),(0,0,1)])
plt.colorbar()
plt.show()
You see that the three tuples I provided to imshow are interpreted as rows of a matrix:
3 0 0
0 2 0
0 0 1
The numeric values are mappped to colors for the plot. The colorbar function shows the mapping between colors and numeric values.
To draw rectangles, refer to this SO question, but replace the value of the facecolor parameter with one of the following possibilities:
A color name, as a string.
A Hex color code, given as a string with a leading # sign. For example, facecolor='#FF0000' is bright red.
A triple with three values between 0 and 1, which specify the (Red, Green, Blue) parts of your color. (Not 0 to 255 like you assumed in your question!)
Use the edgecolor parameter in the same manner to determine the color of the rectangle border, or use 'None' to draw no border.