matlab 在图像处理中常用的函数使用(1)
2015-09-08 18:37
776 查看
<span style="font-size:18px;">tic; % Start timer. clc; % Clear command window. clearvars; % Get rid of variables from prior run of this m-file. fprintf('Running BlobsDemo.m...\n'); % Message sent to command window. workspace; % Make sure the workspace panel with all the variables is showing. imtool close all; % Close all imtool figures. format long g; format compact; captionFontSize = 14; % Check that user has the Image Processing Toolbox installed. hasIPT = license('test', 'image_toolbox'); if ~hasIPT % User does not have the toolbox installed. message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?'); reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes'); if strcmpi(reply, 'No') % User said No, so exit. return; end end % Read in a standard MATLAB demo image of coins (US nickles and dimes, which are 5 cent and 10 cent coins) baseFileName = 'coins.png'; folder = fileparts(which(baseFileName)); % Determine where demo folder is (works with all versions). fullFileName = fullfile(folder, baseFileName); if ~exist(fullFileName, 'file') % It doesn't exist in the current folder. % Look on the search path. if ~exist(baseFileName, 'file') % It doesn't exist on the search path either. % Alert user that we can't find the image. warningMessage = sprintf('Error: the input image file\n%s\nwas not found.\nClick OK to exit the demo.', fullFileName); uiwait(warndlg(warningMessage)); fprintf(1, 'Finished running BlobsDemo.m.\n'); return; end % Found it on the search path. Construct the file name. fullFileName = baseFileName; % Note: don't prepend the folder. end % If we get here, we should have found the image file. originalImage = imread(fullFileName); % Check to make sure that it is grayscale, just in case the user substituted their own image. [rows, columns, numberOfColorChannels] = size(originalImage); if numberOfColorChannels > 1 promptMessage = sprintf('Your image file has %d color channels.\nThis demo was designed for grayscale images.\nDo you want me to convert it to grayscale for you so you can continue?', numberOfColorChannels); button = questdlg(promptMessage, 'Continue', 'Convert and Continue', 'Cancel', 'Convert and Continue'); if strcmp(button, 'Cancel') fprintf(1, 'Finished running BlobsDemo.m.\n'); return; end % Do the conversion using standard book formula originalImage = rgb2gray(originalImage); end % Display the grayscale image. subplot(3, 3, 1); imshow(originalImage); %% % Maximize the figure window. set(gcf, 'units','normalized','outerposition',[0 0 1 1]); % Force it to display RIGHT NOW (otherwise it might not display until it's all done, unless you've stopped at a breakpoint.) drawnow; caption = sprintf('Original "coins" image showing\n6 nickels (the larger coins) and 4 dimes (the smaller coins).'); title(caption, 'FontSize', captionFontSize); axis image; % Make sure image is not artificially stretched because of screen's aspect ratio. %% % Just for fun, let's get its histogram and display it. [pixelCount, grayLevels] = imhist(originalImage); subplot(3, 3, 2); bar(pixelCount); title('Histogram of original image', 'FontSize', captionFontSize); xlim([0 grayLevels(end)]); % Scale x axis manually. grid on; %% % Threshold the image to get a binary image (only 0's and 1's) of class "logical." % Method #1: using im2bw() % normalizedThresholdValue = 0.4; % In range 0 to 1. % thresholdValue = normalizedThresholdValue * max(max(originalImage)); % Gray Levels. % binaryImage = im2bw(originalImage, normalizedThresholdValue); % One way to threshold to binary % Method #2: using a logical operation. thresholdValue = 100; binaryImage = originalImage > thresholdValue; % Bright objects will be chosen if you use >. % ========== IMPORTANT OPTION ============================================================ % Use < if you want to find dark objects instead of bright objects. % binaryImage = originalImage < thresholdValue; % Dark objects will be chosen if you use <. % Do a "hole fill" to get rid of any background pixels or "holes" inside the blobs. binaryImage = imfill(binaryImage, 'holes'); % Show the threshold as a vertical red bar on the histogram. hold on; maxYValue = ylim; line([thresholdValue, thresholdValue], maxYValue, 'Color', 'r'); % Place a text label on the bar chart showing the threshold. annotationText = sprintf('Thresholded at %d gray levels', thresholdValue); % For text(), the x and y need to be of the data class "double" so let's cast both to double. text(double(thresholdValue + 5), double(0.5 * maxYValue(2)), annotationText, 'FontSize', 10, 'Color', [0 .5 0]); text(double(thresholdValue - 70), double(0.94 * maxYValue(2)), 'Background', 'FontSize', 10, 'Color', [0 0 .5]); text(double(thresholdValue + 50), double(0.94 * maxYValue(2)), 'Foreground', 'FontSize', 10, 'Color', [0 0 .5]); % Display the binary image. subplot(3, 3, 3); imshow(binaryImage); title('Binary Image, obtained by thresholding', 'FontSize', captionFontSize); </span>
相关文章推荐
- 模拟退火算法2(实例分析)--Matlab算法
- matlab map
- Matlab和C语言混合编程,包含目录的设定
- Matlab计算运行时间
- Matlab查看数值不用科学计数法显示
- matlab中imresize函数
- matlab2014+vs2010编程
- 如何将matlab画出的图片保存为要求精度
- Matlab cell函数
- 截图保存 matlab
- dir matlab
- Matlab函数——std,std2与mean,mean2区别
- Matlab 仿真实现TI Instaspin 的Foc 逆Clarke变换和SVPWM
- matlab std函数 用法及实例
- Matlab基本用法
- matlab实现人脸识别
- 11.9 Matlab 中Figure的创建和设置
- Matlab psf2otf与fft2函数的关系
- 初学MATLAB笔记-绘图
- MATLAB — axis