找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 386|回复: 0

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

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

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

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

×

                               
登录/注册后可看大图


各位可否幫幫忙........會不會把上面那個花瓶變成3D的MODE....我已經做了很久.....還是運行不了,可以可幫幫忙, 看看我的代码有沒有錯....謝謝謝謝謝謝
  1. [FONT =courier new]
  2. function vase;
  3. %/*this function generate a vector model of the profile of the bottle*/
  4. close all;

  5. %read in the image and turn to gery scale image
  6. I = rgb2gray(imread('vase.jpeg'));
  7. imshow(I);

  8. %adjust the contrast to avoid edge the showdaw of the bottle
  9. I = imadjust(I,[],[], 0.15);
  10. figure, imshow(I)

  11. %set up the best threshold
  12. level = 0.1
  13. BW1 = edge(I,'canny', level);
  14. figure, imshow(BW1);

  15. % remove spur pixels
  16. BW2 = bwmorph(BW1, 'spur', 2);
  17. figure, imshow(BW2), title('bwmorph--spur')

  18. %using the closing technique to fill in the bottle
  19. BW3 = bwmorph(BW2, 'dilate', 40);
  20. figure, imshow(BW3), title('bwmorph--dilate')
  21. BW4 = bwmorph(BW3, 'erode', 40);
  22. figure, imshow(BW4), title('bwmorph--erode')

  23. %remove all the piexls expect the boundary of the bottle
  24. BW = bwmorph(BW4, 'remove', inf);
  25. figure, imshow(BW), title('bwmorph--remove')



  26. %rotate the image by 180 to let the sweep generator working properly
  27. BW = rot90(rot90(BW));
  28. figure, imshow(BW);

  29. %find out the centroid of the bottle
  30. theCentroid = regionprops(BW, 'Centroid');
  31. theCentroid = round([theCentroid.Centroid]);

  32. %find all the nodes of the whole bottle
  33. [Y X] = find(BW);

  34. % %select the peixels of the left half bottle
  35. bufferIndex = find(X >=theCentroid(1));
  36. X = X(bufferIndex);
  37. Y = Y(bufferIndex);

  38. %adjust the coordinates
  39. %subtracting the min.vaule of X element to set the orgin start from 0
  40. X = abs(X - min(X));
  41. %adjust the position of the bottle to touch the ground
  42. Y = Y - min(Y);

  43. %Sort elements of X Y in ascending order of Y
  44. [bufferY, bufferIndex] = sort(Y);
  45. X = X(bufferIndex);
  46. Y = Y(bufferIndex);

  47. %reduce the unncessary piexls
  48. numOfCoordinate = size(X,1);
  49. lastIndex = numOfCoordinate;
  50. currIndex = 1;
  51. while (currIndex < lastIndex)
  52.     currIndex2 = 1;
  53.     while (currIndex2 < lastIndex)
  54.         %remove the pixel if the X or Y is same as the neighborhood
  55.         %as it means it is not the essential pixel to describe the bottle
  56.         if (X(currIndex2)==X(currIndex2+1)) | (Y(currIndex2)==Y(currIndex2+1))
  57.             %remove the pixel
  58.             X(currIndex2) = [];   
  59.             Y(currIndex2) = [];
  60.             lastIndex = lastIndex-1;
  61.         else
  62.             currIndex2 = currIndex2 +1;
  63.         end
  64.     end
  65.     lastIndex = lastIndex-currIndex;
  66. end   
  67. %repair the hole of the bottom of the bottle
  68. X = [X; 0];
  69. Y = [Y; min(Y)+1];

  70. numOfCoordinate = size(X,1);

  71. %call part3 function to sweep generate a 3D bottle
  72. sweep(X,Y,numOfCoordinate);

  73. function sweep(X,Y,numOfCoordinate);   
  74. %/*this function sweeps generate a 3D bottle*/

  75. %set the num of nodes for the sweep generator
  76. n=50;
  77. %calculate the number of nodes and patches
  78. numnodes = numOfCoordinate*n;
  79. numpatch = (numOfCoordinate-1)*n;

  80. %calculate the node values
  81. for i = 0:n-1
  82.     for j = 1:numOfCoordinate
  83.         theta = 2*i*pi/n;
  84.         Node(j+numOfCoordinate*i,1) = X(j)*cos(theta);
  85.         Node(j+numOfCoordinate*i,2) = X(j)*sin(theta);
  86.         Node(j+numOfCoordinate*i,3) = Y(j);
  87.     end
  88. end

  89. %assign the nodes to the patches
  90. for i = 1:n
  91.     term = numOfCoordinate*(i-1);
  92.     for j = 1:(numOfCoordinate-1)
  93.         FaceNode(((numOfCoordinate-1)*(i-1))+j,:) = [(term+j) (term+j+1) (term+j+1+numOfCoordinate) (term+j+numOfCoordinate)];
  94.     end
  95. end

  96. %connects the last profile to the first profile
  97. for i = numpatch-(numOfCoordinate-2):numpatch
  98.     FaceNode(i,3) = FaceNode(i,3)-numnodes;
  99.     FaceNode(i,4) = FaceNode(i,4)-numnodes;
  100. end

  101. %displays the 3D bottle
  102. figure, patch('Vertices',Node,'Faces',FaceNode,'facevertexCdata',[0.2 0.2 1],'facecolor','black', 'EdgeColor','none')
  103. light('Position',[100 -100 0],'Style','infinite','color','white');

  104. %the lighting effects
  105. lighting phong

  106. axis square;
  107. axis([-170, 170, -170, 170, 0, 350]);
  108. rotate3d on;
  109. [/FONT]
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-11-18 12:16 , Processed in 0.181935 second(s), 31 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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