I am trying to take pixels from an image and plot them ontop of a Blue Marble map. I have figured out how to project them on to the map. I have just not been able to figure out how to color each individual pixel when they are projected onto the map.
I have been using the plot() method, when I do them individually the terminal automatically kills my process because it has to plot ~65000 times. Is there another method I could use? Is there a way to use an array of pixel colors in any of these methods? Is this possible with PIL?
rgb is the color array with a 3-tuple ie. (14,0,0) etc. full_x and full_y are a 2 dimensional array where it is # of pixels x 5 different x,y points (to make the pixel shape on the blue marble image)
This is where I tried to do an array of colors:
for i in range(len(rgb)):
hexV = struct.pack('BBB',*rgb[i]).encode('hex')
hexA.append('#' + hexV)
m.plot(full_x, full_y, color=hexA)
I have also tried:
for i in range(len(rgb)):
hexV = struct.pack('BBB',*rgb[i]).encode('hex')
#hexA.append('#' + hexV)
hexA = '#' + hexV
m.plot(full_x[i], full_y[i], color=hexA[i])
This is where I tried to do each pixel individually and then the process was automatically killed.
Any help would be much appreciated. Thanks in advance.
For anyone who sees this and has the same problem:
Apparently all you have to use is scatter. In order to map pixels/any other points with multiple colors use scatter with an x array, y array and pixel color array.
Related
I am using imshow() to create pseudo-coloured maps of matrices of values (2d numpy arrays). Using clim argument one can set the range of values (below 1 and 6) to be represented within the colour scale (see below). This way for example all outliers (whether 7 or 7000000) will be yellow. This skews the perception of the image, as the reader doesn't know if this pixel is 6 or 700000.
Does anyone know of any way to colour all values outside of this range some other fixed colour of choice, for example, magenta?
The image has 11 different colors.
An example showing what I need to get is given in the picture:
I need to map each color to number and to see shape.
Thanks
If you got the Color in an Array of x,y try
xdiff,ydiff=np.Gradient(Colormap)
shape=np.Logical_and(xdiff,ydiff)
it should give you an Image of the spots were the Color changes.
np.unique(Colormap)
gives you all the Colors present
I'm trying to make an indoor navigation and I need indoor map that robot can automatically navigate the way.
I'm thinking of using image which have different colors for each place(certain section), and I want to know the way to get the coordinates of the certain colors. So that I can designate places to certain color area using that coordinate. How can I get the list of pixel coordinates of the image which have certain color using python? In this case, I know RGB code of color! I am currently using pycharm
Assuming your picture has shape (Y, X, 3). Y is the height of the image and X the width. Last dimension is for the 3 RGB colors channels.
You can use this code on the image to get a list of pixels coordinates with particular RGB set:
def pixels(image, rgb_set):
set = list()
for j in range(0, image.shape[0]):
for i in range(0, image.shape[1]):
if image[j][i] == rgb_set:
set.append([j, i])
return set
I am wondering is there any workaround to convert RGB images to pixel vectors without losing its spatial information in python. As far as I know, I can read the images and do transformation for images to pixel vectors. I am not sure doing this way still preserve images' spatial information in pixel vectors. How can I make this happen for making pixel vectors from RGB image?
my attempt:
I tried as follow but I am not sure how to make
import matplotlib.pyplot as pl
image = plt.imread('dog.jpg')
im = image/255.0
print(im.shape) #(32, 32, 3)
pixels = im.reshape(im.shape[0]*im.shape[1], im.shape[2])
but I want to make sure how to make pixel vectors from RGB images without losing pixel order and its spatial information. How to make this happen? any thoughts?
I think maybe numpy might have functions to do this. Can anyone point me how to do this with numpy?
graphic illustration:
here is simple graphic illustration of making pixel vectors from RGB images:
as this diagram shows, we have RGB images with shape of (4,4,3) which needs to make pixel vectors without losing its spatial information and pixel orders then combine pixel vectors from each channel (Red, Green, Blue) as pixel matrix or dataframe. I am curious how to get this done in python?
goal:
I want to make pixel vectors from RGB images so resulted pixel vectors needs to be expanded with taylor expansion. Can anyone point me out how to make this happen?
Are You just trying to reshape each channel to a vector and then joining them horizontally? That's what I understood from the graphic illustration and the way i would do it is something like this:
import matplotlib.pyplot as plt
import numpy as np
image = plt.imread('monkey.png')
image = image / 255.0
red = image[:,:,0]
green = image[:,:,1]
blue = image[:,:,2]
def to_vector(matrix):
result = []
for i in range(matrix.shape[1]):
result = np.vstack(matrix[:,i])
return result
red = to_vector(red)
green = to_vector(green)
blue = to_vector(blue)
vector = np.hstack((red,green,blue))
Your original attempt was almost a full solution - maybe actually a full solution, depending on what the idea is.
print(im.shape) #(32, 32, 3)
pixels = im.reshape(im.shape[0]*im.shape[1], im.shape[2]) # this is exactly correct
print(pixels.shape) #(1024,3)
reds = pixels[:, 0] #just as an example for where things end up in the result
pixels_channelfirst = np.moveaxis(pixels, 1, 0) # if you want the first axis to be channels
print(pixels.shape) #(3, 1024)
reds = pixels[0, :]
"I want to preserve its pixel order and spatial information" - this does that already! Add one non-zero pixel to a zero image and plot where it goes, if you have doubts. np.hstack in the other answer does as well.
I want to write a script to create an image from a connection matrix. Basically, wherever there is a '1' in the matrix, I want that area to be shaded in the image. For eg -
I created this image using Photoshop. But I have a large dataset so I will have to automate the process. It would be really helpful if anyone could point me in the right direction.
EDIT
The image that I am getting after using the script is this. This is due to the fact that the matrix is large (19 x 19). Is there any way I can increase the visibility of this image so the black and white boxes appear more clear?
I would suggest usage of opencv combined with numpy in this case.
Create two-dimensional numpy.array of dtype='uint8' with 0 for black and 255 for white. For example, to get 2x2 array with white left upper, white right lower, black left lower and black right upper, you could use code:
myarray = numpy.array([[255,0],[0,255]],dtype='uint8')
Then you could save that array as image with opencv2 in this way:
cv2.imwrite('image.bmp',myarray)
In which every cell of array is represented by single pixel, however if you want to upscale (so for example every cell is represented by 5x5 square) then you might use numpy.kron function, with following one line:
myarray = numpy.kron(myarray, numpy.ones((5,5)))
before writing image
May be you can try this!
import matplotlib.cm as cm
# Display matrix
plt.imshow(np.random.choice([0, 1], size=100).reshape((10, 10)),cmap=cm.binary)
With a Seaborn heatmap:
import seaborn as sns
np.random.seed(3)
sns.set()
data = np.random.choice([0, 1], size=(16,16), p=[3./4, 1./4])
ax = sns.heatmap(data, square=True, xticklabels=False, yticklabels=False, cbar=False, linewidths=.8, linecolor='lightgray', cmap='gray_r')
Note the reverse colormap gray_r to have black for 1's and white for 0's.