I have 2 images, with same dimension and same picture, a shape.
In the first image (that I show on screen), the shape is monocromatic, in the second image, shape is mapped with different color.
When I move the mouse, on the image, I want to show different text based on the color mapped on second (hidden) image.
I don't want to map square area, but irregular areas, this is my problem.
For example, when mouse cursor is on head (right image), I get color red on the left image (cached but not visualized) and I put a specific text.
How can I load second image an get pixel color? Gosu doesn't permit to get image info (only width and height).
Any ideas?
At the end I choose this GEM chunky_png, because it has no dependency from other gems or library and it's perfect to my needs:
#map_image = ChunkyPNG::Image.from_file('map_shape.png')
# Test pixel color
if #map_image[mouse_x - #main_image_x, mouse_y - #main_image_y] == ChunkyPNG::Color.rgb(255, 0,0)
# Do something
end
Related
How do I control the visual stacking order of images and colour bars in Matlab?
I have 6 images that I show on a 2 x 3 grid using subplot. I add colour bars to each of the images, but some of the colour bars appear behind the images, rendering them invisible. In my example, colour bars belonging to image 3 and 5 (counting as in the subplot command) appear behind their respective images.
I do not think I will be able to use the order of axes children objects, as each axes apparently only has one child, i.e. the image, and the colour bar object is child of the figure (Must be the same as its associated axes).
I have also tried uistack (with top for colour bars, and bottom for images), and although it does change appearances, in the end, other colour bars will wind up behind the images. It seems random which ones, for example after uistack on the image and colour bar belonging to subplot 5, the colour bar of subplot 2 will suddenly appear behind its image.
PS I also tried reordering the children of the figure, but they do not change (even with painters render)
It is very strange: When I just prepared the MCVE, the problem did not reproduce, but I have no idea what is different! Maybe a Windows update? Anyway, the original output with the problem is shown here http://i.stack.imgur.com/LJVG2.png, and the prepared example output which looks as it should here http://i.stack.imgur.com/F1LMX.png. So for now, there is no problem...
The code is
function example()
I1=ones(96,96);
I2=ones(96,96)+1;
I3=ones(96,96)+2;
I4=ones(96,96)+3;
I5=ones(96,96)+4;
I6=ones(96,96)+5;
pos=[10 10 12 9];
fig=figure
fig.Units='centimeters';
fig.Position=pos;
set(gcf,'PaperPositionMode','auto');
h(1)=veryTightSubPlot(2,3,1);
imshow(I1,[0,6]);
h(2)=veryTightSubPlot(2,3,2);
imshow(I2,[0,6]);
h(3)=veryTightSubPlot(2,3,3);
imshow(I3,[0,6]);
h(4)=veryTightSubPlot(2,3,4);
imshow(I4,[0,6]);
h(5)=veryTightSubPlot(2,3,5);
imshow(I5,[0,6]);
h(6)=veryTightSubPlot(2,3,6);
imshow(I6,[0,6]);
V=[0.315,0.65,0.97,0.315,0.65,0.97,0.315,0.65,0.97];
for i=1:6
p=get(h(i),'Position');
c=colorbar(h(i),'Axislocation','in','FontSize',8);
c.Color=[0,0,0];
cp=c.Position;
h(i).Position=p;
cp(3)=cp(3)/2;
c.Position=[V(i) cp(2:4)];
end
colormap(parula);
end
function [ax] = veryTightSubPlot(rows, columns,index)
ix = mod(index-1,columns);
iy = floor((index-1)/columns);
pos = [ix/columns,1-(iy+1)/rows,1/columns,1/rows];
ax = axes('ActivePositionProperty','outerposition','Position',pos);
end
my goal: have a background image which occupies the full window of the display. And then on it, place image buttons which will be nicely centered on this window (buttons being arranged in grid).
The image is 1920X1080.
I have other code snippet which follows exactly the same sequence and the image appears in background. So totally baffled. But wanted to understand the logic correctly of this code/instead of relying on luck!..Note that on the other working code, I do both a place and grid of the background_label !. With this code the buttons don't appear at all.
when I use background_label(content....), the image does appear in the background with buttons in front, but in that case the whole display window is not occupied. The image just expands to fill the area occupied by button images.
root = Tk()
root.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(), root.winfo_screenheight()))
content = ttk.Frame(root, padding=(3,3,12,12))
print(root.winfo_screenwidth(), root.winfo_screenheight())
#prints as 1920 & 1080
#old background_image_file='waves1600x926.gif'
background_image_file='Lake.gif' #this is 1080X1920
background_image=PhotoImage(file=background_image_file)
background_label =ttk.Label(root,image=background_image) #tried content instead of root also
background_label.place(x=0, y=0,relwidth=1, relheight=1)
background_label.grid()
logos=['abc','cbs','nbc','fox','cnbc','amc','bet']
logobuttons=defaultdict(str)
logoimgs=defaultdict(str)
for logo in logos:
logoimgs[logo]=PhotoImage(file=LOGODIR+logo+'.gif')
logobuttons[logo]=ttk.Button(content,image=logoimgs[logo])
content.grid(column=0, row=0, sticky=(N,S,E,W))
col=0
row=0
maxcols=5
for logo in logos:
logobuttons[logo].grid(row=row,column=col,pady=5,padx=5)
if col == maxcols:
col=0
row=row+1
else:
col=col+1
root.mainloop()
When you use both place and grid, only the last one you call for a given widget has any effect. Thus, when you call background_label.grid() it totally negates the effect of background_label.place(...). In other words, you need to remove the call to grid for this widget.
If you want a background image, the best solution is to use place with a relative width and height of 1. You should also create the widget that contains the image first, so it is lowest in the stacking order. Though, you can always lower it later.
I am working on a task in which I have some objects like humans and toys and food which are foreground images/objects and I have a background image, for eg. like a park. I need to place foreground images/objects at specified locations over background image. I am using Matlab.
I was able to place a foreground image over background image, but its positioned in the center - not at a specified position.
How can I place foreground images at specified locations over background image using Matlab ?
My code is as follows:
figure1 = figure;
ax1 = axes('Parent',figure1);
ax2 = axes('Parent',figure1);
set(ax1,'Visible','off');
set(ax2,'Visible','off');
[a,map,alpha] = imread('foreground.png');
I = imshow(a,'Parent',ax2);
set(I,'AlphaData',alpha);
imshow('Background.jpg','Parent',ax1);
My images:
1) What I want:
2) What I'm getting:
Here is a simple solution using ginput in which the user clicks on a figure (here only once) and you fetch the coordinates of the point(s). In this example, there is a message box asking the user to select a point, then the foreground image is drawn over the background image. Notice that in my example I replace the pixels from the background image with those of the foreground. i.e. I do not use alpha and transparency. Hope that's fine for you; if not please tell me.
In the example, my 'background' image is the peppers image that ships with Matlab and the 'foreground' image is the pears image that also ships with Matlab. I use a small portion of the image for the demonstration.
Here is the code:
clear
clc
close all
%// Set up background image (peppers.png) and foreground image (part of
%// pears.png)
BackgroundImage = imread('peppers.png');
DummyForeground = imread('pears.png');
ForegroundImage = DummyForeground(50:200,50:200,:);
%// Get size of foreground image
[rowFore,colFore,channelFore] = size(ForegroundImage);
figure
imshow(BackgroundImage);
hMsg = msgbox('Select an anchor point for foreground image','modal');
uiwait(hMsg)
This looks like this:
%// Use ginput to prompt user to select a single point (i.e. the 1 in
%// brackets).
[x,y] = ginput(1);
Calling ginput results in the following:
x = round(x);
y = round(y);
%// Important!
hold on
%// Replace pixels of background image with foreground image.
BackgroundImage(y:y+rowFore-1,x:x+colFore-1,:) = ForegroundImage;
imshow(BackgroundImage);
And finally the background image with the foreground image:
Note: It looks like there is a shift between the cursor and the actual image placed; that happened when I took the screenshot haha it's not a bug :)
Now if you would like to add many images in the foreground, you could easily modify the code using multiple points for ginput, calling it like so:
[x,y] = ginput %// Indefinite # of points)
or [x,y] = ginput(SomeNumber) %// Any number you want
and add appropriate images for every point you selected.
Hope that was clear enough and it gets you started!
I have read a image like that
a = imread('test.jpg');
image(a)
what the test.jpg is:
but after the image function
the result is:
and I don't know why it show that?
Because I want to crop some part, so I have to see the image shown.
How to fix it by showing the human face by image?
You are using image command to display an image. From here: "image creates an image graphics object by interpreting each element in a matrix as an index into the figure's colormap or directly as RGB values". Since you are providing a 2-D matrix, each element is interpreted as the index to the figures colormap. You can see the figure's colormap by using
c_map=colormap;
Also, the axis is set to square, therefore you see a circle instead of an ellipse. Use imshow(a,[]) to display the grayscale image as desired.
You image data might be in a color map instead. try [a,cmap] = imread(...). If cmap is not empty, a is indices into cmap, and cmap contains the actual colors.
Use img = cat(3,cmap(a,1),cmap(a,2),cmap(a,3)) to get your image, and show it with image(img).
Note that using imagesc might be misleading in this case as it will still show something that looks like your image when simply doing imagesc(a). This as different pixels colors are associated with different index-values in a.
In Mac OSX,
I have an image with black pixel in all 4 directions.
I want to programmatically crop the image to the maximum image rect.
Should i check for the black pixel and then create the crop rect or is there any supported API is there?
Create an NSImage of the desired size, lock focus on it, draw the desired crop rectangle of the source image into the whole bounds of the destination image, and unlock focus. The image you created now contains the crop from the source image.
Note that this will lose information like resolution (DPI), color profile, and EXIF tags. If you want to preserve those things (probably a good idea), use CGImage:
Use CGImageSource to load the image. Be sure to recover the properties of each image from the file, as well as the images themselves. And note that I used the plural: TIFF files can contain multiple images.
Use the CGImageCreateWithImageInRect function to crop out the desired section of each image. Don't forget to release each original image as appropriate.
If you want to write the cropped-out images to a file, do so using CGImageDestination. Pass both the images and the attributes dictionaries you obtained in step 1.