I want to plot a heatmap on a png [closed] - python

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 6 days ago.
Improve this question
I have a dataset of x and y coordinates of eye gaze data with fixation duration.
I want to plot a heatmap on a png image and the output will be like in the picture or in the link
How do I plot it in Python
Let's assume that this is the database below
we have x , y and time [900.399, 980.142, 0.78] ,, so the longest time represents high temperature and the shortest time represents low temperature
x and y represent the coordinates of the eye focus on the image because the image = width and height x and y
data = [ [900.399, 980.142, 0.78], [922.252, 880.885, 0.68], [724.311, 780.543, 0.58], [523.195, 582.994, 0.46], [623.431, 680.427, 0.76], [926.363, 881.791, 1.81], [722.942, 783.257, 0.75], [223.751, 279.995, 0.16], [723.215, 781.004, 0.64], [724.541, 779.889, 0.55] ]
and let's also assume that this is the width and height image that I want to plot on it = [1920, 1080]
Can someone help me designing a method in python to generate heatmap.
https://i.insider.com/53ce61e16bb3f7dd693ffa82?width=1000&format=jpeg&auto=webp

Related

How to I extract objects? [closed]

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 last year.
This post was edited and submitted for review last year and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I want to split images like this in a way that every symbols gets splits up vertically kind of like this input image:
![input image][1]
to this:
![here][2]
The problem is each symbol might have different width so I can't really fix the splitting points like we do in array splitting. If all objects had same width then I could segment the image base on width. In this scenario, what logic I should use to extract these connected objects?
First load the img from the url
import numpy as np
import urllib.request
from PIL import Image
from matplotlib import pyplot as plt
urllib.request.urlretrieve(
'https://i.stack.imgur.com/GRHzg.png',
"img.png")
img = Image.open("img.png")
img.show()
Then consider the black part as "filled" and convert in numpy array
arr = (np.array(img)[:,:,:-1].sum(axis=-1)==0)
If we sum the rows values for each column we can have a simple sum of how much pixel are filled in each column:
plt.subplot(211)
plt.imshow(arr, aspect="auto")
plt.subplot(212)
plt.plot(arr.sum(axis=0))
plt.xlim(0,arr.shape[1])
finally if we compute the differential of this sum over the columns we can obtain the following result:
plt.subplot(211)
plt.imshow(arr, aspect="auto")
plt.subplot(212)
plt.plot(np.diff(arr.sum(axis=0)))
plt.xlim(0,arr.shape[1])
At this point you can simply chose a threshold and cut the image:
threshold = 25
cut = np.abs(np.diff(arr.sum(axis=0)))>threshold
x_lines = np.arange(len(cut))[cut]
plt.imshow(arr, aspect="auto")
plt.vlines(x_lines, 0, arr.shape[0], color="r")
This is my solution and it works fine, but it is sensitive to the chosen threshold and to the columns gradient. I hope it is useful.

