如何显示图像的3D图形?关于MATLab

时间:2017-11-12 23:20:40

标签: matlab

clear all
clc
img = imread('TestImage.jpg');
frame_size = size(img);
allpixels = reshape(img, frame_size(1)*frame_size(2), frame_size(3))
[row,col] = size(allpixels);
totalPixels = 0;
count = 0;
white = 0;
black = 0;
red = 0;
green = 0;
blue = 0;
grey = 0;
purplepink = 0;
yelloworange = 0;
for row = 1:row
    count = 0;
    for col = 1:col
        if count == 0
            R = allpixels(row,col);
            count = count + 1;
        elseif count == 1
            G = allpixels(row,col);
            count = count + 1;
        elseif count == 2
            B = allpixels(row,col);
            count = count + 1;
        end
        totalPixels = totalPixels + 1;
    end
        if R > 230 && G > 230 && B > 230
            white = white + 1;
        elseif R < 70 && G < 70 && B < 70
            black = black + 1;
        elseif abs(R - G) <= 5 && abs(G - B) <= 5 && abs(R - B) <= 5
            grey = grey + 1;
        elseif R > B && R > G
            red = red + 1;
        elseif G > B && G >= R
            green = green + 1;
        elseif B >= R && B >= G
            blue = blue + 1;
        end
end

totalPixels = totalPixels/3;
totalPixels
white
black
grey
red
green
blue
white + black + grey + red + green + blue

PercentOfWhite = (white/totalPixels)* 100
A=PercentOfWhite;
PercentOfBlack = (black/totalPixels) * 100
B=PercentOfBlack;
PercentOfGrey = (grey/totalPixels)* 100
C=PercentOfGrey;
PercentOfRed = (red/totalPixels)* 100
D=PercentOfRed;
PercentOfGreen = (green/totalPixels) * 100
E=PercentOfGreen;
PercentOfBlue = (blue/totalPixels) * 100
F=PercentOfBlue;


[x,y]=meshgrid(1:15,1:15);
tri = delaunay(x,y);
z= peaks(15);
trisurf(tri,x,y,z)

上面的代码显示了一个3D图形。但是,它不显示任何有用的数据。例如,我插入了一个名为TestImage.jpg的图像。它是80%的红色,3D图表出现了。然后我尝试了另一张80%蓝色的图像,图表出现了,它看起来就像我之前测试过的图像!我得出结论,我的代码不是读取任何图像,它只是随机显示3D,没有真正的数据结果。关于如何修复我的代码以显示实际读取我的图像并显示有用数据的3D图形的任何建议

1 个答案:

答案 0 :(得分:0)

我测试了你的代码,看起来你脚本的最后4行没有使用上面的任何变量。这意味着在最后4个代码行之前发生的事情对于3D图形无关紧要,因为它是分开的。

如果使用以下代码替换最后4行,则会出现一个图表,并显示代表颜色百分比的条形码:

names = categorical({'PercentOfWhite' 'PercentOfBlack' 'PercentOfGrey' 'PercentOfRed' 'PercentOfGreen' 'PercentOfBlue'});
bar(names,[PercentOfWhite PercentOfBlack PercentOfGrey PercentOfRed PercentOfGreen PercentOfBlue]);

希望我能提供帮助。