【计算机视觉】【矿泉水瓶水位测量】--Matlab与C++实现



对水瓶水位的标定,主要运用二值化与检查对比度的方法:

Matlab程序:

clc;
clear all
I=imread('D:\14.jpg');
G=rgb2gray(I); 
[a,b]=size(G);
G1=histeq(G);
imwrite((G),'14.1.bmp');
%%%%%%%%%%%读取直方图向量
H=imhist(G);
%自编写直方图均衡化
H=H./(a*b);
HH(1)=H(1);
for i=2:1:256
   HH(i)=HH(i-1)+H(i);
end
HH=HH'*255;
HH=round(HH);
for i=1:1:a
    for j=1:1:b
        for k=0:1:255
            if G(i,j)==k
                G(i,j)=HH(k);
            end
        end
    end  
end
imwrite((G),'14.2.bmp');
G=filter2(fspecial('average',2),G);%3×3均值滤波
imwrite((G),'14.3.bmp');
%%%%%%%%%%%%%%%%%%图像2值化
for i=1:1:a
    for j=1:1:b
       if G(i,j)<=25
           G(i,j)=255;
       else
              G(i,j)=0;    
       end
    end
end
imwrite((G),'14.4.bmp');
%%%%%%%%%%%%%%%%%%%判断对比度
i=1;
a1=500;
b1=b-500;
for a=a1:2:b1
    aa(i)=sum(G(a,:))+sum(G(a+1,:));
    i=i+1;
end
%%%%%%%%%%%%%%%%%%%相减
i=1;
x=size(aa,2);
for s=1:1:x-1
   aaa(i)=aa(s+1)-aa(s);
   i=i+1; 
end
%%%%%%%%%%%%%%%%%%%找寻对比度最大处
aaa=abs(aaa);
x=find(aaa==max(aaa));
xx=2*x+500;
%%%%%%%%%%%%%%%%%%%找寻精确水位处(暂时隐藏)
I(xx-15:xx+15,:,2:3)=0;
imshow(I)
imwrite((I),'14.5.bmp');

C++程序

//计算灰度直方图///
int grayCount[256];
for (int a = 0; a < 256; a++)
	grayCount[a] = 0;
for (int i = 0; i < height; i++)
{
	for (int j = 0; j < width; j++)
	{
		for (int grayValue = 0; grayValue<256; grayValue++)
			grayCount[grayValue]++;
	}
}
//直方图均衡化
double grayCountper[256] = {};
for (int b = 0; b < 256; b++)
{
	grayCountper[b] = grayCount[b] / (height*width);///平均
}
for (int i1 = 2; i1 < 256; i1++)
{
	grayCountper[i1] = grayCountper[i1 - 1] + grayCountper[i1];
}
//计算灰度概率
int (* buff )[2448]=(int (*) [2448]) calloc (3264 * 2448, sizeof(int));
int grayCountper1[256] = {};
for (int l = 0; l<256; l++)
{
	grayCountper1[l] = round(grayCountper[l] * 255);

}
for (int i2 = 0; i2

【计算机视觉】【矿泉水瓶水位测量】--Matlab与C++实现_第1张图片

你可能感兴趣的:(计算机视觉,Matlab,C++)