一个优秀的人都有一个良好的习惯以及一个优秀的习惯。
在建筑风格上,按照时代大致分为以下类型,而在程序设计的过程中,不同的程序员都有自己的风格,而熟悉的程序员的代码也会有熟悉的感觉,一个程序员的编程风格就跟一个建筑师的建筑风格一样重要,可以会很多建筑风格,但是必须擅长其中一二,不然就没有风格和资本在这个领域立稳脚跟。
在此,先贴出建筑风格,摘自维基百科:http://zh.wikipedia.org/wiki/%E5%BB%BA%E7%AD%91%E9%A3%8E%E6%A0%BC
高屋建瓴很重要,不仅要知其然还得知其所以然。
单从编程语言入手,一张图就找到自己目前的位置,摘自维基百科:http://zh.wikipedia.org/wiki/%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%AF%AD%E8%A8%80
作为一个程序员,在面对代码时就没有理由说自己不会。
先贴出一段代码:
template<typename T>
class WImage
{
public:
typedef T BaseType;
// WImage is an abstract class with no other virtual methods so make the
// destructor virtual.
virtual ~WImage() = 0;
// Accessors
IplImage* Ipl() {return image_; }
const IplImage* Ipl() const {return image_; }
T* ImageData() { return reinterpret_cast<T*>(image_->imageData); }
const T* ImageData() const {
return reinterpret_cast<const T*>(image_->imageData);
}
int Width() const {return image_->width; }
int Height() const {return image_->height; }
// WidthStep is the number of bytes to go to the pixel with the next y coord
int WidthStep() const {return image_->widthStep; }
int Channels() const {return image_->nChannels; }
int ChannelSize() const {return sizeof(T); } // number of bytes per channel
// Number of bytes per pixel
int PixelSize() const {return Channels() * ChannelSize(); }
// Return depth type (e.g. IPL_DEPTH_8U, IPL_DEPTH_32F) which is the number
// of bits per channel and with the signed bit set.
// This is known at compile time using specializations.
int Depth() const;
inline const T* Row(int r) const {
return reinterpret_cast<T*>(image_->imageData + r*image_->widthStep);
}
inline T* Row(int r) {
return reinterpret_cast<T*>(image_->imageData + r*image_->widthStep);
}
// Pixel accessors which returns a pointer to the start of the channel
inline T* operator() (int c, int r) {
return reinterpret_cast<T*>(image_->imageData + r*image_->widthStep) +
c*Channels();
}
inline const T* operator() (int c, int r) const {
return reinterpret_cast<T*>(image_->imageData + r*image_->widthStep) +
c*Channels();
}
// Copy the contents from another image which is just a convenience to cvCopy
void CopyFrom(const WImage<T>& src) { cvCopy(src.Ipl(), image_); }
// Set contents to zero which is just a convenient to cvSetZero
void SetZero() { cvSetZero(image_); }
// Construct a view into a region of this image
WImageView<T> View(int c, int r, int width, int height);
protected:
// Disallow copy and assignment
WImage(const WImage&);
void operator=(const WImage&);
explicit WImage(IplImage* img) : image_(img) {
assert(!img || img->depth == Depth());
}
void SetIpl(IplImage* image) {
assert(!image || image->depth == Depth());
image_ = image;
}
IplImage* image_;
};
源自opencv的 wimage.hpp文件部分源码。
function varargout = corner(varargin)
% CORNER Find corner points in an image.
% CORNERS = CORNER(I) takes a grayscale or binary image I as its input
% and returns CORNERS. The M-by-2 double matrix CORNERS contains the X
% and Y coordinates of the corner points detected in I.
%
% CORNERS = CORNER(I,METHOD) detects corners in the grayscale or binary
% image I using the specified METHOD. Supported METHODs are:
%
% 'Harris' : The Harris corner detector. This is the
% default METHOD.
% 'MinimumEigenvalue' : Shi & Tomasi's minimum eigenvalue method.
%
% CORNERS = CORNER(I,N) detects corners in the grayscale or binary
% image I and returns a maximum of N corners. If N is not specified,
% the default maximum number of corners returned is 200.
%
% CORNERS = CORNER(I,METHOD,N) detects corners using the specified METHOD
% and maximum number of corners.
%
% CORNERS = CORNER(...,PARAM1,VAL1,PARAM2,VAL2,...) detects corners in I,
% specifying parameters and corresponding values that control various
% aspects of the corner detection algorithm.
%
% Parameter options:
% -------------------
%
% 'FilterCoefficients' A vector, V, of filter coefficients for the
% separable smoothing filter. The full filter
% kernel is given by the outer product, V*V'.
%
% Default value: fspecial('gaussian',[5 1],1.5)
%
% 'QualityLevel' A scalar Q, 0 < Q < 1, specifying the minimum
% accepted quality of corners. The value of Q
% is multiplied by the largest corner
% metric value. Candidate corners that have
% corner metric values less than the product are
% rejected. Larger values of Q can be used to
% remove erroneous corners.
%
% Default value: 0.01
%
% 'SensitivityFactor' A scalar K, 0 < K < 0.25, specifying the
% sensitivity factor used in the Harris
% detection algorithm. The smaller the value
% of K the more likely the algorithm is to
% detect sharp corners. This parameter is only
% valid with the 'Harris' method.
%
% Default value: 0.04
%
% Notes
% -----
% Non-maxima suppression is performed on candidate corners. Corners will be
% at least two pixels apart.
%
% Example
% -------
% Find and plot corner points in checkerboard image.
%
% I = checkerboard(50,2,2);
% corners = corner(I);
% imshow(I);
% hold on
% plot(corners(:,1),corners(:,2),'r*');
%
% See also CORNERMETRIC
% Copyright 2010 The MathWorks, Inc.
% $Revision: 1.1.6.4 $ $Date: 2010/08/07 07:27:17 $
% parse inputs
[I,method,sensitivity_factor,...
filter_coef,max_corners,quality_level] = parseInputs(varargin{:});
% Pass through parsed inputs to cornermetric. Only specify
% SensitivityFactor if Harris is the method. We've already verified in
% parseInputs that the user did not specify SensitivityFactor with a method
% other than Harris.
if strcmpi(method,'Harris');
cMetric = cornermetric(I,...
'Method',method,...
'SensitivityFactor',sensitivity_factor,...
'FilterCoefficients',filter_coef);
else
cMetric = cornermetric(I,...
'Method',method,...
'FilterCoefficients',filter_coef);
end
% Use findpeak to do peak detection on output of cornermetric to find x/y
% locations of peaks.
[xpeak, ypeak] = findLocalPeak(cMetric,quality_level);
% Create vector of interest points from x/y peak locations.
corners = [xpeak, ypeak];
% Return N corners as specified by maxNumCorners optional input argument.
if max_corners < size(corners,1)
corners = corners(1:max_corners,:);
end
varargout{1} = corners;
%------------------------------------------------------------------
function BW = suppressLowCornerMetricMaxima(cMetric,BW,quality_level)
max_cmetric = max(cMetric(:));
if max_cmetric > 0
min_metric = quality_level * max_cmetric;
else
% Edge case: All corner metric values are 0 or less than zero. In this
% case, mask all local maxima and return. This case arrises for uniform
% input images.
BW(:) = false;
return
end
% Mask peak locations that are less than min_metric.
BW(cMetric < min_metric) = false;
%-------------------------------------------------------------------------
function [xpeak,ypeak] = findLocalPeak(cMetric,quality_level)
% The cornermetric matrix is all equal values for all input
% images with numRows/numCols <= 3. We require at least 4 rows
% and 4 colums to return a non-empty corner array.
[numRows,numCols] = size(cMetric);
if ( (numRows < 4) || (numCols < 4))
xpeak = [];
ypeak = [];
return
end
% Find local maxima of corner metric matrix.
BW = imregionalmax(cMetric,8);
BW = suppressLowCornerMetricMaxima(cMetric,BW,quality_level);
% Suppress connected components which have same intensity and are part of
% one local maxima grouping. We want to 'thin' these local maxima to a
% single point using bwmorph.
BW = bwmorph(BW,'shrink',Inf);
% Return r/c locations that are valid non-thresholded corners. Return
% corners in order of decreasing corner metric value.
[r,c] = sortCornersByCornerMetric(BW,cMetric);
xpeak = c;
ypeak = r;
%-------------------------------------------------------------------------
function [r,c] = sortCornersByCornerMetric(BW,cMetric)
ind = find(BW);
[~,sorted_ind] = sort(cMetric(ind),'descend');
[r,c] = ind2sub(size(BW),ind(sorted_ind));
%-------------------------------------------------------------------------
function [I,method,sensitivity_factor,filter_coef,...
max_corners, quality_level] = parseInputs(varargin)
% Reform varargin into consistent ordering: corner(I,METHOD,N) so that
% we can use inputParser to do input parsing on optional args and P/V
% pairs.
if (nargin>1 && isnumeric(varargin{2}))
if nargin>2
%corner(I,N,P1,V1,...)
varargin(4 : (nargin+1) ) = varargin(3:nargin);
end
%Now that any P/V have been handled, reorder 2nd and 3rd arguments
%into the form:
%corner(I,METHOD,N,...)
varargin{3} = varargin{2};
varargin{2} = 'Harris';
end
parser = commonCornerInputParser(mfilename);
parser.addOptional('N',200,@checkMaxCorners);
parser.addParamValue('QualityLevel',0.01,@checkQualityLevel);
% parse input
parser.parse(varargin{:});
% assign outputs
I = parser.Results.Image;
method = parser.Results.Method;
sensitivity_factor = parser.Results.SensitivityFactor;
filter_coef = parser.Results.FilterCoefficients;
max_corners = parser.Results.N;
quality_level = parser.Results.QualityLevel;
% check for incompatible parameters. if user has specified a sensitivity
% factor with method other than harris, we error. We made the sensitivity
% factor default value a string to determine if one was specified or if the
% default was provided since we cannot get this information from the input
% parser object.
method_is_not_harris = ~strcmpi(method,'Harris');
sensitivity_factor_specified = ~ischar(sensitivity_factor);
if method_is_not_harris && sensitivity_factor_specified
eid = sprintf('Images:%s:invalidParameterCombination',mfilename);
error(eid,'%s',['The ''SensitivityFactor'' parameter is only '...
'valid with the ''Harris'' method.']);
end
% convert from default strings to actual values.
if ischar(sensitivity_factor)
sensitivity_factor = str2double(sensitivity_factor);
end
%-------------------------------
function tf = checkMaxCorners(x)
validateattributes(x,{'numeric'},{'nonempty','nonnan','real',...
'scalar','integer','positive','nonzero'},mfilename,'N');
tf = true;
%-------------------------------
function tf = checkQualityLevel(x)
validateattributes(x,{'numeric'},{'nonempty','nonnan','real',...
'scalar'},mfilename,'QualityLevel');
% The valid range is (0,1).
if (x >=1) || (x <=0)
error('Images:corner:invalidQualityLevel','The ''QualityLevel'' parameter must be in the range (0,1).');
end
tf = true;
以上是matlab安装目录的一个文件的源码。
当适应了一门语言的源码的风格后,理解和使用这门语言也就比较顺手了,个人的见解:
对一门语言的掌握分为以下几个层次:
层次一:
百度+书籍+sample
层次二:
解读源码库
层次三:
优化,重配置
层次四:
神人空间
如何形成自己的风格?
首先必须要了解这个领域的历史,掌握或者熟悉几种或者最好大部分的风格,站在巨人的肩膀上才能看的更远。爱因斯坦的相对论提出也需要学习黎曼,高斯等一批前辈的成果,每一个成果都是在前人的基础上完成的。
客观的看待风格,你可能喜欢一类风格,但是也不能贬低另一种风格,也许就是你鄙视的人在将来某一天限制你的发展,风格跟人一样。
在实践中提高,形成一种风格,需要大量的实践,在实践中尽力的完善,在实践中坚持。

被折叠的 条评论
为什么被折叠?



