Matlab 区域生长法不';对应用直方图均衡化的图像不起作用
我正在尝试应用FEX上的区域增长算法。算法如下:Matlab 区域生长法不';对应用直方图均衡化的图像不起作用,matlab,image-processing,Matlab,Image Processing,我正在尝试应用FEX上的区域增长算法。算法如下: function J=regiongrowing(I) if(exist('reg_maxdist','var')==0), reg_maxdist=0.2; end if(exist('y','var')==0), figure, imshow(I,[]); [y,x]=getpts; y=round(y(1)); x=round(x(1)); end J = zeros(size(I)); % Output Isizes = si
function J=regiongrowing(I)
if(exist('reg_maxdist','var')==0), reg_maxdist=0.2; end
if(exist('y','var')==0), figure, imshow(I,[]); [y,x]=getpts; y=round(y(1)); x=round(x(1)); end
J = zeros(size(I)); % Output
Isizes = size(I); % Dimensions of input image
reg_mean = I(x,y); % The mean of the segmented region
reg_size = 1; % Number of pixels in region
% Free memory to store neighbours of the (segmented) region
neg_free = 10000; neg_pos=0;
neg_list = zeros(neg_free,3);
pixdist=0; % Distance of the region newest pixel to the regio mean
% Neighbor locations (footprint)
neigb=[-1 0; 1 0; 0 -1;0 1];
% Start regiogrowing until distance between regio and posible new pixels become
% higher than a certain treshold
while(pixdist<reg_maxdist&®_size<numel(I))
% Add new neighbors pixels
for j=1:4,
% Calculate the neighbour coordinate
xn = x +neigb(j,1); yn = y +neigb(j,2);
% Check if neighbour is inside or outside the image
ins=(xn>=1)&&(yn>=1)&&(xn<=Isizes(1))&&(yn<=Isizes(2));
% Add neighbor if inside and not already part of the segmented area
if(ins&&(J(xn,yn)==0))
neg_pos = neg_pos+1;
neg_list(neg_pos,:) = [xn yn I(xn,yn)]; J(xn,yn)=1;
end
end
% Add a new block of free memory
if(neg_pos+10>neg_free), neg_free=neg_free+10000; neg_list((neg_pos+1):neg_free,:)=0; end
% Add pixel with intensity nearest to the mean of the region, to the region
dist = abs(neg_list(1:neg_pos,3)-reg_mean);
[pixdist, index] = min(dist);
J(x,y)=2; reg_size=reg_size+1;
% Calculate the new mean of the region
reg_mean= (reg_mean*reg_size + neg_list(index,3))/(reg_size+1);
% Save the x and y coordinates of the pixel (for the neighbour add proccess)
x = neg_list(index,1); y = neg_list(index,2);
% Remove the pixel from the neighbour (check) list
neg_list(index,:)=neg_list(neg_pos,:); neg_pos=neg_pos-1;
end
% Return the segmented area as logical matrix
J=J>1;
提供图像
imhist
输出是一维直方图,而不是图像。你希望从中得到什么?@Bentoy13我不知道为什么我在使用histeq准备图像时发布了imhist。我已经编辑了我的答案,我只是在逻辑上思考这个问题-这可能是我看到像素的白色块而不是红色块的原因吗?是因为像素密度从白色到灰色太接近了吗?如果是这样的话,有没有办法调整这一点?更改这一行cookiesImage=histeq(im2double(imread('cookies.png'))
tocookiesImage=histeq(双精度(imread('cookies.png'))
或将cookiesImage
与255相乘,然后重试。它可能适合您
cookiesImage = histeq(im2double(imread('cookies.png')));
test = regiongrowing(cookiesImage);
figure, imshow(test + cookiesImage);