Mapping image into cylinder or sphere shape? - image

So lets say I have black & white image that is read with imread() command and saved into matrix A.
I want to output/graph this matrix A image in a cylinder shape. I know how to draw a cylinder in MATLAB, but I do not have a clue what I should do if I want to put image on a cylinder or draw image in cylinder shape. Any help will be appreciated. Thank you.
I found this site from googling.
http://www.flashandmath.com/advanced/rolls/cylin.html
This is exactly what I want to do, but I need to do this in MATLAB.

The technique is called texture mapping. This is a code example from surface function (R2011b):
load clown
surface(peaks,flipud(X),...
'FaceColor','texturemap',...
'EdgeColor','none',...
'CDataMapping','direct')
colormap(map)
view(-35,45)
This example loads RGB image from "peppers.png" and maps it onto cylinder:
imgRGB = imread('peppers.png');
[imgInd,map] = rgb2ind(imgRGB,256);
[imgIndRows,imgIndCols] = size(imgInd);
[X,Y,Z] = cylinder(imgIndRows,imgIndCols);
surface(X,Y,Z,flipud(imgInd),...
'FaceColor','texturemap',...
'EdgeColor','none',...
'CDataMapping','direct')
colormap(map)
view(-35,45)
Things are even simpler with the warp function (comes with Image Processing toolbox) as natan suggested:
imgRGB = imread('peppers.png');
[imgRows,imgCols,imgPlanes] = size(imgRGB);
[X,Y,Z] = cylinder(imgRows,imgCols);
warp(X,Y,Z,imgRGB);

Related

is it possible in Unity to slice image from the middle?

I get an image from the backend which I want to put in a puzzle shuffle game, however right now it is cropping from left to almost middle of the image , I would like to crop the center of the image, I was trying to use another Sprite which I cropped to the middle then use it in the ImageSlicer however I am getting same results.
Here are the images :
Main Image , Cropped Image as temp , Sliced Image as imagesSlices
am I doing the right thing and maybe I'm messing something in the code or this isn't how I should crop and slice ?
Sprite temp = Sprite.Create(currentImage, new Rect(currentImage.width * 0.25f, 0, currentImage.width * 0.75f, currentImage.height),new Vector2(0.5f, 0.5f), 100.0f);
Texture2D[,] imageSlices = ImageSlicer.GetSlices(temp.texture, blocksPerLine);
So the texture was staying the same and I was trying to get a new sprite not a new texture but I need a new cropped texture to work with which is why the sliced images were taken from old texture.

How to matched object from main image with the template sub-Image using normxcorr2

I try to determine the coordinates of a puzzle piece on the original image using the normxcorr2 function. Then I draw a rectangle on the correspondence of the two elements. Unfortunately, I notice that the coordinates that this match has given me are not good. Could someone have an idea how to improve the use of this feature and get some better results.
The puzzle piece has the name "cpiece" and the original picture has the name "bild"
clear all;
close all;
clc
cpiece = im2gray(imread('cpiece1.jpg'));
bild = im2gray(imread('original.jpg'));
figure(1)
montage({bild,cpiece})
c = normxcorr2(cpiece,bild);
figure(2)
surf(c)
shading flat
[ypeak,xpeak] = find(c==max(c(:)));
yoffSet = ypeak-size(cpiece,1);
xoffSet = xpeak-size(cpiece,2);
figure(3)
imshow(bild)
drawrectangle(gca,'Position',[xoffSet,yoffSet,size(cpiece,2),size(cpiece,1)],'FaceAlpha',0);
It seems, the problem returns back to the quality of your template image, And Check if the scales between original image and the template are exactly the same

Writing a greyscale video using Videowriter/avifile

