I have a matrix that I made an image of using image(matrix). Is there away to add a legend of the colors to my image like I do when adding a legend to plot?
Or the legend could be provided like this:
legend(grconvertX(0.5, "device"), grconvertY(1, "device"),
c("0",".5","1"), fill = colMap[c(1, 10, 20)], xpd = NA)
where grconvertX() and grconvertY() and xpd makes sure the legend is outside the plotting region.
A plausible example would be:
nsamples <- 20
mat <- rnorm(nsamples, .5, .15)
dim(mat) <- c(4, 5)
colMap <- colorRampPalette(c("red","white","blue" ))(nsamples)
image(1:4, 1:5, mat, col = colMap, ylab="", xlab="")
legend(grconvertX(0.5, "device"), grconvertY(1, "device"),
c("0",".5","1"), fill = colMap[c(1, 10, 20)], xpd = NA)
p.s.: I know it is an old request and it is solved. However I was looking for a similar answer and I could not find it. Since I bother solving this issue I thought maybe someone else could also benefit from it.
image in R is a fairly basic plotting function. You might want to look at filled.contour if you want a function that will automatically allocate space for a legend. Or try this:
library(lattice)
levelplot(matrix)
From the package fields, you could try image.plot. This function is based on the regular image, but it provides a figure legend.
library(fields)
x = 1:10
y = 1:15
z = outer( x,y,"+")
image.plot(x, y, z)
Related
Currently struggling with this code, cannot combine it, have been modifying it for hours. It's supposed to be a script that, when ran, shifts a picture horizontally and vertically. I have the code for the horizontal shift and the vertical shift, and separately, they both run and do what they're supposed to do, however, when I try to combine the two, I cannot get it to work at all. I have modified variables and looked at examples of doing this, but none of the examples have helped with combing the code successfully. Code is below.
Horizontal:
X=imread('photo1.jpg');
X_double=double(X);
X_gray = 0.3*X_double(:,:,1) + 0.3*X_double(:,:,2) + 0.3*X_double(:,:,3);
imagesc(uint8(X_gray))
colormap('gray')
[m,n]=size(X1);
r=240;
E=eye(n);
T=zeros(n,n);
T(:,1:r)=E(:,n-(r-1):n);
T(:,r+1:n)=E(:,1:n-r);
X_shift=X1*T;
imagesc(uint8(X_shift));
colormap('gray');
Vertical:
[m,n] = size(X1);
r = 100;
E = eye(m);
T = zeros(m);
T(1:r,:) = E(m-(r-1):m,:);
T(r+1:m,:) = E(1:m-r,:);
X_shift = T*X_gray ;
imagesc(uint8(X_shift));
colormap('gray');
Yes there are easier ways to do this, but this is how I'm supposed to be doing this. Any help would be appreciated, thank you.
You can use circshift to do that:
X=imread('peppers.png');
dx = -200;
dy = 100;
Y = circshift(X, [dy dx 0]);
imshow(Y)
As an additional note about your code, use rgb2gray for converting images to grayscale.
I would like to rotate an image in Love2D.
I have found a documentation on love2d.org: https://love2d.org/wiki/love.graphics.rotate
But I can't seem to get it to work when I try to load an image.
Heres my code:
local angle = 0
function love.load()
g1 = love.graphics.newImage("1.png")
end
function love.draw()
width = 100
height = 100
love.graphics.translate(width/2, height/2)
love.graphics.rotate(angle)
love.graphics.translate(-width/2, -height/2)
love.graphics.draw(g1, width, height)
end
function love.update(dt)
love.timer.sleep(10)
angle = angle + dt * math.pi/2
angle = angle % (2*math.pi)
end
Could anyone show me an simple example of rotating an image in love2d?
https://love2d.org/wiki/love.graphics.draw
You may be better off using the fourth argument, shown as 'r' to rotate images, such as:
love.graphics.draw(image, x, y, math.pi/4)
It's not worth the trouble of using the translate functions for a single draw, and keeping those for when you're batching many draws at once.
Your code worked perfectly for me, aside from a small unrelated issue (love.timer.sleep uses seconds in LÖVE 0.8.0).
We will be able to help you better, and perhaps reproduce your error, if you provide us with more information.
When you say
I can't seem to get it to work when I try to load an image
..what is the result?
Is the image a white box? Does the application crash? Is there nothing on the screen?
All of these imply a image loading issue, rather than a rotation issue. Although, it could be the case that the image is rotating off of the screen.
If you continue to use translate, rotate, and scale (which is usually a good idea), I recommend you take a look at the push and pop functions.
They allow you to 'stack' transformations so you can render sub elements.
Example uses are rendering a GUI (each child pushes its translation and then renders the children) and drawing sprites on a scrolling map (the camera translates the entire map and then does for entity in entities do push() entity:draw() pop() end. Each entity can translate and rotate in local coordinates (0,0 = centre of sprite)).
love.graphics.draw( drawable, x, y, r, sx, sy, ox, oy, kx, ky )
the R is the rotation.. why don't you just set it to a variable and change it as you please? ... I'm new to programming so I may be wrong but this is how I would do it.
Example of rotating at center of image using LOVE 11.3 (Mysterious Mysteries):
function love.draw()
love.graphics.draw(img, 400,300, wheel.r, wheel.sx, wheel.sy, wheel.w / 2, wheel.h / 2)
end
function love.update(dt)
wheel.r = wheel.r + dt
end
function love.load()
wheel = {x = 0, y = 0, w = 0, h = 0, sx = 0.5, sy = 0.5, r = 0, image = "wheel.png"}
img = love.graphics.newImage(wheel.image)
wheel.w = img:getWidth()
wheel.h = img:getHeight()
end
Normaly the axis for rotating is the upper left corner. To center the axis to the middle of an image you have to use the parameters after the r parameter to half of width and half of height of the image.
I'm new in R coding, so I need help or advise.
I'm building plot based on coordinates (originally this is eye tracking data). I`m exporting just dots without any axes or text. Later I will have to compare this dots with Image at which person was looking at.
So my aim is 1) to have exported image with very specific size (same to original image) and without any distortion (it seems to appear after manual entering of sizes in export dialog window). 2) to have this plot exported without extrafields, that obviously appers at every side.
Thank you in advance
UPD i`m thinking now if there is any way to move point of zero to left bottom corner of image? I was trying to work my problem out with cutting margins: par(mar=c(0, 0, 0, 0), xaxs='i', yaxs='i'), but it seems to cut all the empty parts of image. And I want to stay potentially working parts safe and keep size and proportions.
that is my source code
card1 <- "D:/01_experiment/02.04/p02/card1.txt"
table01 <- read.table(card1, strip.white = TRUE, sep="\t", header = TRUE)
jpeg('card81.jpg',
width = 842,
height = 595)
par(mar=c(0, 0, 0, 0), xaxs='i', yaxs='i')
plot(x = table01[,10],
y = table01[,11],
pch=20,
type="o",
#xaxt='n',
yaxt='n',
ann=FALSE,
bty="n",
xlab = "desity.x",
ylab = "desity.y"
)
dev.off()
Try this,
library(grid)
pdf("points.pdf", width=5, height=3)
grid.points(x <- runif(10), y <- runif(10), vp=dataViewport(x, y, extension = 0))
dev.off()
I have the following image on which i have applied bilateral filtering and subtracted it from my original image
Is it possible to outline the glass defect as shown
After apply Hough i got the following result not perfect :/
My matlab code :
im = imread('C:\Users\SUJIT\Desktop\image003.jpg');
im=rgb2gray(im);
h = fspecial('gaussian', size(im), 1.0);
g = imfilter(im, h);
im=im2double(g);
im=imadjust(im);
imgauss = imfilter(im, fspecial('gaussian',[7 7], 6),'conv');
imbi = bilateralfilter(im, [5 5], 3, 3);
imbi= im - imbi;
imshow(imbi,[]); title('Bilateral Filted Image');
I = imcrop(imbi, [30 30 450 350]);
J = imfilter(I, fspecial('gaussian', [17 17], 5), 'symmetric');
BW = edge(J, 'canny');
%# Hough Transform and show matrix
[H T R] = hough(BW);
imshow(imadjust(mat2gray(H)), [], 'XData',T, 'YData',R, ...
'InitialMagnification','fit')
xlabel('\theta (degrees)'), ylabel('\rho')
axis on, axis normal, hold on
colormap(hot), colorbar
%# detect peaks
P = houghpeaks(H, 10);
plot(T(P(:,2)), R(P(:,1)), 'gs', 'LineWidth',2);
%# detect lines and overlay on top of image
lines = houghlines(BW, T, R, P);
figure, imshow(I), hold on
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:,1), xy(:,2), 'g.-', 'LineWidth',2);
end
hold off
Please help am i doing something wrong here?
It is a bit hard to give a general answer based on only one image and no other information, but I can give a specific answer based on your sample image.
Assuming that what you want to find is the vertical blurry line in the middle of the image, this my approach. I won't go into specific implementation details, but only an outline on how I would do it.
Find the windows. There are multiple approaches to this. Some ideas are to either find the corners, or to find the rectangular structure itself. The Hough transform is a possible tool.
For each window, check if there are vertical structures in it.
I am plotting a 7x7 pixel 'image' in MATLAB, using the imagesc command:
imagesc(conf_matrix, [0 1]);
This represents a confusion matrix, between seven different objects. I have a thumbnail picture of each of the seven objects that I would like to use as the axes tick labels. Is there an easy way to do this?
I don't know an easy way. The axes properties XtickLabel which determines the labels, can only be strings.
If you want a not-so-easy way, you could do something in the spirit of the following non-complete (in the sense of a non-complete solution) code, creating one label:
h = imagesc(rand(7,7));
axh = gca;
figh = gcf;
xticks = get(gca,'xtick');
yticks = get(gca,'ytick');
set(gca,'XTickLabel','');
set(gca,'YTickLabel','');
pos = get(axh,'position'); % position of current axes in parent figure
pic = imread('coins.png');
x = pos(1);
y = pos(2);
dlta = (pos(3)-pos(1)) / length(xticks); % square size in units of parant figure
% create image label
lblAx = axes('parent',figh,'position',[x+dlta/4,y-dlta/2,dlta/2,dlta/2]);
imagesc(pic,'parent',lblAx)
axis(lblAx,'off')
One problem is that the label will have the same colormap of the original image.
#Itmar Katz gives a solution very close to what I want to do, which I've marked as 'accepted'. In the meantime, I made this dirty solution using subplots, which I've given here for completeness. It only works up to a certain size input matrix though, and only displays well when the figure is square.
conf_mat = randn(5);
A = imread('peppers.png');
tick_images = {A, A, A, A, A};
n = length(conf_mat) + 1;
% plotting axis labels at left and top
for i = 1:(n-1)
subplot(n, n, i + 1);
imshow(tick_images{i});
subplot(n, n, i * n + 1);
imshow(tick_images{i});
end
% generating logical array for where the confusion matrix should be
idx = 1:(n*n);
idx(1:n) = 0;
idx(mod(idx, n)==1) = 0;
% plotting the confusion matrix
subplot(n, n, find(idx~=0));
imshow(conf_mat);
axis image
colormap(gray)