删除图片中大于一定面积的像素
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=