I am writing a function that generates a movie mimicking a particle in a fluid. The movie is coloured and I would like to generate a grayscaled movie for the start. Right now I am using avifile instead of videowriter. Any help on changing this code to get grayscale movie? Thanks in advance.
close all;
clear variables;
colormap('gray');
vidObj=avifile('movie.avi');
for i=1:N
[nx,ny]=coordinates(Lx,Ly,Nx,Ny,[x(i),-y(i)]);
[xf,yf]=ndgrid(nx,ny);
zf=zeros(size(xf))+z(i);
% generate a frame here
[E,H]=nfmie(an,bn,xf,yf,zf,rad,ns,nm,lambda,tf_flag,cc_flag);
Ecc=sqrt(real(E(:,:,1)).^2+real(E(:,:,2)).^2+real(E(:,:,3)).^2+imag(E(:,:,1)).^2+imag(E(:,:,2)).^2+imag(E(:,:,3)).^2);
clf
imagesc(nx/rad,ny/rad,Ecc);
writetif(Ecc,i);
if i==1
cl=caxis;
else
caxis(cl)
end
axis image;
axis off;
frame=getframe(gca);
cdata_size = size(frame.cdata);
data = uint8(zeros(ceil(cdata_size(1)/4)*4,ceil(cdata_size(2)/4)*4,3));
data(1:cdata_size(1),1:cdata_size(2),1:cdata_size(3)) = [frame.cdata];
frame.cdata = data;
vidObj = addframe(vidObj,frame);
end
vidObj = close(vidObj);
For your frame data, use rgb2gray to convert a colour frame into its grayscale counterpart. As such, change this line:
data(1:cdata_size(1),1:cdata_size(2),1:cdata_size(3)) = [frame.cdata];
To these two lines:
frameGray = rgb2gray(frame.cdata);
data(1:cdata_size(1),1:cdata_size(2),1:cdata_size(3)) = ...
cat(3,frameGray,frameGray,frameGray);
The first line of the new code will convert your colour frame into a single channel grayscale image. In colour, grayscale images have all of the same values for all of the channels, which is why for the second line, cat(3,frameGray,frameGray,frameGray); is being called. This stacks three copies of the grayscale image on top of each other as a 3D matrix and you can then write this frame to your file.
You need to do this stacking because when writing a frame to file using VideoWriter, the frame must be colour (a.k.a. a 3D matrix). As such, the only workaround you have if you want to write a grayscale frame to the file is to replicate the grayscale image into each of the red, green and blue channels to create its colour equivalent.
BTW, cdata_size(3) will always be 3, as getframe's cdata structure always returns a 3D matrix.
Good luck!

segment object(leaf) which is on the white paper using image processing

I want to get only leaf from an image.
The background is a normal white paper(A4) and there is some shadow.
I apply some method (structure element,edge detection using filter) but I cannot find the general way which can apply all the image.
these are examples.
Are there better methods for this problem??
thank you
another example.
and the result I got is
By using
hsv_I = rgb2hsv(I);
Is = hsv_I(:,:,2);
Is_d = imdilate(Is,strel('diamond',4));
Is_e = imerode(Is,strel('diamond',2));
Is_de = imerode(Is_d,strel('disk',2));
Is_def = imfill(Is_de,'holes');
Is_defe = imerode(Is_def,strel('disk',5));
Then Is_defe is a mask to segment
But the method that i did is very specific. I cannot use this in general.
If you have the Image Processing Toolbox, you could do as follows:
The code below first estimates the threshold with the function graythresh, thresholds the image and fills holes with the imfill function. Suppose I is a cell containing your RGB images:
for k=1:length(I)
t=graythresh(rgb2gray(I{k}));
BW{k}=imfill(~im2bw(I{k}, t), 'holes');
subplot(length(I),1,k), imshow(BW{k});
end

Display image using GUI MATLAB in a specific region

I'm looking for how can I display an image in my GUI in a specific region of my interface.
In GUIDE, you can draw axes into the GUI. Then, in a callback function, you can plot an image into the axes.
Personally, I would rather not have the image inside the GUI, because it makes it harder to resize everything properly, and if you want to have a closer look at the image, or capture it to paste into another application, having the figure as part of the GUI can be inconvenient. Thus, I prefer to have the image open in a separate figure window.
Try the following code:
function movingimage
%# Plotting a figure
fig1=figure('Name','Plotting an image',...
'Unit','normalized', 'Position',[.1 .1 .8 .8]);
uicontrol(fig1,'Style','text','Unit','Normalized',...
'Position',[.9 .85 .1 .07],'String','Press the button below to move the image location.');
uicontrol(fig1,'Style','pushbutton','Unit','Normalized',...
'Position',[.9 .8 .05 .05],'String','Move','Callback',{#action_Callback});
%# Say, you wish to plot an image of relative dimension (.3 x .3) to the figure.
xdim=.3; ydim=.3;
%# Image's movable range in x is (1 - xdim)
dx=1-xdim;
%# Image's Movable range in y is (1 - ydim)
dy=1-ydim;
%# considering the size of the image...
pos = [.5*dx .5*dy xdim ydim]; %# Initial location of the image is at the center of the figure.
ax1 = axes('position',pos);
img = load('mandrill');
image(img.X)
colormap(img.map);axis off;axis equal;
function action_Callback(hObj,eventdata)
pos=[rand(1)*dx rand(1)*dy xdim ydim];
set(ax1,'position',pos);
end
end
The most direct and easy way I find to do this is to use the Axis component as shown in this tutorial:
http://www.aboutcodes.com/2012/06/how-to-display-image-in-gui-using.html

Resources