function outROI = PS_roiEnhance_energy(tempCrop,M,B0)
%1. This function removes background noise for the cropped ROI.
%2. This function also identifies the dominant object within the cropped
%   and remove other smaller objects.
%3. Input variable is the cropped ROI and output variable is the cleaned up
%   ROI.
%
% Created By: Kaichang CHENG, Jul 10,2019
%**************************************************************************
%tempCrop1 = imadjust(tempCrop,stretchlim(tempCrop));
% h = fspecial('gaussian',3,0.5);%
% tempCrop=imfilter(tempCrop,h,'symmetric');
[m,n]=size(tempCrop);
%h = fspecial('gaussian',3,0.5);%
%tempCrop=imfilter(tempCrop,h,'symmetric');
tempCrop0 = padarray(tempCrop, [1, 1], 'symmetric', 'both'); %
tempCrop1=medfilt2(tempCrop0,[3,3]);
tempCrop=tempCrop1(2:(m+1),2:(n+1));
C=double(tempCrop);
B=zeros(size(C,1)-2,size(C,2)-2);
for i=1:size(C,1)-2
    for j=1:size(C,2)-2
        %Sobel mask for x-direction:
        Gx=((2*C(i+2,j+1)+C(i+2,j)+C(i+2,j+2))-(2*C(i,j+1)+C(i,j)+C(i,j+2)));
        %Sobel mask for y-direction:
        Gy=((2*C(i+1,j+2)+C(i,j+2)+C(i+2,j+2))-(2*C(i+1,j)+C(i,j)+C(i+2,j)));
        
        %The gradient of the image
        %B(i,j)=abs(Gx)+abs(Gy);
        B(i,j)=sqrt(Gx.^2+Gy.^2);
        
    end
end

temp1 = uint8(255 * mat2gray(B));
%tempCrop1a= padarray(temp1, [1 1], 0, 'both');%扩充一圈
tempCrop1a = imresize(temp1, size(tempCrop));%保证下一步的处理结果的尺寸为原图尺寸
%tempIm=double(tempCrop)-double(tempCrop1a);
tempIm=double(255-tempCrop)+double(tempCrop1a);

TempIm0=tempIm(:);
MyMin=min(TempIm0);
MyMax=max(TempIm0);
TempIm00=255*(TempIm0-MyMin)/(MyMax-MyMin);
tempImage=uint8(reshape(TempIm00,size(tempIm)));

mask = zeros(size(tempCrop));
mask(5:end-5,5:end-5) = 1;
BW1a = activecontour(tempImage, mask, M);
if any(BW1a(:))
  BW1=BW1a;
    %Remove smaller ROIs
    se = strel('disk',1);
    closeBW = imclose(BW1,se);
    BW2 = imfill(closeBW, 'holes');
    %BW3 = bwmorph(BW2, 'clean');%去除图像中孤立的亮点，比如， 一个像素点， 像素值为1， 其周围像素的像素值全为0， 则这个孤立的亮点将被去除；
    BW3 = bwareafilt(BW2, 3);   % 取面积最大的前3个连通域
    props = regionprops(BW3, 'boundingbox','centroid','FilledImage');
    props_cell=struct2cell(props);
    if size(props_cell,2)==3
        one=norm(props(1).Centroid-[n/2,m/2]);
        two=norm(props(2).Centroid-[n/2,m/2]);
        three=norm(props(3).Centroid-[n/2,m/2]);
        [~,in]=min([one,two,three]);
    elseif size(props_cell,2)==2
        one=norm(props(1).Centroid-[n/2,m/2]);
        two=norm(props(2).Centroid-[n/2,m/2]);
        [~,in]=min([one,two]);
    else
        in=1;
    end
    BW4=props(in).FilledImage;
    %subplot(132),imshow(BW4);title('二值化效果')
    
    %tempCrop = imresize(tempCrop, size(BW4));
    % props = regionprops(BW4, 'boundingbox');
    tempBox1a = props(in).BoundingBox;
    outROI = imcrop(tempCrop, tempBox1a);
    BW5 = imresize(BW4, size(outROI));
    if B0==0
        back=mean(tempCrop(:));
    else
        back=255;
    end
    outROI(~BW5) = back;                            %以原始ROI的最大值填充背景
    outROI = padarray(outROI, [1 1], back, 'both'); %以最大值扩充1圈  
else
    outROI = tempCrop;
end
end