- UID
- 128544
- 积分
- 0
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2004-4-22
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
各位可否幫幫忙........會不會把上面那個花瓶變成3D的MODE....我已經做了很久.....還是運行不了,可以可幫幫忙, 看看我的代码有沒有錯....謝謝謝謝謝謝
[CODE]
function vase;
%/*this function generate a vector model of the profile of the bottle*/
close all;
%read in the image and turn to gery scale image
I = rgb2gray(imread('vase.jpeg'));
imshow(I);
%adjust the contrast to avoid edge the showdaw of the bottle
I = imadjust(I,[],[], 0.15);
figure, imshow(I)
%set up the best threshold
level = 0.1
BW1 = edge(I,'canny', level);
figure, imshow(BW1);
% remove spur pixels
BW2 = bwmorph(BW1, 'spur', 2);
figure, imshow(BW2), title('bwmorph--spur')
%using the closing technique to fill in the bottle
BW3 = bwmorph(BW2, 'dilate', 40);
figure, imshow(BW3), title('bwmorph--dilate')
BW4 = bwmorph(BW3, 'erode', 40);
figure, imshow(BW4), title('bwmorph--erode')
%remove all the piexls expect the boundary of the bottle
BW = bwmorph(BW4, 'remove', inf);
figure, imshow(BW), title('bwmorph--remove')
%rotate the image by 180 to let the sweep generator working properly
BW = rot90(rot90(BW));
figure, imshow(BW);
%find out the centroid of the bottle
theCentroid = regionprops(BW, 'Centroid');
theCentroid = round([theCentroid.Centroid]);
%find all the nodes of the whole bottle
[Y X] = find(BW);
% %select the peixels of the left half bottle
bufferIndex = find(X >=theCentroid(1));
X = X(bufferIndex);
Y = Y(bufferIndex);
%adjust the coordinates
%subtracting the min.vaule of X element to set the orgin start from 0
X = abs(X - min(X));
%adjust the position of the bottle to touch the ground
Y = Y - min(Y);
%Sort elements of X Y in ascending order of Y
[bufferY, bufferIndex] = sort(Y);
X = X(bufferIndex);
Y = Y(bufferIndex);
%reduce the unncessary piexls
numOfCoordinate = size(X,1);
lastIndex = numOfCoordinate;
currIndex = 1;
while (currIndex < lastIndex)
currIndex2 = 1;
while (currIndex2 < lastIndex)
%remove the pixel if the X or Y is same as the neighborhood
%as it means it is not the essential pixel to describe the bottle
if (X(currIndex2)==X(currIndex2+1)) | (Y(currIndex2)==Y(currIndex2+1))
%remove the pixel
X(currIndex2) = [];
Y(currIndex2) = [];
lastIndex = lastIndex-1;
else
currIndex2 = currIndex2 +1;
end
end
lastIndex = lastIndex-currIndex;
end
%repair the hole of the bottom of the bottle
X = [X; 0];
Y = [Y; min(Y)+1];
numOfCoordinate = size(X,1);
%call part3 function to sweep generate a 3D bottle
sweep(X,Y,numOfCoordinate);
function sweep(X,Y,numOfCoordinate);
%/*this function sweeps generate a 3D bottle*/
%set the num of nodes for the sweep generator
n=50;
%calculate the number of nodes and patches
numnodes = numOfCoordinate*n;
numpatch = (numOfCoordinate-1)*n;
%calculate the node values
for i = 0:n-1
for j = 1:numOfCoordinate
theta = 2*i*pi/n;
Node(j+numOfCoordinate*i,1) = X(j)*cos(theta);
Node(j+numOfCoordinate*i,2) = X(j)*sin(theta);
Node(j+numOfCoordinate*i,3) = Y(j);
end
end
%assign the nodes to the patches
for i = 1:n
term = numOfCoordinate*(i-1);
for j = 1:(numOfCoordinate-1)
FaceNode(((numOfCoordinate-1)*(i-1))+j,:) = [(term+j) (term+j+1) (term+j+1+numOfCoordinate) (term+j+numOfCoordinate)];
end
end
%connects the last profile to the first profile
for i = numpatch-(numOfCoordinate-2):numpatch
FaceNode(i,3) = FaceNode(i,3)-numnodes;
FaceNode(i,4) = FaceNode(i,4)-numnodes;
end
%displays the 3D bottle
figure, patch('Vertices',Node,'Faces',FaceNode,'facevertexCdata',[0.2 0.2 1],'facecolor','black', 'EdgeColor','none')
light('Position',[100 -100 0],'Style','infinite','color','white');
%the lighting effects
lighting phong
axis square;
axis([-170, 170, -170, 170, 0, 350]);
rotate3d on;
[CODE] |
|