150 likes | 297 Views
Unit 4 Cell Array and Structure. 中華技術學院電子系 副教授 蔡樸生. Cell Array Creation (II). A(i,j)={x}, and A{i,j}=x, both tell MATLAB to store the content of variable x in the (i,j) element of the cell array. A(i,j) is called cell indexing. A{i,j} is called content addressing. >>clear A
E N D
Unit 4 Cell Array and Structure 中華技術學院電子系 副教授 蔡樸生
Cell Array Creation (II) A(i,j)={x}, and A{i,j}=x, both tell MATLAB to store the content of variable x in the (i,j) element of the cell array. A(i,j) is called cell indexing. A{i,j} is called content addressing >>clear A >>A{1,1}=[1 2 3;4 5 6;7 8 9] >>A{1,2}=2+3i >>A{2,1}=‘A character string’ >>A{2,2}=12:-2:0 A= [3X3 double] [2.0000+3.0000i] ‘A character string ‘ [1X7 double]
Cell Array Creation (I) Cell arrays are MATLAB arrays whose elements are cells. Each cell in a cell array can hold any MATLAB data type including numerical arrays, character strings , other cell arrays, and structures. >>clear A >>A(1,1)={[1 2 3;4 5 6;7 8 9]} >>A(1,2)={2+3i} >>A(2,1)={‘A character string’} >>A(2,2)={12:-2:0}, A= [3X3 double] [2.0000+3.0000i] ‘A character string ‘ [1X7 double]
Cell Array Display The celldisp function forces MATLAB to display the Contents of cells in the usual manner. >>celldisp(A) >>A{2,2} : content addressing >>A(2,2) : cell indexing >>A{1,: } : content addressing of the first row >>A(1,: ) : cell indexing of the first row >>A{1,1}{1,1}, A{1,1}{1,2} }, A{1,1}{2,1} }, A{1,1}{2,2} >>cellplot(A) : Plot the structure diagram of cell >>size(A) : Display the dimension of cell >>cell(m,n) : create mxn empty cell array
Structure Creation Structures are like cell array. Instead of addressing elements by number, structure elements are addressed by names called fields. >>circle.radius = 2.5 >>circle.center = [0 1] >>circle.linestyle = ‘- -’ >>circle.color = ‘red’ The above data is stored in a structure variable circle. >>circle(2).radius = 3.4 >>circle(2).center = [2.3 -1.2] >>circle(2).linestyle = ‘-’ >>circle(2).color = ‘green’
Example A: Image Process (I) %vidobj = videoinput('winvideo'); %Create a video input object %im1 = getsnapshot(vidobj); %Immediately return a single image frame回傳影像的訊框 % im1 = imread('cap05.bmp'); load imm1;
Example A: Image Process (II) subplot('position',[0.1 0.1 0.8 0.8]); imshow(imm1); (2) Convert an RGB image to grayscale imm2 = rgb2gray(imm1); (3) Compute global image threshold using Otsu's method threshold = graythresh(imm2); (4)Convert an image to a binary image, based on threshold bw = im2bw(imm2,threshold); (5) Remove all object containing fewer than 30 pixels bw = bwareaopen(bw,30); (6) Fill a gap in the pen's cap, so that regionprops can be used to estimate se = strel('disk',2); bw = imclose(bw,se); bw = imfill(bw,‘holes’);
Example A: Image Process (III) (7) 找出圖的區域邊界資訊包含封閉區域的個數與邊點集合 [B,L] = bwboundaries(bw,'noholes'); subplot('position',[0.02 0 0.75 0.8]); imshow(label2rgb(L, @jet, [.5 .5 .5])) B = cell array [246x2 double] [140x2 double] [190x2 double] [193x2 double] [192x2 double] [223x2 double] [109x2 double]
Example A: Image Process (IV) (7)繪製物件的邊緣外框,其中length(B)是影像中所能辨識物件數 hold on for k = 1:length(B) boundary = B{k}; plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 2) end (8) 計算物體參數 status = regionprops(L,'Area','Centroid'); for k = 1:length(B) area = status(k).Area; centroid_x = status(k).Centroid(1); centroid_y = status(k).Centroid(2); end
Example B: File Read clear all; fid=fopen('PBN_0912.txt'); A=fscanf(fid,‘%x’,inf); %帶號16進制整數 fclose(fid); DATA_NUM=length(A); for k=1:DATA_NUM GPS_HEX{k}=dec2hex(A(k)); end
Home work : MAGIC DESIGN 魔術撲克牌在電腦上之實現 • h1=figure('position',[20 20 1000 700],'color',[0.6 0.7 0.8]); • X{1}=imread('IMAG0067.jpg'); • subplot(3,7,1), subimage(X{1});axis off;
Home work : MAGIC DESIGN X{1} X{2} X{3} X{4} X{5} X{6} X{7} X{8} X{9} X{10} X{11} X{12} X{13} X{14} X{15} X{16} X{17} X{18} X{19} X{20} X{21} 3k+1 3k+2 3k+3 k=0,1,……,6
Home work : MAGIC DESIGN X{1} X{4} X{7} X{10} X{13} X{16} X{19}X{3} X{6} X{9} X{12} X{15} X{18} X{21} X{2} X{5} X{8} X{11} X{14} X{17} X{20} 9k+1 9k+2 9k+3 k=0,1,……,6
Home work : MAGIC DESIGN X{4} X{13} X{3} X{12} X{21} X{8} X{17} X{1} X{10} X{19} X{9} X{18} X{5} X{14} X{7} X{16} X{6} X{15} X{2} X{11} X{20}
Home work : MAGIC DESIGN X{4} X{12} X{17} X{19} X{5} X{16} X{2} X{3} X{8} X{10} X{18} X{7} X{15} X{20} X{13} X{21} X{1} X{9} X{14} X{6} X{11}