Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to create an array 2D contain like this.
that every tuple contain three numbers that represent the coloring RGB system
[
(0,0,0) (0,0,1) (0,0,2) (0,0,3) (0,0,4) (0,0,5) (0,0,6) (0,0,7)
(0,0,8) (0,0,9) (0,0,10) (0,0,11) (0,0,12) (0,0,13) (0,0,14) (0,0,15)
(0,0,16) (0,0,17) (0,0,18) (0,0,19) (0,0,20) (0,0,21) (0,0,22) (0,0,23)
.....
(250,250,242) (250,250,243) (250,250,244) .... (250,250,250)
]
and the same but for a common RGB, number .this array for example 8x8
and to give a key to each tuple that refers to the name of this color
for example ,like (0,0,0) => black
--
I tried .. and did like this
arr2 = np.array([(i,i,i) for i in range(250)] , dtype = [('Red','i2'),('Green','i2'),('Blue','i2')])
print(arr2)
but that did not work!
You could use a list comprehension:
>>> colors = [(r, g, b) for r in range(256) for g in range(256) for b in range(256)]
>>> colors[:10] # First 10 colors
[(0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 0, 3), (0, 0, 4), (0, 0, 5), (0, 0, 6), (0, 0, 7), (0, 0, 8), (0, 0, 9)]
>>> colors[-10:] # Last 10 colors
[(255, 255, 246), (255, 255, 247), (255, 255, 248), (255, 255, 249), (255, 255, 250), (255, 255, 251), (255, 255, 252), (255, 255, 253), (255, 255, 254), (255, 255, 255)]
>>> len(colors) # Numbers of colors in RGB
16777216
You could try using a dictionary.
dict = {(0, 0, 0): "black", ... }
You could then search for color with the RGB like this.
print(dict[(0, 0, 0)])
Related
I'm trying to get the rgba values of the pixels of an image.
Google suggests I use code similar to this:
from PIL import Image
im = Image.open("C:/Stuff/image.png", "r")
px = list(im.getdata())
My problem is the data not always being in rgba format.
On some images it does return rgba
[(0, 0, 0, 255), (0, 0, 0, 255), (0, 0, 255, 255), [...]
while on others it returns rgb
[(0, 0, 0), (0, 0, 0), (0, 0, 255), [...]
and on some it returns whatever this is
[0, 0, 1, [...]
Is there a way to always get rgba returned?
I have images (png) that are 128x128 pixels, how do I convert the image so that each pixel in the image is closest in color to the ones in the following array?
The array will probably get bigger with more specific colors, but in this case:
[(0, 255, 100), (100, 100, 100), (255, 255, 255), (0, 0, 0), (156, 126, 210)]
I have an array of tuples:
a = [(375, 193)
(364, 113)
(277, 20)
(271, 16)
(52, 106)
(133, 266)
(289, 296)
(372, 282)]
How to draw lines between points in OpenCV?
Here is my code that isn't working:
for index, item in enumerate(a):
print (item[index])
#cv2.line(image, item[index], item[index + 1], [0, 255, 0], 2)
Using draw contours, you can draw the shape all at once.
img = np.zeros([512, 512, 3],np.uint8)
a = np.array([(375, 193), (364, 113), (277, 20), (271, 16), (52, 106), (133, 266), (289, 296), (372, 282)])
cv2.drawContours(img, [a], 0, (255,255,255), 2)
If you don't want the image closed and want to continue how you started:
image = np.zeros([512, 512, 3],np.uint8)
pointsInside = [(375, 193), (364, 113), (277, 20), (271, 16), (52, 106), (133, 266), (289, 296), (372, 282)]
for index, item in enumerate(pointsInside):
if index == len(pointsInside) -1:
break
cv2.line(image, item, pointsInside[index + 1], [0, 255, 0], 2)
Regarding your current code, it looks like you are trying to access the next point by indexing the current point. You need to check for the next point in the original array.
A more Pythonic way of doing the second version would be:
for point1, point2 in zip(a, a[1:]):
cv2.line(image, point1, point2, [0, 255, 0], 2)
If you just want to draw lines, how about cv2.polylines? cv2.drawContours would be preferred when you already have a contours object.
cv2.polylines(image,
a,
isClosed = False,
color = (0,255,0),
thickness = 3,
linetype = cv2.LINE_AA)
I am using the Semantic Segmentation network (SegNet). I am trying to reduce the number of classes and thus rearranging the network.
Therefore, I am also changing the color-coding of the predictions as well. My problem is I don't get the intended colors in the output image.
For e.g.
pascal_palette = np.array([(0, 0, 0),
(0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0),
(0, 0, 128), (0, 128, 0), (0, 0, 0), (0, 0, 0), (128, 0, 0),
(0, 0, 0), (0, 0, 0)
], dtype=np.uint8)
The above line gives perfect results for the three classes as the pixels are only in 1 channel.
The output is as below:
However, if I modify the line and add values to different channels it gives weird output. The output is attached below:
pascal_palette = np.array([(0, 0, 0),
(0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0),
(0, 0, 128), (124, 252, 0), (0, 0, 0), (0, 0, 0), (128, 0, 0),
(0, 0, 0), (0, 0, 0)
], dtype=np.uint8)
Changed the color code to (124, 252, 0). The code should be for lawn green color. I also checked it on a website like RBG codes
What am I missing here? Any explanation will be helpful.
Prediciton code:
prob = model.predict(net_in)[0]
# Reshape to 2d here since the networks outputs a flat array per channel
prob_edge = np.sqrt(prob.shape[0]).astype(np.int)
prob = prob.reshape((prob_edge, prob_edge, 13))
# Upsample
if args.zoom > 1:
prob = interp_map(prob, args.zoom, image_size[1], image_size[0])
# Recover the most likely prediction (actual segment class)
prediction = np.argmax(prob, axis=2)
# Apply the color palette to the segmented image
color_image = np.array(pascal_palette)[prediction.ravel()].reshape(
prediction.shape + (3,))
print('Saving results to: ', args.output_path)
with open(args.output_path, 'wb') as out_file:
Image.fromarray(np.multiply(color_image,255)).save(out_file)
PS. I have used same model for predictions in both case
The problem is very probably in np.multiply(color_image,255).
As you created a pallete already with values from 0 to 255 and you're simply gathering values from this pallete, you don't need to multiply it by 255.
Use simply Image.fromarray(color_image).save(out_file).
I'm trying to create an image from 1d numpy array of integers so that changes to this array reflects in the image. It seems that Image.frombuffer perfectly fits my needs. There's my attempts:
from PIL import Image
import numpy as np
data = np.full(100, 255, dtype = np.int32)
img = Image.frombuffer('RGB', (10, 10), data)
print(list(img.getdata()))
I expected to see a list of 100 tuples (0, 0, 255). But what I'm actually getting is (0, 0, 255), (0, 0, 0), (0, 0, 0), (0, 255, 0), (0, 0, 0), (0, 0, 0), (255, 0, 0), (0, 0, 0), (0, 0, 255), (0, 0, 0), (255, 0, 0), ...
What is the reason of that behavior?
'RGB' uses three bytes per pixel. The buffer that you provided is an array with data type numpy.int32, which uses four bytes per element. So you have a mismatch.
One way to handle it is to use mode 'RGBA':
img = Image.frombuffer('RGBA', (10, 10), data)
Whether or not that is a good solution depends on what you are going to do with the image.
Also note that whether you get (255, 0, 0, 0) or (0, 0, 0, 255) for the RGBA pixels depends on the endianess of the integers in data.
For an RGB image, here's an alternative:
data = np.zeros(300, dtype=np.uint8)
# Set the blue channel to 255.
data[2::3] = 255
img = Image.frombuffer('RGB', (10, 10), data)
Without more context for the problem, I don't know if that is useful for you.