Removing the single pixels from Geotiff bianary image [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
This is classified image of satellite. Can anybody tell me how to remove these single pixels of filter out them. Remember this is in Geotiff format. I already applied erosion or dilation techniques but no success.
I saw a similar question on SO but can't find it. There were a quite good answer that I remade for myself. So here is the method called particle_filter that will be the solution for your problem:
def particle_filter(image_, power):
nb_components, output, stats, centroids = cv2.connectedComponentsWithStats(image_, connectivity=8)
sizes = stats[1:, -1]
nb_components = nb_components - 1
min_size = power
img2 = np.zeros(output.shape, dtype=np.uint8)
for i in range(0, nb_components):
if sizes[i] >= min_size:
img_to_compare = threshold_gray_const(output, (i + 1, i + 1))
img2 = binary_or(img2, img_to_compare)
img2 = img2.astype(np.uint8)
return img2
def threshold_gray_const(image_, rang: tuple):
return cv2.inRange(image_, rang[0], rang[1])
def binary_or(image_1, image_2):
return cv2.bitwise_or(image_1, image_2)
All you need to do is to call this function and give your binary image as first parameter and filter power as the second.
A bit explanation: Whole method - is simply iterating over objects on an image, and if the area of one of an object is less than the power, then it is simply removed.
I would give a try Median Filter (cv2.medianBlur) which should remove single pixels, but might also have other effect. You need to test it with few different settings and decide if it does provide you acceptable result.
Kernel size should be odd for Median Filter, thus median is used on odd number of pixels (9 for size 3, 25 for size 5, 49 for size 7 and so on), therefore Median Filter never introduces new value, thus if you use binary image as input, you will get binary image as output.

How to split a picture in numpy array format? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have an image of shape (31278,25794,3). I would like to know how is possible to obtain MxN segment of the picture, using np functions. For example starting from:
I would like to obtain:
In numpy you can split a picture like you slice an array.
Here's an example with your image:
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
img = np.array(Image.open("cat.jpg"))
plt.imshow(img)
xs = img.shape[0]//2 # division lines for the picture
ys = img.shape[1]//2
# now slice up the image (in a shape that works well with subplots)
splits = [[img[0:xs, 0:ys], img[0:xs, ys:]], [img[xs:, 0:ys], img[xs:, ys:]]]
fig, axs = plt.subplots(2, 2)
for i in range(2):
for j in range(2):
axs[i][j].imshow(splits[i][j])
Keep in mind that the splits here are views into the original array, not arrays with new data, so changes you make to the views will change the original data. If you don't want this, you can do something to copy the data after slice up the array.

How to plot a figure like Wikipedia? [closed]

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 4 years ago.
Improve this question
I'm trying to demonstrate a concept like Fourier Transform. While searching the web, I encountered an image in Wikipedia:
Is that possible to plot this figure in Python or MATLAB?
Have a look at the documentatin of plot3 and patch as well as some standard plot tools.
This code produces the following image:
t = 0:.01:2*pi;
x1 = 1/2*sin(2*t);
x2 = 1/3*sin(4*t);
x3 = 1/4*sin(8*t);
x4 = 1/6*sin(16*t);
x5 = 1/8*sin(24*t);
x6 = 1/10*sin(30*t);
step = double(x1>0);
step(step==0) = -1;
step = step*.5;
figure
hold on
plot3(t,ones(size(t))*0,step,'r')
plot3(t,ones(size(t))*1,x1,'b')
plot3(t,ones(size(t))*2,x2,'b')
plot3(t,ones(size(t))*3,x3,'b')
plot3(t,ones(size(t))*4,x4,'b')
plot3(t,ones(size(t))*5,x5,'b')
plot3(t,ones(size(t))*6,x6,'b')
plot3([2*pi+.5 2*pi+.5],[.5 6],[0 0],'b')
plot3([2*pi+.5 2*pi+.5],[1 1],[0 1/2],'b')
plot3([2*pi+.5 2*pi+.5],[2 2],[0 1/3],'b')
plot3([2*pi+.5 2*pi+.5],[3 3],[0 1/4],'b')
plot3([2*pi+.5 2*pi+.5],[4 4],[0 1/6],'b')
plot3([2*pi+.5 2*pi+.5],[5 5],[0 1/8],'b')
plot3([2*pi+.5 2*pi+.5],[6 6],[0 1/10],'b')
hold off
view([45,45])
patch([0 2*pi 2*pi 0 0],[0 0 0 0 0],[-1 -1 1 1 -1],'g','FaceAlpha',.3,'EdgeColor','none')
patch([2*pi+.5 2*pi+.5 2*pi+.5 2*pi+.5 2*pi+.5],[.5 6 6 .5 .5],[-1 -1 1 1 -1],'g','FaceAlpha',.3,'EdgeColor','none')
zlim([-1,1])
xlim([-.5,2*pi+.5])
ylim([-.5,6.5])
axis off
It could serve you as a start point.
Since you already read the article about fft I leave the red plot as an exercise to yourself ;-)
The function to plot 3D lines is plot3
The following code will produce the various lines
T=(0:.01:2).';
X = repmat(1:6,[length(T),1]);
phase = bsxfun(#times,T*2*pi,1:2:11);
Z = 4/pi*bsxfun(#rdivide,sin(phase),1:2:11);
Xsum = zeros(size(T));
Zsum = sum(Z,2);
figure;
plot3(X,T,Z,'b');
hold on
plot3(Xsum,T,Zsum,'r');
patch objects with an alpha channel can be used for the grey surfaces.
Xpatch=zeros(4,1);
Ypatch= [0 2 2 0].';
Zpatch= [2 2 -2 -2].';
patch(Xpatch,Ypatch,Zpatch,[.5 .5 .5],'FaceAlpha',.3,'EdgeColor',[.5 .5 .5]);
% patch(X,Y,Z,FaceColor_RGB_triplet,'Name','Value',...)
% FaceAlpha : transparency
% EdgeColor : RGB triplet for the edge
The same can be used to plot the frequency spectrum

Plot of a binary file in Python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a binary file from which I have to read data. The file consists of a 128x128x243 matrix (hex-formatted) which I have read with the following code:
with open("zubal_voxel_man.dat", "rb") as fileHandle:
dim_x = 128
dim_y = 128
dim_z = 243
data = np.zeros((dim_x,dim_y,dim_z), dtype=np.int)
for p in range(0, dim_x):
for q in range (0, dim_y):
for r in range(0, dim_z):
data[p][q][r] = ord(fileHandle.read(1))
How do I visualize these data with Python? Each x,y,z position has a value from 0 to 255 (grey scale) which I would like to render.
Any help is greatly appreciated!
Part of your problem is with the code:
datax = data[:,0]
datay = data[:,1]
dataz = data[:,2]
Which is not doing what you are expecting of slicing in a single axis it is taking a slice of the Y=0 then of Y=1, Y=2 and plotting them against each other - your other issue is that you have a 3 dimensional array of values which gives each value 4 dimensions X, Y, Z, Value - and you are trying to plot these into a surface. which only has 3 dimensions.
I think that your first priority is to clarify your what your data represents and how it is structured.

Categories