实验主要使用的函数
MATLAB内部常数pi:圆周率 p(= 3.1415926...)
MATLAB常用基本数学函数:
abs(x):纯量的绝对值或向量的长度;
round(x):四舍五入至最近整数;
floor(x):地板函数,即舍去正小数至最近整数;
MATLAB常用三角函数
sin(x):正弦函数
cos(x):余弦函数;
向量的常用函数
max(x): 向量x的元素的最大值。
MATLAB图像类型转换函数:
rgb2gray:将一副真彩色图像转换成灰度图像;
im2bw:通过设定高度阈值将真彩色,索引色,灰度图转换成二值图像;
MATLAB图形图像文件的读取和显示函数
imread(filename);
MATLAB二进制图像及其显示
imshow(f1)。
用double对二值图像双精度化
图形处理:
sobel算子检测边缘
hough变换检测圆
分别显示灰度图像:
figuresubplot
Sobel:算子边缘检测图像
hough变换检测后的图像
实验相关代码
I=imread('*.jpg')f=rgb2gray(I)
f1=im2bw(f,200/255)
BW1=double(f1)
BW=edge(BW1,'sobel',0.4)
r_max=50
r_min=10step_r=10step_angle=pi/12p=0.7
[m,n] = size(BW)
size_r = round((r_max-r_min)/step_r)+1
size_angle = round(2*pi/step_angle)
hough_space = zeros(m,n,size_r)
[rows,cols] = find(BW)
ecount = size(rows)
for i=1:ecount
for r=1:size_r
for k=1:size_angle
a = round(rows(i)-(r_min+(r-1)*step_r)*cos(k*step_angle))
b = round(cols(i)-(r_min+(r-1)*step_r)*sin(k*step_angle))
if(a>0&&a<=m&&b>0&&b<=n)
hough_space(a,b,r) = hough_space(a,b,r)+1
end
end
end
end
max_para = max(max(max(hough_space)))
index = find(hough_space>=max_para*p)
length = size(index)
hough_circle = false(m,n)
for i=1:ecount
for k=1:length
par3 = floor(index(k)/(m*n))+1
par2 = floor((index(k)-(par3-1)*(m*n))/m)+1
par1 = index(k)-(par3-1)*(m*n)-(par2-1)*m
if((rows(i)-par1)^2+(cols(i)-par2)^2<(r_min+(par3-1)*step_r)^2+5&&...
(rows(i)-par1)^2+(cols(i)-par2)^2>(r_min+(par3-1)*step_r)^2-5)
hough_circle(rows(i),cols(i)) = true
end
end
end
for k=1:length
par3 = floor(index(k)/(m*n))+1
par2 = floor((index(k)-(par3-1)*(m*n))/m)+1
par1 = index(k)-(par3-1)*(m*n)-(par2-1)*m
par3 = r_min+(par3-1)*step_r
fprintf(1,'Center %d %d radius %d\n',par1,par2,par3)
para(:,k) = [par1,par2,par3]
end
subplot(221),imshow(f)
subplot(222),imshow(BW)
subplot(223),imshow(hough_circle)
代码如下:function [accum, varargout] = CircularHough_Grd(img, radrange, varargin)
%Detect circular shapes in a grayscale image. Resolve their center
%positions and radii.
%
% [accum, circen, cirrad, dbg_LMmask] = CircularHough_Grd(
% img, radrange, grdthres, fltr4LM_R, multirad, fltr4accum)
% Circular Hough transform based on the gradient field of an image.
% NOTE:Operates on grayscale images, NOT B/W bitmaps.
% NO loops in the implementation of Circular Hough transform,
% which means faster operation but at the same time larger
% memory consumption.
%
%%%%%%%% INPUT: (img, radrange, grdthres, fltr4LM_R, multirad, fltr4accum)
%
% img: A 2-D grayscale image (NO B/W bitmap)
%
% radrange:The possible minimum and maximum radii of the circles
% to be searched, in the format of
% [minimum_radius , maximum_radius] (unit: pixels)
% **NOTE**: A smaller range saves computational time and
% memory.
%
% grdthres:(Optional, default is 10, must be non-negative)
% The algorithm is based on the gradient field of the
% input image. A thresholding on the gradient magnitude
% is performed before the voting process of the Circular
% Hough transform to remove the 'uniform intensity'
% (sort-of) image background from the voting process.
% In other words, pixels with gradient magnitudes smaller
% than 'grdthres' are NOT considered in the computation.
% **NOTE**: The default parameter value is chosen for
% images with a maximum intensity close to 255. For cases
% with dramatically different maximum intensities, e.g.
% 10-bit bitmaps in stead of the assumed 8-bit, the default
% value can NOT be used. A value of 4% to 10% of the maximum
% intensity may work for general cases.
%
% fltr4LM_R: (Optional, default is 8, minimum is 3)
% The radius of the filter used in the search of local
% maxima in the accumulation array. To detect circles whose
% shapes are less perfect, the radius of the filter needs
% to be set larger.
%
% multirad: (Optional, default is 0.5)
% In case of concentric circles, multiple radii may be
% detected corresponding to a single center position. This
% argument sets the tolerance of picking up the likely
% radii values. It ranges from 0.1 to 1, where 0.1
% corresponds to the largest tolerance, meaning more radii
% values will be detected, and 1 corresponds to the smallest
% tolerance, in which case only the "principal" radius will
% be picked up.
%
% fltr4accum: (Optional. A default filter will be used if not given)
% Filter used to smooth the accumulation array. Depending
% on the image and the parameter settings, the accumulation
% array built has different noise level and noise pattern
% (e.g. noise frequencies). The filter should be set to an
% appropriately size such that it's able to suppress the
% dominant noise frequency.
%
%%%%%%%% OUTPUT: [accum, circen, cirrad, dbg_LMmask]
%
% accum: The result accumulation array from the Circular Hough
% transform. The accumulation array has the same dimension
% as the input image.
%
% circen: (Optional)
% Center positions of the circles detected. Is a N-by-2
% matrix with each row contains the (x, y) positions
% of a circle. For concentric circles (with the same center
% position), say k of them, the same center position will
% appear k times in the matrix.
%
% cirrad: (Optional)
% Estimated radii of the circles detected. Is a N-by-1
% column vector with a one-to-one correspondance to the
% output 'circen'. A value 0 for the radius indicates a
% failed detection of the circle's radius.
%
% dbg_LMmask: (Optional, for debugging purpose)
% Mask from the search of local maxima in the accumulation
% array.
%
%%%%%%%%% EXAMPLE #0:
% rawimg = imread('TestImg_CHT_a2.bmp')
% tic
% [accum, circen, cirrad] = CircularHough_Grd(rawimg, [15 60])
% toc
% figure(1)imagesc(accum)axis image
% title('Accumulation Array from Circular Hough Transform')
% figure(2)imagesc(rawimg)colormap('gray')axis image
% hold on
% plot(circen(:,1), circen(:,2), 'r+')
% for k = 1 : size(circen, 1),
% DrawCircle(circen(k,1), circen(k,2), cirrad(k), 32, 'b-')
% end
% hold off
% title(['Raw Image with Circles Detected ', ...
% '(center positions and radii marked)'])
% figure(3)surf(accum, 'EdgeColor', 'none')axis ij
% title('3-D View of the Accumulation Array')
%
% COMMENTS ON EXAMPLE #0:
% Kind of an easy case to handle. To detect circles in the image whose
% radii range from 15 to 60. Default values for arguments 'grdthres',
% 'fltr4LM_R', 'multirad' and 'fltr4accum' are used.
%
%%%%%%%%% EXAMPLE #1:
% rawimg = imread('TestImg_CHT_a3.bmp')
% tic
% [accum, circen, cirrad] = CircularHough_Grd(rawimg, [15 60], 10, 20)
% toc
% figure(1)imagesc(accum)axis image
% title('Accumulation Array from Circular Hough Transform')
% figure(2)imagesc(rawimg)colormap('gray')axis image
% hold on
% plot(circen(:,1), circen(:,2), 'r+')
% for k = 1 : size(circen, 1),
% DrawCircle(circen(k,1), circen(k,2), cirrad(k), 32, 'b-')
% end
% hold off
% title(['Raw Image with Circles Detected ', ...
% '(center positions and radii marked)'])
% figure(3)surf(accum, 'EdgeColor', 'none')axis ij
% title('3-D View of the Accumulation Array')
%
% COMMENTS ON EXAMPLE #1:
% The shapes in the raw image are not very good circles. As a result,
% the profile of the peaks in the accumulation array are kind of
% 'stumpy', which can be seen clearly from the 3-D view of the
% accumulation array. (As a comparison, please see the sharp peaks in
% the accumulation array in example #0) To extract the peak positions
% nicely, a value of 20 (default is 8) is used for argument 'fltr4LM_R',
% which is the radius of the filter used in the search of peaks.
%
%%%%%%%%% EXAMPLE #2:
% rawimg = imread('TestImg_CHT_b3.bmp')
% fltr4img = [1 1 1 1 11 2 2 2 11 2 4 2 11 2 2 2 11 1 1 1 1]
% fltr4img = fltr4img / sum(fltr4img(:))
% imgfltrd = filter2( fltr4img , rawimg )
% tic
% [accum, circen, cirrad] = CircularHough_Grd(imgfltrd, [15 80], 8, 10)
% toc
% figure(1)imagesc(accum)axis image
% title('Accumulation Array from Circular Hough Transform')
% figure(2)imagesc(rawimg)colormap('gray')axis image
% hold on
% plot(circen(:,1), circen(:,2), 'r+')
% for k = 1 : size(circen, 1),
% DrawCircle(circen(k,1), circen(k,2), cirrad(k), 32, 'b-')
% hold off
% title(['Raw Image with Circles Detected ', ...
% '(center positions and radii marked)'])
后续完整请参阅ilovematlab.cn/thread-74414-1-1.htmlO(∩_∩)O谢谢
1楼说的对,识别圆就用Hough圆检测函数cvHoughCircles();至于识别颜色,无非就是写一个循环函数对每个像素判断,不麻烦。都不用二值化。函数定义:CvSeq*cvHoughCircles(CvArr*image,void*circle_storage,
intmethod,doubledp,doublemin_dist,doubleparam1=100,doubleparam2=100,intmin_radius=0,intmax_radius=0)。
使用例子:
CvMemStorage*storage=cvCreateMemStorage(0)//定义存储器
cvCvtColor(img,gray,CV_BGR2GRAY)//将原图转换为灰度图处理
cvSmooth(gray,gray,CV_GAUSSIAN,9,9)//平滑图像
CvSeq*circles=cvHoughCircles(gray,storage,CV_HOUGH_GRADIENT,2,gray->height/4,200,100)//调用Hough圆检测函数,其中后面几个参数可以根据实际情况修改
inti//画出检测到的所有圆。
for(i=0i<circles->totali++)
{
float*p=(float*)cvGetSeqElem(circles,i)
cvCircle(img,cvPoint(cvRound(p[0]),cvRound(p[1])),3,
CV_RGB(0,255,0),-1,8,0)
cvCircle(img,cvPoint(cvRound(p[0]),cvRound(p[1])),
cvRound(p[2]),CV_RGB(255,0,0),3,8,0)
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)