找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 322|回复: 0

[求助]:各位可否幫幫忙.會不會把花瓶變成3D.還是運行不了..這個星期要交了.

[复制链接]
发表于 2004-4-22 08:18:56 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
各位可否幫幫忙........會不會把上面那個花瓶變成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]
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-11-18 10:34 , Processed in 0.358786 second(s), 31 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表