基于密度的图像形状

时间:2016-08-25 09:09:01

标签: image matlab image-processing

我有一张图片

enter image description here

现在我要做的是我想要移动一个mxn窗口,该窗口按比例(不完全)按比例计算Image.Then我会在图像中水平移动窗口,如果我发现一些密集区域(阈值是常数,如0.35)我会使中心像素为1另外0。

我在matlab中有以下代码

function result=imagePixelDensity(Image,thresh)
    [WidthI,HeightI]=size(Image);
    WidthW=round(0.04*WidthI);   % window width
    HeightW=round(0.02*HeightI);  %window height

    if(mod(WidthW,2)~=1)         % to make odd dimension
        WidthW=WidthW+1;        
    end
    if(mod(HeightW,2)~=1)       % to make odd dimension
        HeightW=HeightW+1;
    end

    minNumber=min(WidthW,HeightW);  % since height and width could vary so I am selecting lowest
    WidthW=minNumber;
    HeightW=minNumber;

    totalDensity=WidthW*HeightW;     % total counts of 1 in a window
    colLimit=WidthI-WidthW+1;        % since window cannot cover the whole width of Image(as it has its own dimension)  so I need to restrict its col movements
    rowLimit=HeightI-HeightW+1;      % since window cannot cover the whole height of Image(as it has its own dimension)  so I need to restrict its row movements
    stepRow=0;
    stepCol=0;
    for r=1:rowLimit
        for c=1:colLimit
            if(((HeightW+stepRow)<=rowLimit) && (WidthW+stepCol)<=colLimit)

                Temp=Image(r:HeightW+stepRow,c:WidthW+stepCol);   % get a chunk equal to window size from image
                dens=sum(Temp(:))/totalDensity;                   % calculating density
                rowMid=(r+HeightW+stepRow)/2;                     % row index of center of chunk 
                colMid=(c+WidthW+stepCol)/2;                      % col index of center of chunk    


                if(dens>=thresh)
                    Image(rowMid,colMid)=1;   % making that center pixel to 1 in the original image
                else
                    Image(rowMid,colMid)=0;
                end     
            end
             stepCol=stepCol+1;
        end
       stepRow=stepRow+1;
    end
    result =Image;
end

结果应该是我标记为红色的区域,因为这些是更密集的区域将成为矩形区域,因为像素将是1s。

但我没有得到所需的结果(实际上根本没有发生任何事情)。

有人可以在这里指导我或有更好的想法吗?

1 个答案:

答案 0 :(得分:1)

您的问题是Image(rowMid,colMid)=1;

rowMidcolMid是标量,意味着单个值,因此您不会制作所有白色&#34;,而只是一个像素。你需要像

这样的东西
Image(rowMid-winsize/2:rowMid+winsize/2,colMid-winsize/2:colMid+winsize/2)=1;

此外,我还会使用Imageauxresult。因为如果您修改原始图像,那么您将在下一次迭代中检测到该卡盘。不要修改输入数据。