绘制ROI区域的灰度直方图
环境:matlab+windows10
目标:绘制感兴趣区域的灰度直方图,可规则区域,也可非规则区域。
步骤:
首先,我们知道,matlab提供了一个绘制灰度直方图的函数 imhist
,其用法如下:
f = imread('test.jpg');
f = rgb2gray(f);% 如果输入是RGB图的话。否者注释掉该句
imhist(f)
输入图片:
输出直方图
正式讲ROI区域的直方图
首先我们需要给出需要绘制的区域,然后找到该区域的边界,然后根据边界绘图。
代码如下
f = imread('84.jpg');
f = rgb2gray(f); %如果是rgb图像的话
mask = roipoly(f);
g = bwperim(mask,8);
[x1,x2] = find(g);
[P,NPIX1] = histroi(f,x1,x2);
close all;
figure,plot(P)
其中histroi.m
定义如下:
function [p, npix] = histroi(f, c, r)
B = roipoly(f, c, r);
% Compute the histogram of the pixels in the ROI.
p = imhist(f(B));
% Obtain the number of pixels in the ROI if requested in the output.
if nargout > 1
npix = sum(B(:));
end
操作如下:
首先运行上面的程序,弹出图片,然后鼠标左键划定roi区域,然后右键结束。选定区域里面右键,创建mask。然后等一会儿,图就绘制出来了。
绘制的车部分:
The end!