matlab 删除图片中大于一定面积的像素

删除图片中大于一定面积的像素

matlab自带函数bw1=bwareaopen(bw,p,conn),改函数的作用是删除像素连通小于一定p的,comm是邻域,取值可以是4,8,16,默认是8。若要删除大于一定连通区域的像素,可以根据该函数进行修改,核心修改如下

%打开bwareaopen.m
idxToKeep = CC.PixelIdxList(area <= p);

修改为

idxToKeep = CC.PixelIdxList(area <= p);

其效果
在这里插入图片描述

clc
clear all
close all
img_data=strcat(pwd,'/img/');
img_name=dir(img_data);
len=length(img_name)-3;
%计算每个像素的面积
area_th=2;
area_per_pix=(1392*1040)/(440*330);
area_p=area_th/area_per_pix;
area_p=ceil(area_p);


for i=1:len
   img_path=strcat(img_data,img_name(i+2).name);
   img=imread(img_path);
   %灰度图
   subplot(2,2,1);
   imshow(img);
   title('原图');
   img_gray=rgb2gray(img);
   subplot(2,2,2);
   imshow(img_gray);
   title('灰度图');
   %图像二值化
   thresh=graythresh(img_gray);
   bw=im2bw(img_gray,thresh);
   subplot(2,2,3);
   imshow(bw);
   title('二值化图')
   bw=~bw;
   bw1=delete_bw_area(bw,area_p,8);
 
   subplot(2,2,4);
   bw1_logical(:,:,1)=bw1;
   bw1_logical(:,:,2)=bw1_logical(:,:,1);
   bw1_logical(:,:,3)=bw1_logical(:,:,1);
   
   new_img=~img;
   bw2=new_img&bw1_logical;
   bw2=~bw2;
   bw2=double(bw2);
   imshow(bw2);
   title('去除颗粒后的图');
   save_path=strcat(pwd,'/result/');
   save_name=strcat(save_path,img_name(i+2).name);
   imwrite(bw2,save_name);
end

delete_bw_area.m

function bw2 = delete_bw_area(varargin)
%BWAREAOPEN Remove small objects from binary image.
%   BW2 = BWAREAOPEN(BW,P) removes from a binary image all connected
%   components (objects) that have fewer than P pixels, producing another
%   binary image BW2.  This operation is known as an area opening.  The
%   default connectivity is 8 for two dimensions, 26 for three dimensions,
%   and CONNDEF(NDIMS(BW),'maximal') for higher dimensions. 
%
%   BW2 = BWAREAOPEN(BW,P,CONN) specifies the desired connectivity.  CONN
%   may have the following scalar values:  
%
%       4     two-dimensional four-connected neighborhood
%       8     two-dimensional eight-connected neighborhood
%       6     three-dimensional six-connected neighborhood
%       18    three-dimensional 18-connected neighborhood
%       26    three-dimensional 26-connected neighborhood
%
%   Connectivity may be defined in a more general way for any dimension by
%   using for CONN a 3-by-3-by- ... -by-3 matrix of 0s and 1s.  The
%   1-valued elements define neighborhood locations relative to the center
%   element of CONN.  CONN must be symmetric about its center element.
%
%   Class Support
%   -------------
%   BW can be a logical or numeric array of any dimension, and it must be
%   nonsparse.
%
%   BW2 is logical.
%
%   Example
%   -------
%   Remove all objects in the image text.png containing fewer than 50
%   pixels.
%
%       BW = imread('text.png');
%       BW2 = bwareaopen(BW,50);
%       imshow(BW);
%       figure, imshow(BW2)
%
%   See also BWCONNCOMP, CONNDEF, REGIONPROPS.

%   Copyright 1993-2011 The MathWorks, Inc.

% Input/output specs
% ------------------
% BW:    N-D real full matrix
%        any numeric class
%        sparse not allowed
%        anything that's not logical is converted first using
%          bw = BW ~= 0
%        Empty ok
%        Inf's ok, treated as 1
%        NaN's ok, treated as 1
%
% P:     double scalar
%        nonnegative integer
%
% CONN:  connectivity
%
% BW2:   logical, same size as BW
%        contains only 0s and 1s.

[bw,p,conn] = parse_inputs(varargin{:});

CC = bwconncomp(bw,conn);
area = cellfun(@numel, CC.PixelIdxList);

idxToKeep = CC.PixelIdxList(area <= p);
idxToKeep = vertcat(idxToKeep{:});

bw2 = false(size(bw));
bw2(idxToKeep) = true;


%%%
%%% parse_inputs
%%%
function [bw,p,conn] = parse_inputs(varargin)

narginchk(2,3)

bw = varargin{1};
validateattributes(bw,{'numeric' 'logical'},{'nonsparse'},mfilename,'BW',1);
if ~islogical(bw)
    bw = bw ~= 0;
end

p = varargin{2};
validateattributes(p,{'double'},{'scalar' 'integer' 'nonnegative'},...
    mfilename,'P',2);

if (nargin >= 3)
    conn = varargin{3};
else
    conn = conndef(ndims(bw),'maximal');
end
iptcheckconn(conn,mfilename,'CONN',3)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

暗嘿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值